#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 |
10604 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 |
8 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 |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10476 KB |
Output is correct |
16 |
Correct |
8 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 |
10496 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
8 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
8 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
8 ms |
10476 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
8 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10476 KB |
Output is correct |
30 |
Correct |
8 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
8 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10604 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 |
8 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 |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10476 KB |
Output is correct |
16 |
Correct |
8 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 |
10496 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
8 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
8 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
8 ms |
10476 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
8 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10476 KB |
Output is correct |
30 |
Correct |
8 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
8 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
11 ms |
10732 KB |
Output is correct |
35 |
Correct |
94 ms |
15980 KB |
Output is correct |
36 |
Correct |
140 ms |
18028 KB |
Output is correct |
37 |
Correct |
127 ms |
18124 KB |
Output is correct |
38 |
Correct |
112 ms |
17900 KB |
Output is correct |
39 |
Correct |
11 ms |
10604 KB |
Output is correct |
40 |
Correct |
11 ms |
10732 KB |
Output is correct |
41 |
Correct |
10 ms |
10732 KB |
Output is correct |
42 |
Correct |
9 ms |
10604 KB |
Output is correct |
43 |
Correct |
10 ms |
10732 KB |
Output is correct |
44 |
Correct |
10 ms |
10860 KB |
Output is correct |
45 |
Correct |
9 ms |
10604 KB |
Output is correct |
46 |
Correct |
9 ms |
10604 KB |
Output is correct |
47 |
Correct |
10 ms |
10752 KB |
Output is correct |
48 |
Correct |
10 ms |
10732 KB |
Output is correct |
49 |
Correct |
16 ms |
11244 KB |
Output is correct |
50 |
Correct |
113 ms |
18156 KB |
Output is correct |
51 |
Correct |
14 ms |
10988 KB |
Output is correct |
52 |
Correct |
105 ms |
17132 KB |
Output is correct |
53 |
Correct |
20 ms |
11244 KB |
Output is correct |
54 |
Correct |
109 ms |
17644 KB |
Output is correct |
55 |
Correct |
12 ms |
10988 KB |
Output is correct |
56 |
Correct |
16 ms |
10988 KB |
Output is correct |
57 |
Correct |
12 ms |
10988 KB |
Output is correct |
58 |
Correct |
12 ms |
10988 KB |
Output is correct |
59 |
Correct |
9 ms |
10604 KB |
Output is correct |
60 |
Correct |
103 ms |
15468 KB |
Output is correct |
61 |
Correct |
11 ms |
10860 KB |
Output is correct |
62 |
Correct |
115 ms |
17900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10604 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 |
8 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 |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10476 KB |
Output is correct |
16 |
Correct |
8 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 |
10496 KB |
Output is correct |
20 |
Correct |
8 ms |
10476 KB |
Output is correct |
21 |
Correct |
8 ms |
10604 KB |
Output is correct |
22 |
Correct |
8 ms |
10476 KB |
Output is correct |
23 |
Correct |
8 ms |
10604 KB |
Output is correct |
24 |
Correct |
8 ms |
10476 KB |
Output is correct |
25 |
Correct |
8 ms |
10604 KB |
Output is correct |
26 |
Correct |
8 ms |
10476 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
8 ms |
10476 KB |
Output is correct |
29 |
Correct |
7 ms |
10476 KB |
Output is correct |
30 |
Correct |
8 ms |
10476 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
8 ms |
10476 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
11 ms |
10732 KB |
Output is correct |
35 |
Correct |
94 ms |
15980 KB |
Output is correct |
36 |
Correct |
140 ms |
18028 KB |
Output is correct |
37 |
Correct |
127 ms |
18124 KB |
Output is correct |
38 |
Correct |
112 ms |
17900 KB |
Output is correct |
39 |
Correct |
11 ms |
10604 KB |
Output is correct |
40 |
Correct |
11 ms |
10732 KB |
Output is correct |
41 |
Correct |
10 ms |
10732 KB |
Output is correct |
42 |
Correct |
9 ms |
10604 KB |
Output is correct |
43 |
Correct |
10 ms |
10732 KB |
Output is correct |
44 |
Correct |
10 ms |
10860 KB |
Output is correct |
45 |
Correct |
9 ms |
10604 KB |
Output is correct |
46 |
Correct |
9 ms |
10604 KB |
Output is correct |
47 |
Correct |
10 ms |
10752 KB |
Output is correct |
48 |
Correct |
10 ms |
10732 KB |
Output is correct |
49 |
Correct |
16 ms |
11244 KB |
Output is correct |
50 |
Correct |
113 ms |
18156 KB |
Output is correct |
51 |
Correct |
14 ms |
10988 KB |
Output is correct |
52 |
Correct |
105 ms |
17132 KB |
Output is correct |
53 |
Correct |
20 ms |
11244 KB |
Output is correct |
54 |
Correct |
109 ms |
17644 KB |
Output is correct |
55 |
Correct |
12 ms |
10988 KB |
Output is correct |
56 |
Correct |
16 ms |
10988 KB |
Output is correct |
57 |
Correct |
12 ms |
10988 KB |
Output is correct |
58 |
Correct |
12 ms |
10988 KB |
Output is correct |
59 |
Correct |
9 ms |
10604 KB |
Output is correct |
60 |
Correct |
103 ms |
15468 KB |
Output is correct |
61 |
Correct |
11 ms |
10860 KB |
Output is correct |
62 |
Correct |
115 ms |
17900 KB |
Output is correct |
63 |
Correct |
447 ms |
44368 KB |
Output is correct |
64 |
Correct |
437 ms |
44268 KB |
Output is correct |
65 |
Correct |
435 ms |
44272 KB |
Output is correct |
66 |
Correct |
104 ms |
14956 KB |
Output is correct |
67 |
Correct |
230 ms |
18924 KB |
Output is correct |
68 |
Correct |
94 ms |
14956 KB |
Output is correct |
69 |
Correct |
275 ms |
19692 KB |
Output is correct |
70 |
Correct |
110 ms |
14956 KB |
Output is correct |
71 |
Correct |
121 ms |
14956 KB |
Output is correct |
72 |
Correct |
237 ms |
19052 KB |
Output is correct |
73 |
Correct |
232 ms |
19052 KB |
Output is correct |
74 |
Correct |
714 ms |
28268 KB |
Output is correct |
75 |
Correct |
422 ms |
24428 KB |
Output is correct |
76 |
Correct |
545 ms |
27144 KB |
Output is correct |
77 |
Correct |
557 ms |
27500 KB |
Output is correct |
78 |
Correct |
178 ms |
18480 KB |
Output is correct |
79 |
Correct |
308 ms |
22252 KB |
Output is correct |
80 |
Correct |
183 ms |
18540 KB |
Output is correct |
81 |
Correct |
267 ms |
21740 KB |
Output is correct |
82 |
Correct |
548 ms |
33004 KB |
Output is correct |
83 |
Correct |
500 ms |
32876 KB |
Output is correct |
84 |
Correct |
488 ms |
33560 KB |
Output is correct |
85 |
Correct |
468 ms |
33260 KB |
Output is correct |
86 |
Correct |
116 ms |
14188 KB |
Output is correct |
87 |
Correct |
149 ms |
16364 KB |
Output is correct |
88 |
Correct |
258 ms |
19180 KB |
Output is correct |
89 |
Correct |
539 ms |
26732 KB |
Output is correct |