#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
const I N=100000;
I pars[N];
set<I>incs1[N],outs1[N];
set<I>incs2[N],outs2[N];
set<I>nods[N];
Lli res=0;
I fnd(I i){
return pars[i]<0?i:pars[i]=fnd(pars[i]);
}
I siz(I i){
return-pars[fnd(i)];
}
void uni(I a,I b){
if((a=fnd(a))==(b=fnd(b)))return;
if(pars[a]>pars[b])swap(a,b);
Lli siz1=siz(a),siz2=siz(b);
res-=siz1*(incs1[a].size()+siz1-1);
res-=siz2*(incs1[b].size()+siz2-1);
for(auto c:incs1[b]){
outs1[c].erase(b);
if(fnd(c)==a)continue;
incs1[a].insert(c);
outs1[c].insert(a);
}
for(auto c:nods[b]){
nods[a].insert(c);
if(!outs1[c].count(a))continue;
outs1[c].erase(a);
incs1[a].erase(c);
}
for(auto c:incs2[b]){
outs2[c].erase(b);
if(c==a)continue;
incs2[a].insert(c);
outs2[c].insert(a);
}
for(auto c:outs2[b]){
incs2[c].erase(b);
if(c==a)continue;
outs2[a].insert(c);
incs2[c].insert(a);
}
pars[a]+=pars[b],pars[b]=a;
Lli siz3=siz(a);
res+=siz3*(incs1[a].size()+siz3-1);
for(auto c:incs2[b]){
if(c==a)continue;
if(outs2[a].count(c))uni(a,c);
}
for(auto c:outs2[b]){
if(c==a)continue;
if(outs2[c].count(a))uni(c,a);
}
incs1[b].clear();
incs2[b].clear(),outs2[b].clear();
nods[b].clear();
}
I main(){
cin.tie(0)->sync_with_stdio(0);
I n,m;cin>>n>>m;
fill_n(pars,n,-1);
for(I i=0;i<n;i++)nods[i].insert(i);
for(I i=0;i<m;i++){
I a,b;cin>>a>>b,a--,b--;
I c=fnd(a),d=fnd(b);
if(c!=d){
if(!outs1[a].count(d)){
incs1[d].insert(a);
outs1[a].insert(d);
res+=siz(d);
}
if(!outs2[c].count(d)){
incs2[d].insert(c);
outs2[c].insert(d);
if(outs2[d].count(c))uni(c,d);
}
}
printf("%lli\n",res);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
12 ms |
23744 KB |
Output is correct |
4 |
Correct |
13 ms |
23868 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23860 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23864 KB |
Output is correct |
10 |
Correct |
12 ms |
23708 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23740 KB |
Output is correct |
13 |
Correct |
13 ms |
23812 KB |
Output is correct |
14 |
Correct |
12 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23804 KB |
Output is correct |
16 |
Correct |
12 ms |
23804 KB |
Output is correct |
17 |
Correct |
12 ms |
23720 KB |
Output is correct |
18 |
Correct |
12 ms |
23740 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
15 ms |
23812 KB |
Output is correct |
22 |
Correct |
13 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23744 KB |
Output is correct |
25 |
Correct |
12 ms |
23816 KB |
Output is correct |
26 |
Correct |
12 ms |
23824 KB |
Output is correct |
27 |
Correct |
12 ms |
23800 KB |
Output is correct |
28 |
Correct |
12 ms |
23752 KB |
Output is correct |
29 |
Correct |
15 ms |
23804 KB |
Output is correct |
30 |
Correct |
12 ms |
23736 KB |
Output is correct |
31 |
Correct |
16 ms |
23792 KB |
Output is correct |
32 |
Correct |
13 ms |
23804 KB |
Output is correct |
33 |
Correct |
14 ms |
23732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
12 ms |
23744 KB |
Output is correct |
4 |
Correct |
13 ms |
23868 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23860 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23864 KB |
Output is correct |
10 |
Correct |
12 ms |
23708 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23740 KB |
Output is correct |
13 |
Correct |
13 ms |
23812 KB |
Output is correct |
14 |
Correct |
12 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23804 KB |
Output is correct |
16 |
Correct |
12 ms |
23804 KB |
Output is correct |
17 |
Correct |
12 ms |
23720 KB |
Output is correct |
18 |
Correct |
12 ms |
23740 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
15 ms |
23812 KB |
Output is correct |
22 |
Correct |
13 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23744 KB |
Output is correct |
25 |
Correct |
12 ms |
23816 KB |
Output is correct |
26 |
Correct |
12 ms |
23824 KB |
Output is correct |
27 |
Correct |
12 ms |
23800 KB |
Output is correct |
28 |
Correct |
12 ms |
23752 KB |
Output is correct |
29 |
Correct |
15 ms |
23804 KB |
Output is correct |
30 |
Correct |
12 ms |
23736 KB |
Output is correct |
31 |
Correct |
16 ms |
23792 KB |
Output is correct |
32 |
Correct |
13 ms |
23804 KB |
Output is correct |
33 |
Correct |
14 ms |
23732 KB |
Output is correct |
34 |
Correct |
15 ms |
24052 KB |
Output is correct |
35 |
Correct |
84 ms |
30156 KB |
Output is correct |
36 |
Correct |
115 ms |
33528 KB |
Output is correct |
37 |
Correct |
114 ms |
33664 KB |
Output is correct |
38 |
Correct |
104 ms |
33204 KB |
Output is correct |
39 |
Correct |
15 ms |
23944 KB |
Output is correct |
40 |
Correct |
17 ms |
24156 KB |
Output is correct |
41 |
Correct |
20 ms |
24076 KB |
Output is correct |
42 |
Correct |
15 ms |
23948 KB |
Output is correct |
43 |
Correct |
19 ms |
24080 KB |
Output is correct |
44 |
Correct |
16 ms |
24080 KB |
Output is correct |
45 |
Correct |
14 ms |
23948 KB |
Output is correct |
46 |
Correct |
13 ms |
23944 KB |
Output is correct |
47 |
Correct |
16 ms |
24148 KB |
Output is correct |
48 |
Correct |
16 ms |
24084 KB |
Output is correct |
49 |
Correct |
24 ms |
25048 KB |
Output is correct |
50 |
Correct |
153 ms |
33672 KB |
Output is correct |
51 |
Correct |
22 ms |
24424 KB |
Output is correct |
52 |
Correct |
99 ms |
31612 KB |
Output is correct |
53 |
Correct |
26 ms |
24916 KB |
Output is correct |
54 |
Correct |
107 ms |
32636 KB |
Output is correct |
55 |
Correct |
18 ms |
24656 KB |
Output is correct |
56 |
Correct |
17 ms |
24660 KB |
Output is correct |
57 |
Correct |
17 ms |
24916 KB |
Output is correct |
58 |
Correct |
17 ms |
24968 KB |
Output is correct |
59 |
Correct |
14 ms |
23920 KB |
Output is correct |
60 |
Correct |
81 ms |
28896 KB |
Output is correct |
61 |
Correct |
16 ms |
24148 KB |
Output is correct |
62 |
Correct |
107 ms |
32904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
12 ms |
23744 KB |
Output is correct |
4 |
Correct |
13 ms |
23868 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23860 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23864 KB |
Output is correct |
10 |
Correct |
12 ms |
23708 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23740 KB |
Output is correct |
13 |
Correct |
13 ms |
23812 KB |
Output is correct |
14 |
Correct |
12 ms |
23808 KB |
Output is correct |
15 |
Correct |
12 ms |
23804 KB |
Output is correct |
16 |
Correct |
12 ms |
23804 KB |
Output is correct |
17 |
Correct |
12 ms |
23720 KB |
Output is correct |
18 |
Correct |
12 ms |
23740 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
15 ms |
23812 KB |
Output is correct |
22 |
Correct |
13 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23744 KB |
Output is correct |
25 |
Correct |
12 ms |
23816 KB |
Output is correct |
26 |
Correct |
12 ms |
23824 KB |
Output is correct |
27 |
Correct |
12 ms |
23800 KB |
Output is correct |
28 |
Correct |
12 ms |
23752 KB |
Output is correct |
29 |
Correct |
15 ms |
23804 KB |
Output is correct |
30 |
Correct |
12 ms |
23736 KB |
Output is correct |
31 |
Correct |
16 ms |
23792 KB |
Output is correct |
32 |
Correct |
13 ms |
23804 KB |
Output is correct |
33 |
Correct |
14 ms |
23732 KB |
Output is correct |
34 |
Correct |
15 ms |
24052 KB |
Output is correct |
35 |
Correct |
84 ms |
30156 KB |
Output is correct |
36 |
Correct |
115 ms |
33528 KB |
Output is correct |
37 |
Correct |
114 ms |
33664 KB |
Output is correct |
38 |
Correct |
104 ms |
33204 KB |
Output is correct |
39 |
Correct |
15 ms |
23944 KB |
Output is correct |
40 |
Correct |
17 ms |
24156 KB |
Output is correct |
41 |
Correct |
20 ms |
24076 KB |
Output is correct |
42 |
Correct |
15 ms |
23948 KB |
Output is correct |
43 |
Correct |
19 ms |
24080 KB |
Output is correct |
44 |
Correct |
16 ms |
24080 KB |
Output is correct |
45 |
Correct |
14 ms |
23948 KB |
Output is correct |
46 |
Correct |
13 ms |
23944 KB |
Output is correct |
47 |
Correct |
16 ms |
24148 KB |
Output is correct |
48 |
Correct |
16 ms |
24084 KB |
Output is correct |
49 |
Correct |
24 ms |
25048 KB |
Output is correct |
50 |
Correct |
153 ms |
33672 KB |
Output is correct |
51 |
Correct |
22 ms |
24424 KB |
Output is correct |
52 |
Correct |
99 ms |
31612 KB |
Output is correct |
53 |
Correct |
26 ms |
24916 KB |
Output is correct |
54 |
Correct |
107 ms |
32636 KB |
Output is correct |
55 |
Correct |
18 ms |
24656 KB |
Output is correct |
56 |
Correct |
17 ms |
24660 KB |
Output is correct |
57 |
Correct |
17 ms |
24916 KB |
Output is correct |
58 |
Correct |
17 ms |
24968 KB |
Output is correct |
59 |
Correct |
14 ms |
23920 KB |
Output is correct |
60 |
Correct |
81 ms |
28896 KB |
Output is correct |
61 |
Correct |
16 ms |
24148 KB |
Output is correct |
62 |
Correct |
107 ms |
32904 KB |
Output is correct |
63 |
Correct |
519 ms |
90688 KB |
Output is correct |
64 |
Correct |
488 ms |
90696 KB |
Output is correct |
65 |
Correct |
490 ms |
90692 KB |
Output is correct |
66 |
Correct |
229 ms |
33300 KB |
Output is correct |
67 |
Correct |
478 ms |
42076 KB |
Output is correct |
68 |
Correct |
195 ms |
33228 KB |
Output is correct |
69 |
Correct |
495 ms |
42700 KB |
Output is correct |
70 |
Correct |
216 ms |
33376 KB |
Output is correct |
71 |
Correct |
205 ms |
33308 KB |
Output is correct |
72 |
Correct |
509 ms |
41992 KB |
Output is correct |
73 |
Correct |
545 ms |
42152 KB |
Output is correct |
74 |
Correct |
1028 ms |
56780 KB |
Output is correct |
75 |
Correct |
674 ms |
49764 KB |
Output is correct |
76 |
Correct |
842 ms |
57180 KB |
Output is correct |
77 |
Correct |
870 ms |
57240 KB |
Output is correct |
78 |
Correct |
283 ms |
41544 KB |
Output is correct |
79 |
Correct |
436 ms |
45020 KB |
Output is correct |
80 |
Correct |
224 ms |
41464 KB |
Output is correct |
81 |
Correct |
414 ms |
44768 KB |
Output is correct |
82 |
Correct |
768 ms |
70004 KB |
Output is correct |
83 |
Correct |
764 ms |
70060 KB |
Output is correct |
84 |
Correct |
751 ms |
85580 KB |
Output is correct |
85 |
Correct |
761 ms |
85432 KB |
Output is correct |
86 |
Correct |
384 ms |
34076 KB |
Output is correct |
87 |
Correct |
351 ms |
36316 KB |
Output is correct |
88 |
Correct |
524 ms |
42036 KB |
Output is correct |
89 |
Correct |
852 ms |
55612 KB |
Output is correct |