#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e5 + 10 ;
int arr[MAX] ;
int n , m ;
int par[MAX] , sz[MAX] ;
set<int>adj1[MAX] , adj2[MAX] ; //from a clique to another clique and reverse
set<int>adj[MAX] , adjr[MAX] ; //from a node to clique and reverse edge
vector<int>v[MAX] ;
int cnt[MAX] ;
void init()
{
for(int i = 1 ; i <= n ; ++i)
par[i] = i , sz[i] = 1 , v[i] = {i} ;
}
int root(int node)
{
if(par[node] == node)
return node ;
return (par[node] = root(par[node])) ;
}
long long ans = 0 ;
void Union(int x , int y)
{
int a = root(x) ;
int b = root(y) ;
if(a == b)
return ;
if(sz[a] < sz[b])
swap(a , b) ;
vector<int>newedges ;
for(auto &node : adj1[b])
{
adj1[a].insert(node) , adj2[node].erase(b) , adj2[node].insert(a) ;
if(adj2[a].find(node) != adj2[a].end())
newedges.push_back(node) ;
}
for(auto &node : adj2[b])
{
adj1[node].erase(b) ;
adj1[node].insert(a) ;
adj2[a].insert(node) ;
if(adj1[a].find(node) != adj1[a].end())
newedges.push_back(node) ;
}
ans += 1ll * (sz[a] + cnt[a]) * sz[b] ;
for(auto &node : v[b])
{
if(adjr[a].find(node) == adjr[a].end())
ans += sz[a] ;
else
ans -= sz[b] , cnt[a]-- , adjr[a].erase(node) ;
v[a].push_back(node) ;
}
v[b].clear() ;
for(auto &node : adjr[b]) //from node outside b to b
{
if(root(node) == a || adjr[a].find(node) != adjr[a].end())
ans -= sz[b] , cnt[b]-- ;
else
ans += sz[a] , adjr[a].insert(node) ;
}
par[b] = a ;
sz[a] += sz[b] ;
cnt[a] += cnt[b] ;
for(auto &node : newedges)
Union(a , node) ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
init() ;
for(int i = 0 ; i < m ; ++i)
{
int x , y ;
cin>>x>>y ;
if(root(x) != root(y))
{
if(adj1[root(y)].find(root(x)) != adj1[root(y)].end())
Union(x , y) ;
else if(adjr[root(y)].find(x) == adjr[root(y)].end())
{
ans += sz[root(y)] , cnt[root(y)]++ ;
adjr[root(y)].insert(x) , adj1[root(x)].insert(root(y)) , adj2[root(y)].insert(root(x)) ;
}
}
cout<<ans<<"\n" ;
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21460 KB |
Output is correct |
12 |
Correct |
10 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21424 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
12 ms |
21540 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21460 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21588 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21472 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
10 ms |
21460 KB |
Output is correct |
28 |
Correct |
11 ms |
21460 KB |
Output is correct |
29 |
Correct |
11 ms |
21380 KB |
Output is correct |
30 |
Correct |
11 ms |
21460 KB |
Output is correct |
31 |
Correct |
11 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21460 KB |
Output is correct |
33 |
Correct |
11 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21460 KB |
Output is correct |
12 |
Correct |
10 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21424 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
12 ms |
21540 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21460 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21588 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21472 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
10 ms |
21460 KB |
Output is correct |
28 |
Correct |
11 ms |
21460 KB |
Output is correct |
29 |
Correct |
11 ms |
21380 KB |
Output is correct |
30 |
Correct |
11 ms |
21460 KB |
Output is correct |
31 |
Correct |
11 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21460 KB |
Output is correct |
33 |
Correct |
11 ms |
21460 KB |
Output is correct |
34 |
Correct |
12 ms |
21588 KB |
Output is correct |
35 |
Correct |
65 ms |
27340 KB |
Output is correct |
36 |
Correct |
84 ms |
30364 KB |
Output is correct |
37 |
Correct |
96 ms |
30664 KB |
Output is correct |
38 |
Correct |
84 ms |
29992 KB |
Output is correct |
39 |
Correct |
13 ms |
21716 KB |
Output is correct |
40 |
Correct |
13 ms |
21844 KB |
Output is correct |
41 |
Correct |
13 ms |
21844 KB |
Output is correct |
42 |
Correct |
12 ms |
21716 KB |
Output is correct |
43 |
Correct |
13 ms |
21716 KB |
Output is correct |
44 |
Correct |
13 ms |
21716 KB |
Output is correct |
45 |
Correct |
12 ms |
21716 KB |
Output is correct |
46 |
Correct |
12 ms |
21716 KB |
Output is correct |
47 |
Correct |
14 ms |
21844 KB |
Output is correct |
48 |
Correct |
13 ms |
21844 KB |
Output is correct |
49 |
Correct |
19 ms |
22868 KB |
Output is correct |
50 |
Correct |
87 ms |
30532 KB |
Output is correct |
51 |
Correct |
18 ms |
22100 KB |
Output is correct |
52 |
Correct |
93 ms |
28812 KB |
Output is correct |
53 |
Correct |
18 ms |
22740 KB |
Output is correct |
54 |
Correct |
79 ms |
29556 KB |
Output is correct |
55 |
Correct |
14 ms |
22228 KB |
Output is correct |
56 |
Correct |
15 ms |
22180 KB |
Output is correct |
57 |
Correct |
16 ms |
22612 KB |
Output is correct |
58 |
Correct |
15 ms |
22612 KB |
Output is correct |
59 |
Correct |
13 ms |
21844 KB |
Output is correct |
60 |
Correct |
62 ms |
26696 KB |
Output is correct |
61 |
Correct |
14 ms |
21972 KB |
Output is correct |
62 |
Correct |
81 ms |
29900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21460 KB |
Output is correct |
12 |
Correct |
10 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21424 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
12 ms |
21540 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21460 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21588 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21472 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
10 ms |
21460 KB |
Output is correct |
28 |
Correct |
11 ms |
21460 KB |
Output is correct |
29 |
Correct |
11 ms |
21380 KB |
Output is correct |
30 |
Correct |
11 ms |
21460 KB |
Output is correct |
31 |
Correct |
11 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21460 KB |
Output is correct |
33 |
Correct |
11 ms |
21460 KB |
Output is correct |
34 |
Correct |
12 ms |
21588 KB |
Output is correct |
35 |
Correct |
65 ms |
27340 KB |
Output is correct |
36 |
Correct |
84 ms |
30364 KB |
Output is correct |
37 |
Correct |
96 ms |
30664 KB |
Output is correct |
38 |
Correct |
84 ms |
29992 KB |
Output is correct |
39 |
Correct |
13 ms |
21716 KB |
Output is correct |
40 |
Correct |
13 ms |
21844 KB |
Output is correct |
41 |
Correct |
13 ms |
21844 KB |
Output is correct |
42 |
Correct |
12 ms |
21716 KB |
Output is correct |
43 |
Correct |
13 ms |
21716 KB |
Output is correct |
44 |
Correct |
13 ms |
21716 KB |
Output is correct |
45 |
Correct |
12 ms |
21716 KB |
Output is correct |
46 |
Correct |
12 ms |
21716 KB |
Output is correct |
47 |
Correct |
14 ms |
21844 KB |
Output is correct |
48 |
Correct |
13 ms |
21844 KB |
Output is correct |
49 |
Correct |
19 ms |
22868 KB |
Output is correct |
50 |
Correct |
87 ms |
30532 KB |
Output is correct |
51 |
Correct |
18 ms |
22100 KB |
Output is correct |
52 |
Correct |
93 ms |
28812 KB |
Output is correct |
53 |
Correct |
18 ms |
22740 KB |
Output is correct |
54 |
Correct |
79 ms |
29556 KB |
Output is correct |
55 |
Correct |
14 ms |
22228 KB |
Output is correct |
56 |
Correct |
15 ms |
22180 KB |
Output is correct |
57 |
Correct |
16 ms |
22612 KB |
Output is correct |
58 |
Correct |
15 ms |
22612 KB |
Output is correct |
59 |
Correct |
13 ms |
21844 KB |
Output is correct |
60 |
Correct |
62 ms |
26696 KB |
Output is correct |
61 |
Correct |
14 ms |
21972 KB |
Output is correct |
62 |
Correct |
81 ms |
29900 KB |
Output is correct |
63 |
Correct |
424 ms |
73484 KB |
Output is correct |
64 |
Correct |
434 ms |
73556 KB |
Output is correct |
65 |
Correct |
442 ms |
73380 KB |
Output is correct |
66 |
Correct |
129 ms |
39900 KB |
Output is correct |
67 |
Correct |
329 ms |
47376 KB |
Output is correct |
68 |
Correct |
126 ms |
39876 KB |
Output is correct |
69 |
Correct |
285 ms |
39872 KB |
Output is correct |
70 |
Correct |
152 ms |
39880 KB |
Output is correct |
71 |
Correct |
125 ms |
39864 KB |
Output is correct |
72 |
Correct |
314 ms |
45544 KB |
Output is correct |
73 |
Correct |
325 ms |
45768 KB |
Output is correct |
74 |
Correct |
867 ms |
75576 KB |
Output is correct |
75 |
Correct |
491 ms |
47280 KB |
Output is correct |
76 |
Correct |
645 ms |
61108 KB |
Output is correct |
77 |
Correct |
630 ms |
61376 KB |
Output is correct |
78 |
Correct |
175 ms |
43296 KB |
Output is correct |
79 |
Correct |
289 ms |
48324 KB |
Output is correct |
80 |
Correct |
185 ms |
42092 KB |
Output is correct |
81 |
Correct |
345 ms |
45624 KB |
Output is correct |
82 |
Correct |
573 ms |
61216 KB |
Output is correct |
83 |
Correct |
567 ms |
61044 KB |
Output is correct |
84 |
Correct |
541 ms |
77704 KB |
Output is correct |
85 |
Correct |
575 ms |
77892 KB |
Output is correct |
86 |
Correct |
179 ms |
44000 KB |
Output is correct |
87 |
Correct |
212 ms |
46080 KB |
Output is correct |
88 |
Correct |
342 ms |
46020 KB |
Output is correct |
89 |
Correct |
601 ms |
58412 KB |
Output is correct |