#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> G[100100];
ll ans;
queue<pair<int, int>> mergeQ;
struct DSU{
ll path[100100], sz[100100];
set<int> st[100100], elem[100100];
void init(int n){
for (int i=1;i<=n;i++){
path[i] = i;
st[i].clear();
elem[i].clear();
elem[i].insert(i);
sz[i] = 1;
}
}
int find(int s){
if (s==path[s]) return s;
return path[s] = find(path[s]);
}
void merge(int x, int y){
// printf(" merge %d %d\n", x, y);
x = find(x), y = find(y);
if (x==y) return;
if (st[x].size() + elem[x].size() + G[x].size() < st[y].size() + elem[y].size() + G[y].size()) swap(x, y);
ans -= (ll)st[x].size() * sz[x];
ans -= (ll)st[y].size() * sz[y];
ans -= sz[x] * (sz[x]-1);
ans -= sz[y] * (sz[y]-1);
// printf(" delete -> %lld\n", ans);
for (auto p:st[y]) if (find(p)!=x){
st[x].insert(p);
G[find(p)].insert(x);
if (G[x].find(find(p))!=G[x].end()) mergeQ.emplace(x, find(p));
}
for (auto p:elem[y]){
auto iter = st[x].find(p);
if (iter!=st[x].end()) st[x].erase(iter);
elem[x].insert(p);
}
for (auto p:G[y]){
int fp = find(p);
if (G[fp].find(x)!=G[fp].end()) mergeQ.emplace(x, find(p));
G[x].insert(find(p));
}
sz[x] += sz[y];
ans += (ll)st[x].size() * sz[x];
ans += sz[x] * (sz[x]-1);
// printf(" add -> %lld (%d %lld)\n", ans, (int)st[x].size(), sz[x]);
path[y] = x;
}
void insert(int x, int y){
y = find(y);
if (st[y].find(x)!=st[y].end()) return;
ans += sz[y];
st[y].insert(x);
G[find(x)].insert(y);
}
}dsu;
void update(int x, int y){
int cx = dsu.find(x), cy = dsu.find(y);
if (cx==cy) return;
if (G[cy].find(cx)!=G[cy].end()){
mergeQ.emplace(cx, cy);
while(!mergeQ.empty()){
auto [p, q] = mergeQ.front(); mergeQ.pop();
dsu.merge(p, q);
}
}
else dsu.insert(x, cy);
}
int adj[55][55];
int naive(int n, int x, int y){
adj[x][y] = 1;
while(true){
int flag = 0;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
for (int k=1;k<=n;k++) if (i!=j && j!=k && i!=k){
if (flag) break;
if (adj[i][j] && adj[j][k] && adj[k][j] && !adj[i][k]){
adj[i][k] = 1;
flag = 1;
}
}
if (flag) break;
}
if (flag) break;
}
if (!flag) break;
}
int ret = 0;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++) ret += adj[i][j];
}
return ret;
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
dsu.init(n);
for (int i=1;i<=m;i++){
int x, y;
scanf("%d %d", &x, &y);
update(x, y);
// int tmp = naive(n, x, y);
// printf("%lld (%d)\n", ans, tmp);
// assert(tmp==ans);
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:133:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14300 KB |
Output is correct |
4 |
Correct |
7 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14344 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14316 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14308 KB |
Output is correct |
16 |
Correct |
7 ms |
14340 KB |
Output is correct |
17 |
Correct |
9 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14468 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14420 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14400 KB |
Output is correct |
26 |
Correct |
8 ms |
14408 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
10 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14372 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
6 ms |
14420 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14300 KB |
Output is correct |
4 |
Correct |
7 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14344 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14316 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14308 KB |
Output is correct |
16 |
Correct |
7 ms |
14340 KB |
Output is correct |
17 |
Correct |
9 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14468 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14420 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14400 KB |
Output is correct |
26 |
Correct |
8 ms |
14408 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
10 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14372 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
6 ms |
14420 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
79 ms |
19960 KB |
Output is correct |
36 |
Correct |
112 ms |
22248 KB |
Output is correct |
37 |
Correct |
117 ms |
22328 KB |
Output is correct |
38 |
Correct |
98 ms |
22044 KB |
Output is correct |
39 |
Correct |
8 ms |
14804 KB |
Output is correct |
40 |
Correct |
10 ms |
15188 KB |
Output is correct |
41 |
Correct |
10 ms |
15160 KB |
Output is correct |
42 |
Correct |
9 ms |
14804 KB |
Output is correct |
43 |
Correct |
9 ms |
14804 KB |
Output is correct |
44 |
Correct |
11 ms |
14844 KB |
Output is correct |
45 |
Correct |
8 ms |
14892 KB |
Output is correct |
46 |
Correct |
10 ms |
14932 KB |
Output is correct |
47 |
Correct |
10 ms |
15060 KB |
Output is correct |
48 |
Correct |
11 ms |
15064 KB |
Output is correct |
49 |
Correct |
17 ms |
15632 KB |
Output is correct |
50 |
Correct |
96 ms |
22420 KB |
Output is correct |
51 |
Correct |
11 ms |
15188 KB |
Output is correct |
52 |
Correct |
96 ms |
21036 KB |
Output is correct |
53 |
Correct |
14 ms |
15572 KB |
Output is correct |
54 |
Correct |
119 ms |
21700 KB |
Output is correct |
55 |
Correct |
11 ms |
15228 KB |
Output is correct |
56 |
Correct |
10 ms |
15156 KB |
Output is correct |
57 |
Correct |
10 ms |
15188 KB |
Output is correct |
58 |
Correct |
13 ms |
15188 KB |
Output is correct |
59 |
Correct |
9 ms |
15188 KB |
Output is correct |
60 |
Correct |
77 ms |
20132 KB |
Output is correct |
61 |
Correct |
10 ms |
15060 KB |
Output is correct |
62 |
Correct |
93 ms |
21828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14300 KB |
Output is correct |
4 |
Correct |
7 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14344 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14316 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14308 KB |
Output is correct |
16 |
Correct |
7 ms |
14340 KB |
Output is correct |
17 |
Correct |
9 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14468 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14420 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14400 KB |
Output is correct |
26 |
Correct |
8 ms |
14408 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
10 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14372 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
6 ms |
14420 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
79 ms |
19960 KB |
Output is correct |
36 |
Correct |
112 ms |
22248 KB |
Output is correct |
37 |
Correct |
117 ms |
22328 KB |
Output is correct |
38 |
Correct |
98 ms |
22044 KB |
Output is correct |
39 |
Correct |
8 ms |
14804 KB |
Output is correct |
40 |
Correct |
10 ms |
15188 KB |
Output is correct |
41 |
Correct |
10 ms |
15160 KB |
Output is correct |
42 |
Correct |
9 ms |
14804 KB |
Output is correct |
43 |
Correct |
9 ms |
14804 KB |
Output is correct |
44 |
Correct |
11 ms |
14844 KB |
Output is correct |
45 |
Correct |
8 ms |
14892 KB |
Output is correct |
46 |
Correct |
10 ms |
14932 KB |
Output is correct |
47 |
Correct |
10 ms |
15060 KB |
Output is correct |
48 |
Correct |
11 ms |
15064 KB |
Output is correct |
49 |
Correct |
17 ms |
15632 KB |
Output is correct |
50 |
Correct |
96 ms |
22420 KB |
Output is correct |
51 |
Correct |
11 ms |
15188 KB |
Output is correct |
52 |
Correct |
96 ms |
21036 KB |
Output is correct |
53 |
Correct |
14 ms |
15572 KB |
Output is correct |
54 |
Correct |
119 ms |
21700 KB |
Output is correct |
55 |
Correct |
11 ms |
15228 KB |
Output is correct |
56 |
Correct |
10 ms |
15156 KB |
Output is correct |
57 |
Correct |
10 ms |
15188 KB |
Output is correct |
58 |
Correct |
13 ms |
15188 KB |
Output is correct |
59 |
Correct |
9 ms |
15188 KB |
Output is correct |
60 |
Correct |
77 ms |
20132 KB |
Output is correct |
61 |
Correct |
10 ms |
15060 KB |
Output is correct |
62 |
Correct |
93 ms |
21828 KB |
Output is correct |
63 |
Correct |
353 ms |
54172 KB |
Output is correct |
64 |
Correct |
335 ms |
54356 KB |
Output is correct |
65 |
Correct |
321 ms |
54228 KB |
Output is correct |
66 |
Correct |
210 ms |
39228 KB |
Output is correct |
67 |
Correct |
365 ms |
65732 KB |
Output is correct |
68 |
Correct |
211 ms |
39308 KB |
Output is correct |
69 |
Correct |
253 ms |
36676 KB |
Output is correct |
70 |
Correct |
232 ms |
39252 KB |
Output is correct |
71 |
Correct |
217 ms |
39176 KB |
Output is correct |
72 |
Correct |
351 ms |
49296 KB |
Output is correct |
73 |
Correct |
321 ms |
51064 KB |
Output is correct |
74 |
Correct |
691 ms |
73268 KB |
Output is correct |
75 |
Correct |
381 ms |
44240 KB |
Output is correct |
76 |
Correct |
511 ms |
58088 KB |
Output is correct |
77 |
Correct |
504 ms |
58292 KB |
Output is correct |
78 |
Correct |
157 ms |
38044 KB |
Output is correct |
79 |
Correct |
326 ms |
45952 KB |
Output is correct |
80 |
Correct |
183 ms |
36796 KB |
Output is correct |
81 |
Correct |
302 ms |
41772 KB |
Output is correct |
82 |
Correct |
528 ms |
57108 KB |
Output is correct |
83 |
Correct |
515 ms |
57248 KB |
Output is correct |
84 |
Correct |
438 ms |
57044 KB |
Output is correct |
85 |
Correct |
411 ms |
57128 KB |
Output is correct |
86 |
Correct |
275 ms |
71956 KB |
Output is correct |
87 |
Correct |
330 ms |
74100 KB |
Output is correct |
88 |
Correct |
311 ms |
50268 KB |
Output is correct |
89 |
Correct |
505 ms |
56784 KB |
Output is correct |