#include<bits/stdc++.h>
//#define cin fin
//#define cout fout
using namespace std;
//ifstream fin("a.in");
//ofstream fout("a.out");
const int N = 2e5;
typedef long long ll;
ll ans = 0;
int n,m;
struct DSU{
int p,sz;
set <int> boys;
vector <int> comp;
}dsu[N];
set <int> edges[N];
multiset <int> compedges[N];
int leader(int x){
if(x == dsu[x].p)return x;
return dsu[x].p = leader(dsu[x].p);
}
bool checkedge(int x,int y){
int compx = leader(x);
int compy = leader(y);
if(compx == compy)return 1;
if(dsu[compy].boys.find(x) != dsu[compy].boys.end())return 1;
return 0;
}
bool check(int x,int y){
if(x == y)return 1;
if(compedges[x].find(y) != compedges[x].end())return 1;
return 0;
}
void connect(int x,int y){
x = leader(x);
y = leader(y);
if(x == y)return;
if(dsu[x].sz + dsu[x].boys.size() + compedges[x].size() > dsu[y].sz + dsu[y].boys.size() + compedges[y].size())swap(x,y);
///check new edges
queue <int> q;
int nr1 = -1;
for(auto i:dsu[x].boys){
if(leader(i) == x || leader(i) == y)continue;
if(check(leader(i),y) && check(x,leader(i))){
q.push(leader(i));
}
if(check(leader(i),x) && check(x,leader(i))){
q.push(leader(i));
}
if(check(leader(i),y) && check(y,leader(i))){
q.push(leader(i));
}
if(check(leader(i),x) && check(y,leader(i))){
q.push(leader(i));
}
}
for(auto i:compedges[x]){
if(leader(i) == x || leader(i) == y)continue;
if(check(leader(i),y) && check(x,leader(i))){
q.push(leader(i));
}
if(check(leader(i),x) && check(x,leader(i))){
q.push(leader(i));
}
if(check(leader(i),y) && check(y,leader(i))){
q.push(leader(i));
}
if(check(leader(i),x) && check(y,leader(i))){
q.push(leader(i));
}
}
///y connected to x
auto it = dsu[x].boys.begin();
while(it != dsu[x].boys.end()){
if(leader(*it) == y){
ans-=dsu[x].sz;
it = dsu[x].boys.erase(it);
edges[*it].erase(x);
compedges[y].erase(compedges[y].find(x));
}else{
it++;
}
}
///x connected to y
for(auto i:dsu[x].comp){
auto it = dsu[y].boys.find(i);
if(it != dsu[y].boys.end()){
ans-=dsu[y].sz;
dsu[y].boys.erase(it);
edges[*it].erase(y);
compedges[x].erase(compedges[x].find(y));
}
dsu[y].comp.push_back(i);
}
ans+=1ll*dsu[x].boys.size()*dsu[y].sz;
ans+=1ll*dsu[y].boys.size()*dsu[x].sz;
///x and y connected to random node
for(auto i:dsu[x].boys){
assert(leader(i) != y);
if(dsu[y].boys.find(i) != dsu[y].boys.end()){
ans-=dsu[x].sz;
ans-=dsu[y].sz;
}
dsu[y].boys.insert(i);
edges[i].erase(x);
compedges[leader(i)].erase(compedges[leader(i)].find(leader(x)));
edges[i].insert(y);
compedges[leader(i)].insert(leader(y));
}
for(auto i:compedges[x]){
compedges[y].insert(i);
}
ans+=2ll*dsu[x].sz*dsu[y].sz;
dsu[x].p = y;
dsu[y].sz+=dsu[x].sz;
dsu[x].comp.clear();
dsu[x].boys.clear();
compedges[x].clear();
while(!q.empty()){
int x = q.front();
q.pop();
connect(y,x);
}
}
void addedge(int x,int y){
int compx = leader(x);
int compy = leader(y);
if(checkedge(x,y))return;
if(check(compy,compx)){
connect(x,y);
}else{
dsu[compy].boys.insert(x);
edges[x].insert(compy);
compedges[compx].insert(compy);
ans+=dsu[compy].sz;
}
}
void dbg(){
for(int i = 0;i < n;i++){
cout<<"dsuinvestigate:"<<i<<'\n';
cout<<dsu[i].p<<' '<<dsu[i].sz<<'\n';
for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i = 0;i < n;i++){
dsu[i] = {i,1};
dsu[i].comp.push_back(i);
}
for(int i = 0;i < m;i++){
int u,w;
cin>>u>>w;
u--;w--;
addedge(u,w);
//dbg();
cout<<ans<<'\n';
}
return 0;
}
/**
5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2
5 2
1 4
4 1
3 4
5 1
2 4
2 1
4 3
1 3
5 4
3 5
5 3
**/
Compilation message
joitter2.cpp: In function 'void connect(int, int)':
joitter2.cpp:41:9: warning: unused variable 'nr1' [-Wunused-variable]
41 | int nr1 = -1;
| ^~~
joitter2.cpp: In function 'void dbg()':
joitter2.cpp:143:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
143 | for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
| ^~~
joitter2.cpp:143:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
143 | for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
| ^~~~
joitter2.cpp:144:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
144 | for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
| ^~~
joitter2.cpp:144:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
144 | for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
34648 KB |
Output is correct |
2 |
Correct |
6 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
6 ms |
34652 KB |
Output is correct |
5 |
Correct |
6 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34908 KB |
Output is correct |
7 |
Correct |
6 ms |
34908 KB |
Output is correct |
8 |
Correct |
6 ms |
34928 KB |
Output is correct |
9 |
Correct |
7 ms |
35160 KB |
Output is correct |
10 |
Correct |
6 ms |
34652 KB |
Output is correct |
11 |
Correct |
6 ms |
34680 KB |
Output is correct |
12 |
Correct |
5 ms |
34652 KB |
Output is correct |
13 |
Correct |
6 ms |
34652 KB |
Output is correct |
14 |
Correct |
6 ms |
34652 KB |
Output is correct |
15 |
Correct |
5 ms |
34908 KB |
Output is correct |
16 |
Correct |
5 ms |
34676 KB |
Output is correct |
17 |
Correct |
5 ms |
34652 KB |
Output is correct |
18 |
Correct |
6 ms |
34648 KB |
Output is correct |
19 |
Correct |
5 ms |
34652 KB |
Output is correct |
20 |
Correct |
7 ms |
34908 KB |
Output is correct |
21 |
Correct |
5 ms |
34856 KB |
Output is correct |
22 |
Correct |
5 ms |
34668 KB |
Output is correct |
23 |
Correct |
6 ms |
34908 KB |
Output is correct |
24 |
Correct |
8 ms |
34740 KB |
Output is correct |
25 |
Correct |
6 ms |
34908 KB |
Output is correct |
26 |
Correct |
6 ms |
34904 KB |
Output is correct |
27 |
Correct |
6 ms |
34908 KB |
Output is correct |
28 |
Correct |
6 ms |
34908 KB |
Output is correct |
29 |
Correct |
6 ms |
34808 KB |
Output is correct |
30 |
Correct |
6 ms |
34652 KB |
Output is correct |
31 |
Correct |
6 ms |
34680 KB |
Output is correct |
32 |
Correct |
5 ms |
34908 KB |
Output is correct |
33 |
Correct |
6 ms |
34908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
34648 KB |
Output is correct |
2 |
Correct |
6 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
6 ms |
34652 KB |
Output is correct |
5 |
Correct |
6 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34908 KB |
Output is correct |
7 |
Correct |
6 ms |
34908 KB |
Output is correct |
8 |
Correct |
6 ms |
34928 KB |
Output is correct |
9 |
Correct |
7 ms |
35160 KB |
Output is correct |
10 |
Correct |
6 ms |
34652 KB |
Output is correct |
11 |
Correct |
6 ms |
34680 KB |
Output is correct |
12 |
Correct |
5 ms |
34652 KB |
Output is correct |
13 |
Correct |
6 ms |
34652 KB |
Output is correct |
14 |
Correct |
6 ms |
34652 KB |
Output is correct |
15 |
Correct |
5 ms |
34908 KB |
Output is correct |
16 |
Correct |
5 ms |
34676 KB |
Output is correct |
17 |
Correct |
5 ms |
34652 KB |
Output is correct |
18 |
Correct |
6 ms |
34648 KB |
Output is correct |
19 |
Correct |
5 ms |
34652 KB |
Output is correct |
20 |
Correct |
7 ms |
34908 KB |
Output is correct |
21 |
Correct |
5 ms |
34856 KB |
Output is correct |
22 |
Correct |
5 ms |
34668 KB |
Output is correct |
23 |
Correct |
6 ms |
34908 KB |
Output is correct |
24 |
Correct |
8 ms |
34740 KB |
Output is correct |
25 |
Correct |
6 ms |
34908 KB |
Output is correct |
26 |
Correct |
6 ms |
34904 KB |
Output is correct |
27 |
Correct |
6 ms |
34908 KB |
Output is correct |
28 |
Correct |
6 ms |
34908 KB |
Output is correct |
29 |
Correct |
6 ms |
34808 KB |
Output is correct |
30 |
Correct |
6 ms |
34652 KB |
Output is correct |
31 |
Correct |
6 ms |
34680 KB |
Output is correct |
32 |
Correct |
5 ms |
34908 KB |
Output is correct |
33 |
Correct |
6 ms |
34908 KB |
Output is correct |
34 |
Correct |
7 ms |
34908 KB |
Output is correct |
35 |
Correct |
58 ms |
38764 KB |
Output is correct |
36 |
Correct |
89 ms |
41412 KB |
Output is correct |
37 |
Correct |
94 ms |
41456 KB |
Output is correct |
38 |
Correct |
77 ms |
41144 KB |
Output is correct |
39 |
Correct |
7 ms |
34904 KB |
Output is correct |
40 |
Correct |
8 ms |
35164 KB |
Output is correct |
41 |
Correct |
8 ms |
35128 KB |
Output is correct |
42 |
Correct |
7 ms |
34908 KB |
Output is correct |
43 |
Correct |
8 ms |
35160 KB |
Output is correct |
44 |
Correct |
8 ms |
34908 KB |
Output is correct |
45 |
Correct |
7 ms |
34908 KB |
Output is correct |
46 |
Correct |
7 ms |
34908 KB |
Output is correct |
47 |
Correct |
8 ms |
35164 KB |
Output is correct |
48 |
Correct |
8 ms |
35132 KB |
Output is correct |
49 |
Correct |
16 ms |
36188 KB |
Output is correct |
50 |
Correct |
100 ms |
41656 KB |
Output is correct |
51 |
Correct |
10 ms |
35160 KB |
Output is correct |
52 |
Correct |
66 ms |
40124 KB |
Output is correct |
53 |
Correct |
14 ms |
35932 KB |
Output is correct |
54 |
Correct |
74 ms |
40972 KB |
Output is correct |
55 |
Correct |
10 ms |
35420 KB |
Output is correct |
56 |
Correct |
9 ms |
35420 KB |
Output is correct |
57 |
Correct |
10 ms |
36952 KB |
Output is correct |
58 |
Correct |
11 ms |
36948 KB |
Output is correct |
59 |
Correct |
7 ms |
34908 KB |
Output is correct |
60 |
Correct |
51 ms |
37508 KB |
Output is correct |
61 |
Correct |
8 ms |
35160 KB |
Output is correct |
62 |
Correct |
87 ms |
41096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
34648 KB |
Output is correct |
2 |
Correct |
6 ms |
34652 KB |
Output is correct |
3 |
Correct |
5 ms |
34652 KB |
Output is correct |
4 |
Correct |
6 ms |
34652 KB |
Output is correct |
5 |
Correct |
6 ms |
34652 KB |
Output is correct |
6 |
Correct |
5 ms |
34908 KB |
Output is correct |
7 |
Correct |
6 ms |
34908 KB |
Output is correct |
8 |
Correct |
6 ms |
34928 KB |
Output is correct |
9 |
Correct |
7 ms |
35160 KB |
Output is correct |
10 |
Correct |
6 ms |
34652 KB |
Output is correct |
11 |
Correct |
6 ms |
34680 KB |
Output is correct |
12 |
Correct |
5 ms |
34652 KB |
Output is correct |
13 |
Correct |
6 ms |
34652 KB |
Output is correct |
14 |
Correct |
6 ms |
34652 KB |
Output is correct |
15 |
Correct |
5 ms |
34908 KB |
Output is correct |
16 |
Correct |
5 ms |
34676 KB |
Output is correct |
17 |
Correct |
5 ms |
34652 KB |
Output is correct |
18 |
Correct |
6 ms |
34648 KB |
Output is correct |
19 |
Correct |
5 ms |
34652 KB |
Output is correct |
20 |
Correct |
7 ms |
34908 KB |
Output is correct |
21 |
Correct |
5 ms |
34856 KB |
Output is correct |
22 |
Correct |
5 ms |
34668 KB |
Output is correct |
23 |
Correct |
6 ms |
34908 KB |
Output is correct |
24 |
Correct |
8 ms |
34740 KB |
Output is correct |
25 |
Correct |
6 ms |
34908 KB |
Output is correct |
26 |
Correct |
6 ms |
34904 KB |
Output is correct |
27 |
Correct |
6 ms |
34908 KB |
Output is correct |
28 |
Correct |
6 ms |
34908 KB |
Output is correct |
29 |
Correct |
6 ms |
34808 KB |
Output is correct |
30 |
Correct |
6 ms |
34652 KB |
Output is correct |
31 |
Correct |
6 ms |
34680 KB |
Output is correct |
32 |
Correct |
5 ms |
34908 KB |
Output is correct |
33 |
Correct |
6 ms |
34908 KB |
Output is correct |
34 |
Correct |
7 ms |
34908 KB |
Output is correct |
35 |
Correct |
58 ms |
38764 KB |
Output is correct |
36 |
Correct |
89 ms |
41412 KB |
Output is correct |
37 |
Correct |
94 ms |
41456 KB |
Output is correct |
38 |
Correct |
77 ms |
41144 KB |
Output is correct |
39 |
Correct |
7 ms |
34904 KB |
Output is correct |
40 |
Correct |
8 ms |
35164 KB |
Output is correct |
41 |
Correct |
8 ms |
35128 KB |
Output is correct |
42 |
Correct |
7 ms |
34908 KB |
Output is correct |
43 |
Correct |
8 ms |
35160 KB |
Output is correct |
44 |
Correct |
8 ms |
34908 KB |
Output is correct |
45 |
Correct |
7 ms |
34908 KB |
Output is correct |
46 |
Correct |
7 ms |
34908 KB |
Output is correct |
47 |
Correct |
8 ms |
35164 KB |
Output is correct |
48 |
Correct |
8 ms |
35132 KB |
Output is correct |
49 |
Correct |
16 ms |
36188 KB |
Output is correct |
50 |
Correct |
100 ms |
41656 KB |
Output is correct |
51 |
Correct |
10 ms |
35160 KB |
Output is correct |
52 |
Correct |
66 ms |
40124 KB |
Output is correct |
53 |
Correct |
14 ms |
35932 KB |
Output is correct |
54 |
Correct |
74 ms |
40972 KB |
Output is correct |
55 |
Correct |
10 ms |
35420 KB |
Output is correct |
56 |
Correct |
9 ms |
35420 KB |
Output is correct |
57 |
Correct |
10 ms |
36952 KB |
Output is correct |
58 |
Correct |
11 ms |
36948 KB |
Output is correct |
59 |
Correct |
7 ms |
34908 KB |
Output is correct |
60 |
Correct |
51 ms |
37508 KB |
Output is correct |
61 |
Correct |
8 ms |
35160 KB |
Output is correct |
62 |
Correct |
87 ms |
41096 KB |
Output is correct |
63 |
Correct |
299 ms |
82252 KB |
Output is correct |
64 |
Correct |
313 ms |
82256 KB |
Output is correct |
65 |
Correct |
289 ms |
82260 KB |
Output is correct |
66 |
Correct |
76 ms |
45000 KB |
Output is correct |
67 |
Correct |
121 ms |
49996 KB |
Output is correct |
68 |
Correct |
117 ms |
44992 KB |
Output is correct |
69 |
Correct |
174 ms |
49880 KB |
Output is correct |
70 |
Correct |
88 ms |
47300 KB |
Output is correct |
71 |
Correct |
79 ms |
47300 KB |
Output is correct |
72 |
Correct |
149 ms |
50636 KB |
Output is correct |
73 |
Correct |
179 ms |
50740 KB |
Output is correct |
74 |
Correct |
510 ms |
68304 KB |
Output is correct |
75 |
Correct |
281 ms |
56612 KB |
Output is correct |
76 |
Correct |
353 ms |
62152 KB |
Output is correct |
77 |
Correct |
381 ms |
63932 KB |
Output is correct |
78 |
Correct |
102 ms |
49808 KB |
Output is correct |
79 |
Correct |
181 ms |
54216 KB |
Output is correct |
80 |
Correct |
101 ms |
49464 KB |
Output is correct |
81 |
Correct |
154 ms |
52936 KB |
Output is correct |
82 |
Correct |
393 ms |
70352 KB |
Output is correct |
83 |
Correct |
359 ms |
70220 KB |
Output is correct |
84 |
Correct |
413 ms |
143676 KB |
Output is correct |
85 |
Correct |
430 ms |
143820 KB |
Output is correct |
86 |
Correct |
87 ms |
51352 KB |
Output is correct |
87 |
Correct |
117 ms |
53516 KB |
Output is correct |
88 |
Correct |
172 ms |
50768 KB |
Output is correct |
89 |
Correct |
335 ms |
60912 KB |
Output is correct |