#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
const int N = 1e5 + 5;
int n, m;
int lab[N], sz[N];
set<int> in[N];
set<ii> out[N];
int64_t ans;
int findset(int x) {
if(lab[x] == x) return x;
return lab[x] = findset(lab[x]);
}
void join(int u, int v) {
int x = findset(u);
int y = findset(v);
if(x == y) return;
auto it = out[y].lower_bound(make_pair(x, 0));
if(it != out[y].end() && (*it).fi == x) {
if(in[x].size() + out[x].size() < in[y].size() + out[y].size()) swap(x, y);
vector<ii> vo;
for (auto &i : out[y]) {
ans -= sz[i.fi];
in[i.fi].erase(i.se);
if(i.fi != x) vo.push_back(i);
}
out[y].clear();
ans += 1ll * sz[y] * (2 * sz[x] + in[x].size() - in[y].size());
vector<int> vi;
for (auto &i : in[y]) {
int j = findset(i);
out[j].erase(make_pair(y, i));
if(j != x) vi.push_back(i);
}
in[y].clear();
lab[y] = x;
sz[x] += sz[y];
for (int &i : vi) join(i, x);
for (ii &i : vo) join(i.se, i.fi);
} else if(!in[y].count(u)) {
ans += 1ll * sz[y];
in[y].insert(u);
out[x].insert(make_pair(y, u));
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < N; ++i)
lab[i] = i, sz[i] = 1;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
join(u, v);
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10476 KB |
Output is correct |
2 |
Correct |
7 ms |
10476 KB |
Output is correct |
3 |
Correct |
7 ms |
10476 KB |
Output is correct |
4 |
Correct |
7 ms |
10476 KB |
Output is correct |
5 |
Correct |
7 ms |
10476 KB |
Output is correct |
6 |
Correct |
7 ms |
10476 KB |
Output is correct |
7 |
Correct |
8 ms |
10604 KB |
Output is correct |
8 |
Correct |
8 ms |
10604 KB |
Output is correct |
9 |
Correct |
8 ms |
10604 KB |
Output is correct |
10 |
Correct |
7 ms |
10476 KB |
Output is correct |
11 |
Correct |
7 ms |
10476 KB |
Output is correct |
12 |
Correct |
7 ms |
10476 KB |
Output is correct |
13 |
Correct |
7 ms |
10476 KB |
Output is correct |
14 |
Correct |
7 ms |
10476 KB |
Output is correct |
15 |
Correct |
8 ms |
10476 KB |
Output is correct |
16 |
Correct |
7 ms |
10476 KB |
Output is correct |
17 |
Correct |
7 ms |
10476 KB |
Output is correct |
18 |
Correct |
7 ms |
10476 KB |
Output is correct |
19 |
Correct |
7 ms |
10476 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
7 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
7 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
7 ms |
10476 KB |
Output is correct |
27 |
Correct |
7 ms |
10476 KB |
Output is correct |
28 |
Correct |
7 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10476 KB |
Output is correct |
32 |
Correct |
7 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10476 KB |
Output is correct |
2 |
Correct |
7 ms |
10476 KB |
Output is correct |
3 |
Correct |
7 ms |
10476 KB |
Output is correct |
4 |
Correct |
7 ms |
10476 KB |
Output is correct |
5 |
Correct |
7 ms |
10476 KB |
Output is correct |
6 |
Correct |
7 ms |
10476 KB |
Output is correct |
7 |
Correct |
8 ms |
10604 KB |
Output is correct |
8 |
Correct |
8 ms |
10604 KB |
Output is correct |
9 |
Correct |
8 ms |
10604 KB |
Output is correct |
10 |
Correct |
7 ms |
10476 KB |
Output is correct |
11 |
Correct |
7 ms |
10476 KB |
Output is correct |
12 |
Correct |
7 ms |
10476 KB |
Output is correct |
13 |
Correct |
7 ms |
10476 KB |
Output is correct |
14 |
Correct |
7 ms |
10476 KB |
Output is correct |
15 |
Correct |
8 ms |
10476 KB |
Output is correct |
16 |
Correct |
7 ms |
10476 KB |
Output is correct |
17 |
Correct |
7 ms |
10476 KB |
Output is correct |
18 |
Correct |
7 ms |
10476 KB |
Output is correct |
19 |
Correct |
7 ms |
10476 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
7 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
7 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
7 ms |
10476 KB |
Output is correct |
27 |
Correct |
7 ms |
10476 KB |
Output is correct |
28 |
Correct |
7 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10476 KB |
Output is correct |
32 |
Correct |
7 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
10 ms |
10604 KB |
Output is correct |
35 |
Correct |
94 ms |
14572 KB |
Output is correct |
36 |
Correct |
117 ms |
16364 KB |
Output is correct |
37 |
Correct |
116 ms |
16108 KB |
Output is correct |
38 |
Correct |
113 ms |
15852 KB |
Output is correct |
39 |
Correct |
9 ms |
10732 KB |
Output is correct |
40 |
Correct |
9 ms |
10604 KB |
Output is correct |
41 |
Correct |
9 ms |
10604 KB |
Output is correct |
42 |
Correct |
9 ms |
10604 KB |
Output is correct |
43 |
Correct |
12 ms |
10732 KB |
Output is correct |
44 |
Correct |
10 ms |
10604 KB |
Output is correct |
45 |
Correct |
8 ms |
10604 KB |
Output is correct |
46 |
Correct |
9 ms |
10604 KB |
Output is correct |
47 |
Correct |
10 ms |
10604 KB |
Output is correct |
48 |
Correct |
10 ms |
10604 KB |
Output is correct |
49 |
Correct |
17 ms |
11244 KB |
Output is correct |
50 |
Correct |
112 ms |
15980 KB |
Output is correct |
51 |
Correct |
13 ms |
10860 KB |
Output is correct |
52 |
Correct |
108 ms |
14956 KB |
Output is correct |
53 |
Correct |
15 ms |
11264 KB |
Output is correct |
54 |
Correct |
106 ms |
15468 KB |
Output is correct |
55 |
Correct |
11 ms |
10988 KB |
Output is correct |
56 |
Correct |
11 ms |
10988 KB |
Output is correct |
57 |
Correct |
11 ms |
10988 KB |
Output is correct |
58 |
Correct |
11 ms |
10988 KB |
Output is correct |
59 |
Correct |
8 ms |
10604 KB |
Output is correct |
60 |
Correct |
98 ms |
13548 KB |
Output is correct |
61 |
Correct |
10 ms |
10752 KB |
Output is correct |
62 |
Correct |
110 ms |
15468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10476 KB |
Output is correct |
2 |
Correct |
7 ms |
10476 KB |
Output is correct |
3 |
Correct |
7 ms |
10476 KB |
Output is correct |
4 |
Correct |
7 ms |
10476 KB |
Output is correct |
5 |
Correct |
7 ms |
10476 KB |
Output is correct |
6 |
Correct |
7 ms |
10476 KB |
Output is correct |
7 |
Correct |
8 ms |
10604 KB |
Output is correct |
8 |
Correct |
8 ms |
10604 KB |
Output is correct |
9 |
Correct |
8 ms |
10604 KB |
Output is correct |
10 |
Correct |
7 ms |
10476 KB |
Output is correct |
11 |
Correct |
7 ms |
10476 KB |
Output is correct |
12 |
Correct |
7 ms |
10476 KB |
Output is correct |
13 |
Correct |
7 ms |
10476 KB |
Output is correct |
14 |
Correct |
7 ms |
10476 KB |
Output is correct |
15 |
Correct |
8 ms |
10476 KB |
Output is correct |
16 |
Correct |
7 ms |
10476 KB |
Output is correct |
17 |
Correct |
7 ms |
10476 KB |
Output is correct |
18 |
Correct |
7 ms |
10476 KB |
Output is correct |
19 |
Correct |
7 ms |
10476 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
7 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
7 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
7 ms |
10476 KB |
Output is correct |
27 |
Correct |
7 ms |
10476 KB |
Output is correct |
28 |
Correct |
7 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10476 KB |
Output is correct |
32 |
Correct |
7 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
10 ms |
10604 KB |
Output is correct |
35 |
Correct |
94 ms |
14572 KB |
Output is correct |
36 |
Correct |
117 ms |
16364 KB |
Output is correct |
37 |
Correct |
116 ms |
16108 KB |
Output is correct |
38 |
Correct |
113 ms |
15852 KB |
Output is correct |
39 |
Correct |
9 ms |
10732 KB |
Output is correct |
40 |
Correct |
9 ms |
10604 KB |
Output is correct |
41 |
Correct |
9 ms |
10604 KB |
Output is correct |
42 |
Correct |
9 ms |
10604 KB |
Output is correct |
43 |
Correct |
12 ms |
10732 KB |
Output is correct |
44 |
Correct |
10 ms |
10604 KB |
Output is correct |
45 |
Correct |
8 ms |
10604 KB |
Output is correct |
46 |
Correct |
9 ms |
10604 KB |
Output is correct |
47 |
Correct |
10 ms |
10604 KB |
Output is correct |
48 |
Correct |
10 ms |
10604 KB |
Output is correct |
49 |
Correct |
17 ms |
11244 KB |
Output is correct |
50 |
Correct |
112 ms |
15980 KB |
Output is correct |
51 |
Correct |
13 ms |
10860 KB |
Output is correct |
52 |
Correct |
108 ms |
14956 KB |
Output is correct |
53 |
Correct |
15 ms |
11264 KB |
Output is correct |
54 |
Correct |
106 ms |
15468 KB |
Output is correct |
55 |
Correct |
11 ms |
10988 KB |
Output is correct |
56 |
Correct |
11 ms |
10988 KB |
Output is correct |
57 |
Correct |
11 ms |
10988 KB |
Output is correct |
58 |
Correct |
11 ms |
10988 KB |
Output is correct |
59 |
Correct |
8 ms |
10604 KB |
Output is correct |
60 |
Correct |
98 ms |
13548 KB |
Output is correct |
61 |
Correct |
10 ms |
10752 KB |
Output is correct |
62 |
Correct |
110 ms |
15468 KB |
Output is correct |
63 |
Correct |
406 ms |
40940 KB |
Output is correct |
64 |
Correct |
410 ms |
41708 KB |
Output is correct |
65 |
Correct |
421 ms |
41324 KB |
Output is correct |
66 |
Correct |
100 ms |
13292 KB |
Output is correct |
67 |
Correct |
185 ms |
17388 KB |
Output is correct |
68 |
Correct |
95 ms |
13292 KB |
Output is correct |
69 |
Correct |
250 ms |
18028 KB |
Output is correct |
70 |
Correct |
110 ms |
13292 KB |
Output is correct |
71 |
Correct |
109 ms |
13268 KB |
Output is correct |
72 |
Correct |
222 ms |
17516 KB |
Output is correct |
73 |
Correct |
222 ms |
17516 KB |
Output is correct |
74 |
Correct |
715 ms |
25608 KB |
Output is correct |
75 |
Correct |
418 ms |
21612 KB |
Output is correct |
76 |
Correct |
549 ms |
24556 KB |
Output is correct |
77 |
Correct |
526 ms |
24812 KB |
Output is correct |
78 |
Correct |
161 ms |
17644 KB |
Output is correct |
79 |
Correct |
287 ms |
19564 KB |
Output is correct |
80 |
Correct |
162 ms |
17644 KB |
Output is correct |
81 |
Correct |
266 ms |
19180 KB |
Output is correct |
82 |
Correct |
490 ms |
31436 KB |
Output is correct |
83 |
Correct |
490 ms |
31456 KB |
Output is correct |
84 |
Correct |
450 ms |
31852 KB |
Output is correct |
85 |
Correct |
454 ms |
31852 KB |
Output is correct |
86 |
Correct |
108 ms |
12652 KB |
Output is correct |
87 |
Correct |
139 ms |
13676 KB |
Output is correct |
88 |
Correct |
231 ms |
17644 KB |
Output is correct |
89 |
Correct |
553 ms |
24300 KB |
Output is correct |