#include<bits/stdc++.h>
#define int long long
using namespace std;
int par[100100],sz[100100],ans;
set<int>adj[100100],radj[100100],st[100100];
queue<pair<int,int>> ops;
int abp(int a){
return par[a]?par[a]=abp(par[a]):a;
}
void weak(int a,int b){
adj[a].insert(b);
radj[b].insert(a);
if(adj[b].count(a))
ops.push({a,b});
}
inline int ssz(int x){
return st[x].size()+radj[x].size()+adj[x].size();
}
void merge(int a,int b){
if(a==b)return;
ans+=sz[a]*st[b].size()+sz[b]*st[a].size();
if(ssz(a)<ssz(b))swap(a,b);
par[b]=a;sz[a]+=sz[b];
for(auto i:st[b])
if(st[a].count(i))
ans-=sz[a];
else st[a].insert(i);
adj[a].erase(b);
adj[b].erase(a);
radj[a].erase(b);
radj[b].erase(a);
for(auto i:adj[b])
radj[i].erase(b),
weak(a,i);
for(auto i:radj[b])
adj[i].erase(b),
weak(i,a);
}
void dostuf(int a,int b){
b=abp(b);
if(abp(a)!=b&&!st[b].count(a)){
st[b].insert(a);
ans+=sz[b];
a=abp(a);
weak(a,b);
while(ops.size()){
auto[a,b]=ops.front();
ops.pop();
merge(abp(a),abp(b));
}
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
st[i].insert(i),sz[i]=1;
while(m--){
int a,b;
cin>>a>>b;
dostuf(a,b);
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
14928 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14928 KB |
Output is correct |
13 |
Correct |
3 ms |
14968 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
3 ms |
15096 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
4 ms |
14928 KB |
Output is correct |
19 |
Correct |
3 ms |
14928 KB |
Output is correct |
20 |
Correct |
3 ms |
14928 KB |
Output is correct |
21 |
Correct |
4 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14960 KB |
Output is correct |
24 |
Correct |
4 ms |
14928 KB |
Output is correct |
25 |
Correct |
4 ms |
14928 KB |
Output is correct |
26 |
Correct |
6 ms |
14928 KB |
Output is correct |
27 |
Correct |
4 ms |
15100 KB |
Output is correct |
28 |
Correct |
3 ms |
14928 KB |
Output is correct |
29 |
Correct |
3 ms |
14928 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14928 KB |
Output is correct |
33 |
Correct |
4 ms |
15100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
14928 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14928 KB |
Output is correct |
13 |
Correct |
3 ms |
14968 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
3 ms |
15096 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
4 ms |
14928 KB |
Output is correct |
19 |
Correct |
3 ms |
14928 KB |
Output is correct |
20 |
Correct |
3 ms |
14928 KB |
Output is correct |
21 |
Correct |
4 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14960 KB |
Output is correct |
24 |
Correct |
4 ms |
14928 KB |
Output is correct |
25 |
Correct |
4 ms |
14928 KB |
Output is correct |
26 |
Correct |
6 ms |
14928 KB |
Output is correct |
27 |
Correct |
4 ms |
15100 KB |
Output is correct |
28 |
Correct |
3 ms |
14928 KB |
Output is correct |
29 |
Correct |
3 ms |
14928 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14928 KB |
Output is correct |
33 |
Correct |
4 ms |
15100 KB |
Output is correct |
34 |
Correct |
5 ms |
14928 KB |
Output is correct |
35 |
Correct |
56 ms |
18612 KB |
Output is correct |
36 |
Correct |
96 ms |
20932 KB |
Output is correct |
37 |
Correct |
77 ms |
21064 KB |
Output is correct |
38 |
Correct |
71 ms |
20656 KB |
Output is correct |
39 |
Correct |
5 ms |
15184 KB |
Output is correct |
40 |
Correct |
6 ms |
15480 KB |
Output is correct |
41 |
Correct |
6 ms |
15440 KB |
Output is correct |
42 |
Correct |
4 ms |
15184 KB |
Output is correct |
43 |
Correct |
6 ms |
15184 KB |
Output is correct |
44 |
Correct |
6 ms |
15172 KB |
Output is correct |
45 |
Correct |
4 ms |
15288 KB |
Output is correct |
46 |
Correct |
5 ms |
15184 KB |
Output is correct |
47 |
Correct |
7 ms |
15408 KB |
Output is correct |
48 |
Correct |
6 ms |
15440 KB |
Output is correct |
49 |
Correct |
11 ms |
16376 KB |
Output is correct |
50 |
Correct |
78 ms |
21252 KB |
Output is correct |
51 |
Correct |
8 ms |
15440 KB |
Output is correct |
52 |
Correct |
61 ms |
19528 KB |
Output is correct |
53 |
Correct |
10 ms |
15952 KB |
Output is correct |
54 |
Correct |
70 ms |
20264 KB |
Output is correct |
55 |
Correct |
8 ms |
15696 KB |
Output is correct |
56 |
Correct |
9 ms |
15696 KB |
Output is correct |
57 |
Correct |
7 ms |
15696 KB |
Output is correct |
58 |
Correct |
7 ms |
15696 KB |
Output is correct |
59 |
Correct |
6 ms |
15696 KB |
Output is correct |
60 |
Correct |
53 ms |
17992 KB |
Output is correct |
61 |
Correct |
6 ms |
15612 KB |
Output is correct |
62 |
Correct |
74 ms |
20644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
14928 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
3 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14928 KB |
Output is correct |
13 |
Correct |
3 ms |
14968 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
3 ms |
15096 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
4 ms |
14928 KB |
Output is correct |
19 |
Correct |
3 ms |
14928 KB |
Output is correct |
20 |
Correct |
3 ms |
14928 KB |
Output is correct |
21 |
Correct |
4 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14960 KB |
Output is correct |
24 |
Correct |
4 ms |
14928 KB |
Output is correct |
25 |
Correct |
4 ms |
14928 KB |
Output is correct |
26 |
Correct |
6 ms |
14928 KB |
Output is correct |
27 |
Correct |
4 ms |
15100 KB |
Output is correct |
28 |
Correct |
3 ms |
14928 KB |
Output is correct |
29 |
Correct |
3 ms |
14928 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14928 KB |
Output is correct |
33 |
Correct |
4 ms |
15100 KB |
Output is correct |
34 |
Correct |
5 ms |
14928 KB |
Output is correct |
35 |
Correct |
56 ms |
18612 KB |
Output is correct |
36 |
Correct |
96 ms |
20932 KB |
Output is correct |
37 |
Correct |
77 ms |
21064 KB |
Output is correct |
38 |
Correct |
71 ms |
20656 KB |
Output is correct |
39 |
Correct |
5 ms |
15184 KB |
Output is correct |
40 |
Correct |
6 ms |
15480 KB |
Output is correct |
41 |
Correct |
6 ms |
15440 KB |
Output is correct |
42 |
Correct |
4 ms |
15184 KB |
Output is correct |
43 |
Correct |
6 ms |
15184 KB |
Output is correct |
44 |
Correct |
6 ms |
15172 KB |
Output is correct |
45 |
Correct |
4 ms |
15288 KB |
Output is correct |
46 |
Correct |
5 ms |
15184 KB |
Output is correct |
47 |
Correct |
7 ms |
15408 KB |
Output is correct |
48 |
Correct |
6 ms |
15440 KB |
Output is correct |
49 |
Correct |
11 ms |
16376 KB |
Output is correct |
50 |
Correct |
78 ms |
21252 KB |
Output is correct |
51 |
Correct |
8 ms |
15440 KB |
Output is correct |
52 |
Correct |
61 ms |
19528 KB |
Output is correct |
53 |
Correct |
10 ms |
15952 KB |
Output is correct |
54 |
Correct |
70 ms |
20264 KB |
Output is correct |
55 |
Correct |
8 ms |
15696 KB |
Output is correct |
56 |
Correct |
9 ms |
15696 KB |
Output is correct |
57 |
Correct |
7 ms |
15696 KB |
Output is correct |
58 |
Correct |
7 ms |
15696 KB |
Output is correct |
59 |
Correct |
6 ms |
15696 KB |
Output is correct |
60 |
Correct |
53 ms |
17992 KB |
Output is correct |
61 |
Correct |
6 ms |
15612 KB |
Output is correct |
62 |
Correct |
74 ms |
20644 KB |
Output is correct |
63 |
Correct |
462 ms |
64068 KB |
Output is correct |
64 |
Correct |
432 ms |
64184 KB |
Output is correct |
65 |
Correct |
401 ms |
64328 KB |
Output is correct |
66 |
Correct |
157 ms |
32176 KB |
Output is correct |
67 |
Correct |
277 ms |
61512 KB |
Output is correct |
68 |
Correct |
157 ms |
34632 KB |
Output is correct |
69 |
Correct |
273 ms |
35656 KB |
Output is correct |
70 |
Correct |
186 ms |
34524 KB |
Output is correct |
71 |
Correct |
182 ms |
34632 KB |
Output is correct |
72 |
Correct |
325 ms |
45572 KB |
Output is correct |
73 |
Correct |
319 ms |
47176 KB |
Output is correct |
74 |
Correct |
734 ms |
69604 KB |
Output is correct |
75 |
Correct |
409 ms |
42488 KB |
Output is correct |
76 |
Correct |
560 ms |
54536 KB |
Output is correct |
77 |
Correct |
526 ms |
54648 KB |
Output is correct |
78 |
Correct |
174 ms |
38216 KB |
Output is correct |
79 |
Correct |
292 ms |
42568 KB |
Output is correct |
80 |
Correct |
157 ms |
36936 KB |
Output is correct |
81 |
Correct |
286 ms |
40780 KB |
Output is correct |
82 |
Correct |
652 ms |
55112 KB |
Output is correct |
83 |
Correct |
611 ms |
55112 KB |
Output is correct |
84 |
Correct |
479 ms |
53700 KB |
Output is correct |
85 |
Correct |
423 ms |
53832 KB |
Output is correct |
86 |
Correct |
236 ms |
67420 KB |
Output is correct |
87 |
Correct |
305 ms |
69628 KB |
Output is correct |
88 |
Correct |
347 ms |
46352 KB |
Output is correct |
89 |
Correct |
591 ms |
52356 KB |
Output is correct |