Submission #4828360
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>; using LL = long long;
using VI = vector<int>; using VVI = vector<VI>;
using PII = pair<int, int>; using PLL = pair<LL, LL>;
using VL = vector<LL>; using VVL = vector<VL>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9; const LL LINF = 1e16;
const LL MOD = 1000000007; const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
/* ----- 2019/04/05 Problem: ABC 040 D / Link: http://abc040.contest.atcoder.jp/tasks/abc040_d ----- */
struct UnionFind {
vector<int> data;
UnionFind(int n) { data.assign(n, -1); }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
LL N, M; cin >> N >> M;
using tp = tuple<int, int, int, int, int>;
// y, q, a, b, queryi
vector<tp>querys;
FOR(_, 0, M) {
int a, b, y;
cin >> a >> b >> y;
a--, b--;
querys.push_back(tp(y, 0, a, b, -1));
}
int Q; cin >> Q;
FOR(i, 0, Q) {
int v, y; cin >> v >> y;
v--;
querys.push_back(tp(y, 1, v, 0, i));
}
RSORT(querys);
UnionFind uf(N);
VL ans(Q);
for (auto query : querys) {
int y, q, a, b, id;
tie(y, q, a, b, id) = query;
if (q == 1) {
ans[id] = uf.size(a);
}
else {
uf.unionSet(a, b);
}
}
for (auto it : ans) {
cout << it << endl;
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - 道路の老朽化対策について |
| User |
Yang33 |
| Language |
C++14 (GCC 5.4.1) |
| Score |
100 |
| Code Size |
2220 Byte |
| Status |
AC |
| Exec Time |
259 ms |
| Memory |
12524 KiB |
Judge Result
| Set Name |
Sample |
Subtask1 |
All |
| Score / Max Score |
0 / 0 |
50 / 50 |
50 / 50 |
| Status |
|
|
|
| Set Name |
Test Cases |
| Sample |
sample_01.txt, sample_02.txt, sample_03.txt |
| Subtask1 |
sample_01.txt, sample_02.txt, sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt |
| All |
sample_01.txt, sample_02.txt, sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt |
| Case Name |
Status |
Exec Time |
Memory |
| sample_01.txt |
AC |
1 ms |
256 KiB |
| sample_02.txt |
AC |
1 ms |
256 KiB |
| sample_03.txt |
AC |
1 ms |
256 KiB |
| subtask1_01.txt |
AC |
4 ms |
384 KiB |
| subtask1_02.txt |
AC |
4 ms |
384 KiB |
| subtask1_03.txt |
AC |
4 ms |
384 KiB |
| subtask1_04.txt |
AC |
3 ms |
384 KiB |
| subtask1_05.txt |
AC |
4 ms |
384 KiB |
| subtask1_06.txt |
AC |
4 ms |
384 KiB |
| subtask1_07.txt |
AC |
4 ms |
384 KiB |
| subtask2_01.txt |
AC |
250 ms |
11884 KiB |
| subtask2_02.txt |
AC |
253 ms |
12012 KiB |
| subtask2_03.txt |
AC |
254 ms |
10604 KiB |
| subtask2_04.txt |
AC |
255 ms |
11500 KiB |
| subtask2_05.txt |
AC |
217 ms |
7280 KiB |
| subtask2_06.txt |
AC |
253 ms |
11628 KiB |
| subtask2_07.txt |
AC |
252 ms |
12524 KiB |
| subtask2_08.txt |
AC |
248 ms |
12524 KiB |
| subtask2_09.txt |
AC |
256 ms |
12396 KiB |
| subtask2_10.txt |
AC |
259 ms |
11244 KiB |
| subtask2_11.txt |
AC |
255 ms |
11628 KiB |
| subtask2_12.txt |
AC |
255 ms |
11116 KiB |