#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,t,tes,msh[100009],C,D;
long long pas;
vector <int> V[100009];
set <int> in[100009],out[100009],sm[100009];//sm-c ideashi in-ebia ogond komponentis magivrad wveros nomers agdeb
set <int>::iterator it,tt;
bool check(int q, int w){
if(out[w].find(q)==out[w].end()) return 0; else return 1;
}
void mrg(int Q, int W){
int q=msh[Q],w=msh[W];
if(q==w) return;
if(V[q].size()+in[q].size()+out[q].size()+sm[q].size()<V[w].size()+in[w].size()+out[w].size()+sm[w].size()) swap(q,w);
long long qw=V[q].size(),we=V[w].size(),Sq=sm[q].size(),Sw=sm[w].size();
pas-=qw*(qw-1);
pas-=we*(we-1);
pas-=qw*Sq;
pas-=we*Sw;
//
vector <pair <int, int> > NXT;
for(vector <int>::iterator IT=V[w].begin(); IT!=V[w].end(); IT++){
V[q].push_back((*IT));
tt=sm[q].find((*IT));
if(tt!=sm[q].end()){
sm[q].erase(tt);
}
msh[(*IT)]=q;
}
V[w].clear();
for(it=sm[w].begin(); it!=sm[w].end(); it++){
if(msh[(*it)]==q) continue;
sm[q].insert((*it));
}
Sq=sm[q].size();
pas+=(qw+we)*Sq;
//
for(it=in[w].begin(); it!=in[w].end(); it++){
out[(*it)].erase(out[(*it)].find(w));
if((*it)==q) continue;
out[(*it)].insert(q);
in[q].insert((*it));
if(check((*it),q)) NXT.push_back({(*it),q});
}
for(it=out[w].begin(); it!=out[w].end(); it++){
in[(*it)].erase(in[(*it)].find(w));
if((*it)==q) continue;
in[(*it)].insert(q);
out[q].insert((*it));
if(check(q,(*it))) NXT.push_back({(*it),q});
}
//
in[w].clear();out[w].clear();sm[w].clear();
qw=V[q].size();
pas+=qw*(qw-1);
while(NXT.size()){
mrg(NXT.back().first,NXT.back().second);
NXT.pop_back();
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>tes;
for(i=1; i<=a; i++){
msh[i]=i;V[i].push_back(i);
}
for(t=1; t<=tes; t++){
cin>>C>>D;
c=msh[C],d=msh[D];
if(c==d){
cout<<pas<<"\n";
continue;
}
if(check(c,d)==0){
if(sm[d].find(C)!=sm[d].end()){
cout<<pas<<"\n";
continue;
}
pas+=V[d].size();
in[d].insert(c);out[c].insert(d);
sm[d].insert(C);
cout<<pas<<"\n";
continue;
}
in[d].insert(c);out[c].insert(d);
sm[d].insert(C);
pas+=V[d].size();
mrg(c,d);
cout<<pas<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16768 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16768 KB |
Output is correct |
5 |
Correct |
8 ms |
16776 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16780 KB |
Output is correct |
8 |
Correct |
8 ms |
16776 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16712 KB |
Output is correct |
12 |
Correct |
8 ms |
16764 KB |
Output is correct |
13 |
Correct |
9 ms |
16764 KB |
Output is correct |
14 |
Correct |
8 ms |
16672 KB |
Output is correct |
15 |
Correct |
10 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16728 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16852 KB |
Output is correct |
19 |
Correct |
9 ms |
16752 KB |
Output is correct |
20 |
Correct |
8 ms |
16728 KB |
Output is correct |
21 |
Correct |
11 ms |
16736 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16776 KB |
Output is correct |
25 |
Correct |
9 ms |
16744 KB |
Output is correct |
26 |
Correct |
8 ms |
16768 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16780 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
8 ms |
16732 KB |
Output is correct |
32 |
Correct |
9 ms |
16760 KB |
Output is correct |
33 |
Correct |
8 ms |
16772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16768 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16768 KB |
Output is correct |
5 |
Correct |
8 ms |
16776 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16780 KB |
Output is correct |
8 |
Correct |
8 ms |
16776 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16712 KB |
Output is correct |
12 |
Correct |
8 ms |
16764 KB |
Output is correct |
13 |
Correct |
9 ms |
16764 KB |
Output is correct |
14 |
Correct |
8 ms |
16672 KB |
Output is correct |
15 |
Correct |
10 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16728 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16852 KB |
Output is correct |
19 |
Correct |
9 ms |
16752 KB |
Output is correct |
20 |
Correct |
8 ms |
16728 KB |
Output is correct |
21 |
Correct |
11 ms |
16736 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16776 KB |
Output is correct |
25 |
Correct |
9 ms |
16744 KB |
Output is correct |
26 |
Correct |
8 ms |
16768 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16780 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
8 ms |
16732 KB |
Output is correct |
32 |
Correct |
9 ms |
16760 KB |
Output is correct |
33 |
Correct |
8 ms |
16772 KB |
Output is correct |
34 |
Correct |
11 ms |
16908 KB |
Output is correct |
35 |
Correct |
86 ms |
22548 KB |
Output is correct |
36 |
Correct |
109 ms |
25388 KB |
Output is correct |
37 |
Correct |
121 ms |
25548 KB |
Output is correct |
38 |
Correct |
98 ms |
25124 KB |
Output is correct |
39 |
Correct |
10 ms |
16852 KB |
Output is correct |
40 |
Correct |
11 ms |
16980 KB |
Output is correct |
41 |
Correct |
11 ms |
16980 KB |
Output is correct |
42 |
Correct |
11 ms |
16852 KB |
Output is correct |
43 |
Correct |
12 ms |
16980 KB |
Output is correct |
44 |
Correct |
11 ms |
17036 KB |
Output is correct |
45 |
Correct |
10 ms |
16852 KB |
Output is correct |
46 |
Correct |
9 ms |
16852 KB |
Output is correct |
47 |
Correct |
10 ms |
16980 KB |
Output is correct |
48 |
Correct |
12 ms |
17032 KB |
Output is correct |
49 |
Correct |
21 ms |
17804 KB |
Output is correct |
50 |
Correct |
106 ms |
25572 KB |
Output is correct |
51 |
Correct |
17 ms |
17292 KB |
Output is correct |
52 |
Correct |
84 ms |
23908 KB |
Output is correct |
53 |
Correct |
16 ms |
17744 KB |
Output is correct |
54 |
Correct |
94 ms |
24708 KB |
Output is correct |
55 |
Correct |
12 ms |
17364 KB |
Output is correct |
56 |
Correct |
13 ms |
17364 KB |
Output is correct |
57 |
Correct |
13 ms |
17844 KB |
Output is correct |
58 |
Correct |
14 ms |
17796 KB |
Output is correct |
59 |
Correct |
10 ms |
16980 KB |
Output is correct |
60 |
Correct |
74 ms |
21904 KB |
Output is correct |
61 |
Correct |
12 ms |
17108 KB |
Output is correct |
62 |
Correct |
104 ms |
25000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16768 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16768 KB |
Output is correct |
5 |
Correct |
8 ms |
16776 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16780 KB |
Output is correct |
8 |
Correct |
8 ms |
16776 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16712 KB |
Output is correct |
12 |
Correct |
8 ms |
16764 KB |
Output is correct |
13 |
Correct |
9 ms |
16764 KB |
Output is correct |
14 |
Correct |
8 ms |
16672 KB |
Output is correct |
15 |
Correct |
10 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16728 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16852 KB |
Output is correct |
19 |
Correct |
9 ms |
16752 KB |
Output is correct |
20 |
Correct |
8 ms |
16728 KB |
Output is correct |
21 |
Correct |
11 ms |
16736 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16776 KB |
Output is correct |
25 |
Correct |
9 ms |
16744 KB |
Output is correct |
26 |
Correct |
8 ms |
16768 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16780 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
8 ms |
16732 KB |
Output is correct |
32 |
Correct |
9 ms |
16760 KB |
Output is correct |
33 |
Correct |
8 ms |
16772 KB |
Output is correct |
34 |
Correct |
11 ms |
16908 KB |
Output is correct |
35 |
Correct |
86 ms |
22548 KB |
Output is correct |
36 |
Correct |
109 ms |
25388 KB |
Output is correct |
37 |
Correct |
121 ms |
25548 KB |
Output is correct |
38 |
Correct |
98 ms |
25124 KB |
Output is correct |
39 |
Correct |
10 ms |
16852 KB |
Output is correct |
40 |
Correct |
11 ms |
16980 KB |
Output is correct |
41 |
Correct |
11 ms |
16980 KB |
Output is correct |
42 |
Correct |
11 ms |
16852 KB |
Output is correct |
43 |
Correct |
12 ms |
16980 KB |
Output is correct |
44 |
Correct |
11 ms |
17036 KB |
Output is correct |
45 |
Correct |
10 ms |
16852 KB |
Output is correct |
46 |
Correct |
9 ms |
16852 KB |
Output is correct |
47 |
Correct |
10 ms |
16980 KB |
Output is correct |
48 |
Correct |
12 ms |
17032 KB |
Output is correct |
49 |
Correct |
21 ms |
17804 KB |
Output is correct |
50 |
Correct |
106 ms |
25572 KB |
Output is correct |
51 |
Correct |
17 ms |
17292 KB |
Output is correct |
52 |
Correct |
84 ms |
23908 KB |
Output is correct |
53 |
Correct |
16 ms |
17744 KB |
Output is correct |
54 |
Correct |
94 ms |
24708 KB |
Output is correct |
55 |
Correct |
12 ms |
17364 KB |
Output is correct |
56 |
Correct |
13 ms |
17364 KB |
Output is correct |
57 |
Correct |
13 ms |
17844 KB |
Output is correct |
58 |
Correct |
14 ms |
17796 KB |
Output is correct |
59 |
Correct |
10 ms |
16980 KB |
Output is correct |
60 |
Correct |
74 ms |
21904 KB |
Output is correct |
61 |
Correct |
12 ms |
17108 KB |
Output is correct |
62 |
Correct |
104 ms |
25000 KB |
Output is correct |
63 |
Correct |
413 ms |
67988 KB |
Output is correct |
64 |
Correct |
445 ms |
67744 KB |
Output is correct |
65 |
Correct |
450 ms |
67764 KB |
Output is correct |
66 |
Correct |
117 ms |
25252 KB |
Output is correct |
67 |
Correct |
248 ms |
32928 KB |
Output is correct |
68 |
Correct |
116 ms |
25320 KB |
Output is correct |
69 |
Correct |
342 ms |
32048 KB |
Output is correct |
70 |
Correct |
119 ms |
25204 KB |
Output is correct |
71 |
Correct |
116 ms |
25200 KB |
Output is correct |
72 |
Correct |
324 ms |
31944 KB |
Output is correct |
73 |
Correct |
286 ms |
32192 KB |
Output is correct |
74 |
Correct |
786 ms |
44348 KB |
Output is correct |
75 |
Correct |
453 ms |
37580 KB |
Output is correct |
76 |
Correct |
573 ms |
43084 KB |
Output is correct |
77 |
Correct |
601 ms |
43148 KB |
Output is correct |
78 |
Correct |
206 ms |
30716 KB |
Output is correct |
79 |
Correct |
260 ms |
33912 KB |
Output is correct |
80 |
Correct |
185 ms |
30848 KB |
Output is correct |
81 |
Correct |
285 ms |
33908 KB |
Output is correct |
82 |
Correct |
576 ms |
52824 KB |
Output is correct |
83 |
Correct |
627 ms |
52756 KB |
Output is correct |
84 |
Correct |
581 ms |
71324 KB |
Output is correct |
85 |
Correct |
563 ms |
71336 KB |
Output is correct |
86 |
Correct |
161 ms |
29684 KB |
Output is correct |
87 |
Correct |
166 ms |
31564 KB |
Output is correct |
88 |
Correct |
316 ms |
32140 KB |
Output is correct |
89 |
Correct |
582 ms |
42048 KB |
Output is correct |