#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,t,tes,msh[100009],pas,C,D;
vector <long long> V[100009];
set <long long> in[100009],out[100009],sm[100009];//sm-c ideashi in-ebia ogond komponentis magivrad wveros nomers agdeb
set <long long>::iterator it,tt;
bool check(long long q, long long w){
if(out[w].find(q)==out[w].end()) return 0; else return 1;
}
void mrg(long long Q, long long W){
long long 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 <long long, long long> > NXT;
for(vector <long long>::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;
if(sm[q].find((*it))==sm[q].end()){
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();
pas+=V[q].size()*(V[q].size()-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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16772 KB |
Output is correct |
5 |
Correct |
9 ms |
16748 KB |
Output is correct |
6 |
Correct |
9 ms |
16680 KB |
Output is correct |
7 |
Correct |
10 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16824 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16772 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16768 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16772 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
10 ms |
16776 KB |
Output is correct |
21 |
Correct |
10 ms |
16768 KB |
Output is correct |
22 |
Correct |
11 ms |
16736 KB |
Output is correct |
23 |
Correct |
9 ms |
16780 KB |
Output is correct |
24 |
Correct |
9 ms |
16772 KB |
Output is correct |
25 |
Correct |
9 ms |
16740 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16776 KB |
Output is correct |
29 |
Correct |
9 ms |
16768 KB |
Output is correct |
30 |
Correct |
9 ms |
16776 KB |
Output is correct |
31 |
Correct |
9 ms |
16780 KB |
Output is correct |
32 |
Correct |
8 ms |
16752 KB |
Output is correct |
33 |
Correct |
10 ms |
16776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16772 KB |
Output is correct |
5 |
Correct |
9 ms |
16748 KB |
Output is correct |
6 |
Correct |
9 ms |
16680 KB |
Output is correct |
7 |
Correct |
10 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16824 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16772 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16768 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16772 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
10 ms |
16776 KB |
Output is correct |
21 |
Correct |
10 ms |
16768 KB |
Output is correct |
22 |
Correct |
11 ms |
16736 KB |
Output is correct |
23 |
Correct |
9 ms |
16780 KB |
Output is correct |
24 |
Correct |
9 ms |
16772 KB |
Output is correct |
25 |
Correct |
9 ms |
16740 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16776 KB |
Output is correct |
29 |
Correct |
9 ms |
16768 KB |
Output is correct |
30 |
Correct |
9 ms |
16776 KB |
Output is correct |
31 |
Correct |
9 ms |
16780 KB |
Output is correct |
32 |
Correct |
8 ms |
16752 KB |
Output is correct |
33 |
Correct |
10 ms |
16776 KB |
Output is correct |
34 |
Correct |
13 ms |
16852 KB |
Output is correct |
35 |
Correct |
84 ms |
20672 KB |
Output is correct |
36 |
Correct |
102 ms |
23072 KB |
Output is correct |
37 |
Correct |
109 ms |
23160 KB |
Output is correct |
38 |
Correct |
93 ms |
22860 KB |
Output is correct |
39 |
Correct |
10 ms |
16852 KB |
Output is correct |
40 |
Correct |
12 ms |
17108 KB |
Output is correct |
41 |
Correct |
11 ms |
17108 KB |
Output is correct |
42 |
Correct |
10 ms |
16852 KB |
Output is correct |
43 |
Correct |
12 ms |
17044 KB |
Output is correct |
44 |
Correct |
13 ms |
17044 KB |
Output is correct |
45 |
Correct |
10 ms |
16852 KB |
Output is correct |
46 |
Correct |
11 ms |
16888 KB |
Output is correct |
47 |
Correct |
11 ms |
17108 KB |
Output is correct |
48 |
Correct |
13 ms |
17036 KB |
Output is correct |
49 |
Correct |
18 ms |
17876 KB |
Output is correct |
50 |
Correct |
113 ms |
23384 KB |
Output is correct |
51 |
Correct |
14 ms |
17292 KB |
Output is correct |
52 |
Correct |
86 ms |
21588 KB |
Output is correct |
53 |
Correct |
17 ms |
17716 KB |
Output is correct |
54 |
Correct |
94 ms |
22560 KB |
Output is correct |
55 |
Correct |
14 ms |
17492 KB |
Output is correct |
56 |
Correct |
14 ms |
17428 KB |
Output is correct |
57 |
Correct |
15 ms |
17812 KB |
Output is correct |
58 |
Correct |
13 ms |
17808 KB |
Output is correct |
59 |
Correct |
11 ms |
16980 KB |
Output is correct |
60 |
Correct |
69 ms |
19436 KB |
Output is correct |
61 |
Correct |
12 ms |
17108 KB |
Output is correct |
62 |
Correct |
93 ms |
22680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16772 KB |
Output is correct |
5 |
Correct |
9 ms |
16748 KB |
Output is correct |
6 |
Correct |
9 ms |
16680 KB |
Output is correct |
7 |
Correct |
10 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16824 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16772 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16768 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16772 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
10 ms |
16776 KB |
Output is correct |
21 |
Correct |
10 ms |
16768 KB |
Output is correct |
22 |
Correct |
11 ms |
16736 KB |
Output is correct |
23 |
Correct |
9 ms |
16780 KB |
Output is correct |
24 |
Correct |
9 ms |
16772 KB |
Output is correct |
25 |
Correct |
9 ms |
16740 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16776 KB |
Output is correct |
29 |
Correct |
9 ms |
16768 KB |
Output is correct |
30 |
Correct |
9 ms |
16776 KB |
Output is correct |
31 |
Correct |
9 ms |
16780 KB |
Output is correct |
32 |
Correct |
8 ms |
16752 KB |
Output is correct |
33 |
Correct |
10 ms |
16776 KB |
Output is correct |
34 |
Correct |
13 ms |
16852 KB |
Output is correct |
35 |
Correct |
84 ms |
20672 KB |
Output is correct |
36 |
Correct |
102 ms |
23072 KB |
Output is correct |
37 |
Correct |
109 ms |
23160 KB |
Output is correct |
38 |
Correct |
93 ms |
22860 KB |
Output is correct |
39 |
Correct |
10 ms |
16852 KB |
Output is correct |
40 |
Correct |
12 ms |
17108 KB |
Output is correct |
41 |
Correct |
11 ms |
17108 KB |
Output is correct |
42 |
Correct |
10 ms |
16852 KB |
Output is correct |
43 |
Correct |
12 ms |
17044 KB |
Output is correct |
44 |
Correct |
13 ms |
17044 KB |
Output is correct |
45 |
Correct |
10 ms |
16852 KB |
Output is correct |
46 |
Correct |
11 ms |
16888 KB |
Output is correct |
47 |
Correct |
11 ms |
17108 KB |
Output is correct |
48 |
Correct |
13 ms |
17036 KB |
Output is correct |
49 |
Correct |
18 ms |
17876 KB |
Output is correct |
50 |
Correct |
113 ms |
23384 KB |
Output is correct |
51 |
Correct |
14 ms |
17292 KB |
Output is correct |
52 |
Correct |
86 ms |
21588 KB |
Output is correct |
53 |
Correct |
17 ms |
17716 KB |
Output is correct |
54 |
Correct |
94 ms |
22560 KB |
Output is correct |
55 |
Correct |
14 ms |
17492 KB |
Output is correct |
56 |
Correct |
14 ms |
17428 KB |
Output is correct |
57 |
Correct |
15 ms |
17812 KB |
Output is correct |
58 |
Correct |
13 ms |
17808 KB |
Output is correct |
59 |
Correct |
11 ms |
16980 KB |
Output is correct |
60 |
Correct |
69 ms |
19436 KB |
Output is correct |
61 |
Correct |
12 ms |
17108 KB |
Output is correct |
62 |
Correct |
93 ms |
22680 KB |
Output is correct |
63 |
Correct |
416 ms |
65116 KB |
Output is correct |
64 |
Correct |
429 ms |
65228 KB |
Output is correct |
65 |
Correct |
413 ms |
65176 KB |
Output is correct |
66 |
Correct |
117 ms |
23776 KB |
Output is correct |
67 |
Correct |
250 ms |
34140 KB |
Output is correct |
68 |
Correct |
115 ms |
23732 KB |
Output is correct |
69 |
Correct |
327 ms |
30588 KB |
Output is correct |
70 |
Correct |
117 ms |
23820 KB |
Output is correct |
71 |
Correct |
109 ms |
23812 KB |
Output is correct |
72 |
Correct |
310 ms |
31600 KB |
Output is correct |
73 |
Correct |
292 ms |
32188 KB |
Output is correct |
74 |
Correct |
768 ms |
44084 KB |
Output is correct |
75 |
Correct |
451 ms |
35560 KB |
Output is correct |
76 |
Correct |
608 ms |
41348 KB |
Output is correct |
77 |
Correct |
605 ms |
41484 KB |
Output is correct |
78 |
Correct |
155 ms |
29736 KB |
Output is correct |
79 |
Correct |
256 ms |
31640 KB |
Output is correct |
80 |
Correct |
186 ms |
29696 KB |
Output is correct |
81 |
Correct |
276 ms |
31800 KB |
Output is correct |
82 |
Correct |
615 ms |
51412 KB |
Output is correct |
83 |
Correct |
596 ms |
51400 KB |
Output is correct |
84 |
Correct |
543 ms |
71604 KB |
Output is correct |
85 |
Correct |
549 ms |
71516 KB |
Output is correct |
86 |
Correct |
155 ms |
33848 KB |
Output is correct |
87 |
Correct |
171 ms |
34956 KB |
Output is correct |
88 |
Correct |
312 ms |
31832 KB |
Output is correct |
89 |
Correct |
566 ms |
40428 KB |
Output is correct |