#include <bits/stdc++.h>
using namespace std;
set<int> ch[100001],adj[100001],rev[100001];
long long ans = 0;
long long pr[100001],sz[100001];
queue<pair<int,int>> to;
void insertcon(int x,int y){
adj[x].insert(y);
rev[y].insert(x);
if(adj[y].count(x))to.push({x,y});
}
int find(int x){
if(x==pr[x])return x;
return pr[x] = find(pr[x]);
}
int dsu_sz(int x){
return ch[x].size()+adj[x].size()+rev[x].size();
}
void mergegroup(int a,int b){
if(a==b)return ;
if(dsu_sz(a)<dsu_sz(b))swap(a,b);
ans+=sz[a]*ch[b].size()+sz[b]*ch[a].size();
pr[b] = a;sz[a]+=sz[b];
for(auto i:ch[b]){
if(ch[a].count(i))ans-=sz[a];
else ch[a].insert(i);
}
adj[a].erase(b);rev[b].erase(a);
adj[b].erase(a);rev[a].erase(b);
for(auto i:adj[b]){
rev[i].erase(b);
insertcon(a,i);
}for(auto i:rev[b]){
adj[i].erase(b);
insertcon(i,a);
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i = 1;i<=n;i++){
pr[i] = i;
sz[i] = 1;
ch[i].insert(i);
}
for(int i = 0;i<m;i++){
int a,b;cin>>a>>b;
b = find(b);
if(find(a)!=b&&!ch[b].count(a)){
ch[b].insert(a);
ans+=sz[b];
a = find(a);
insertcon(a,b);
while(!to.empty()){
int A = to.front().first;
int B = to.front().second;
mergegroup(find(A),find(B));
to.pop();
}
}
cout<<ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
6 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14292 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
11 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14292 KB |
Output is correct |
11 |
Correct |
8 ms |
14396 KB |
Output is correct |
12 |
Correct |
9 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14292 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
10 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14436 KB |
Output is correct |
30 |
Correct |
8 ms |
14292 KB |
Output is correct |
31 |
Correct |
9 ms |
14540 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
6 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14292 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
11 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14292 KB |
Output is correct |
11 |
Correct |
8 ms |
14396 KB |
Output is correct |
12 |
Correct |
9 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14292 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
10 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14436 KB |
Output is correct |
30 |
Correct |
8 ms |
14292 KB |
Output is correct |
31 |
Correct |
9 ms |
14540 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
34 |
Correct |
20 ms |
14548 KB |
Output is correct |
35 |
Correct |
416 ms |
20172 KB |
Output is correct |
36 |
Correct |
456 ms |
23068 KB |
Output is correct |
37 |
Correct |
446 ms |
23172 KB |
Output is correct |
38 |
Correct |
470 ms |
22776 KB |
Output is correct |
39 |
Correct |
14 ms |
14676 KB |
Output is correct |
40 |
Correct |
15 ms |
15060 KB |
Output is correct |
41 |
Correct |
15 ms |
15060 KB |
Output is correct |
42 |
Correct |
17 ms |
14676 KB |
Output is correct |
43 |
Correct |
15 ms |
14804 KB |
Output is correct |
44 |
Correct |
15 ms |
14804 KB |
Output is correct |
45 |
Correct |
14 ms |
14676 KB |
Output is correct |
46 |
Correct |
15 ms |
14728 KB |
Output is correct |
47 |
Correct |
19 ms |
14932 KB |
Output is correct |
48 |
Correct |
16 ms |
14932 KB |
Output is correct |
49 |
Correct |
38 ms |
15700 KB |
Output is correct |
50 |
Correct |
461 ms |
23268 KB |
Output is correct |
51 |
Correct |
27 ms |
15184 KB |
Output is correct |
52 |
Correct |
441 ms |
21512 KB |
Output is correct |
53 |
Correct |
27 ms |
15572 KB |
Output is correct |
54 |
Correct |
455 ms |
22260 KB |
Output is correct |
55 |
Correct |
18 ms |
15188 KB |
Output is correct |
56 |
Correct |
16 ms |
15188 KB |
Output is correct |
57 |
Correct |
17 ms |
15060 KB |
Output is correct |
58 |
Correct |
16 ms |
15060 KB |
Output is correct |
59 |
Correct |
18 ms |
15188 KB |
Output is correct |
60 |
Correct |
422 ms |
20020 KB |
Output is correct |
61 |
Correct |
16 ms |
14932 KB |
Output is correct |
62 |
Correct |
451 ms |
22620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
6 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14292 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
11 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14292 KB |
Output is correct |
11 |
Correct |
8 ms |
14396 KB |
Output is correct |
12 |
Correct |
9 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14292 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
10 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14436 KB |
Output is correct |
30 |
Correct |
8 ms |
14292 KB |
Output is correct |
31 |
Correct |
9 ms |
14540 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
34 |
Correct |
20 ms |
14548 KB |
Output is correct |
35 |
Correct |
416 ms |
20172 KB |
Output is correct |
36 |
Correct |
456 ms |
23068 KB |
Output is correct |
37 |
Correct |
446 ms |
23172 KB |
Output is correct |
38 |
Correct |
470 ms |
22776 KB |
Output is correct |
39 |
Correct |
14 ms |
14676 KB |
Output is correct |
40 |
Correct |
15 ms |
15060 KB |
Output is correct |
41 |
Correct |
15 ms |
15060 KB |
Output is correct |
42 |
Correct |
17 ms |
14676 KB |
Output is correct |
43 |
Correct |
15 ms |
14804 KB |
Output is correct |
44 |
Correct |
15 ms |
14804 KB |
Output is correct |
45 |
Correct |
14 ms |
14676 KB |
Output is correct |
46 |
Correct |
15 ms |
14728 KB |
Output is correct |
47 |
Correct |
19 ms |
14932 KB |
Output is correct |
48 |
Correct |
16 ms |
14932 KB |
Output is correct |
49 |
Correct |
38 ms |
15700 KB |
Output is correct |
50 |
Correct |
461 ms |
23268 KB |
Output is correct |
51 |
Correct |
27 ms |
15184 KB |
Output is correct |
52 |
Correct |
441 ms |
21512 KB |
Output is correct |
53 |
Correct |
27 ms |
15572 KB |
Output is correct |
54 |
Correct |
455 ms |
22260 KB |
Output is correct |
55 |
Correct |
18 ms |
15188 KB |
Output is correct |
56 |
Correct |
16 ms |
15188 KB |
Output is correct |
57 |
Correct |
17 ms |
15060 KB |
Output is correct |
58 |
Correct |
16 ms |
15060 KB |
Output is correct |
59 |
Correct |
18 ms |
15188 KB |
Output is correct |
60 |
Correct |
422 ms |
20020 KB |
Output is correct |
61 |
Correct |
16 ms |
14932 KB |
Output is correct |
62 |
Correct |
451 ms |
22620 KB |
Output is correct |
63 |
Correct |
937 ms |
68384 KB |
Output is correct |
64 |
Correct |
930 ms |
68252 KB |
Output is correct |
65 |
Correct |
977 ms |
68348 KB |
Output is correct |
66 |
Correct |
475 ms |
34356 KB |
Output is correct |
67 |
Correct |
677 ms |
61380 KB |
Output is correct |
68 |
Correct |
475 ms |
34424 KB |
Output is correct |
69 |
Correct |
653 ms |
35568 KB |
Output is correct |
70 |
Correct |
494 ms |
34436 KB |
Output is correct |
71 |
Correct |
479 ms |
34380 KB |
Output is correct |
72 |
Correct |
664 ms |
45328 KB |
Output is correct |
73 |
Correct |
673 ms |
47060 KB |
Output is correct |
74 |
Correct |
1340 ms |
69004 KB |
Output is correct |
75 |
Correct |
920 ms |
42272 KB |
Output is correct |
76 |
Correct |
1074 ms |
54228 KB |
Output is correct |
77 |
Correct |
1145 ms |
54520 KB |
Output is correct |
78 |
Correct |
422 ms |
38024 KB |
Output is correct |
79 |
Correct |
750 ms |
42404 KB |
Output is correct |
80 |
Correct |
406 ms |
36812 KB |
Output is correct |
81 |
Correct |
703 ms |
40492 KB |
Output is correct |
82 |
Correct |
993 ms |
55012 KB |
Output is correct |
83 |
Correct |
960 ms |
55064 KB |
Output is correct |
84 |
Correct |
789 ms |
53692 KB |
Output is correct |
85 |
Correct |
814 ms |
53828 KB |
Output is correct |
86 |
Correct |
575 ms |
67212 KB |
Output is correct |
87 |
Correct |
721 ms |
69380 KB |
Output is correct |
88 |
Correct |
634 ms |
46284 KB |
Output is correct |
89 |
Correct |
1028 ms |
52348 KB |
Output is correct |