#include<bits/stdc++.h>
//tof_to_in_zendegy_ke_man_eshtebahy_ye_soal_dg_hal_kardam
using namespace std;
const int maxn=100000+10;
int n,m;
struct dsu{
int par[maxn],sz[maxn];
set<pair<int,int>>kho[maxn];
set<int>vorod[maxn];
long long res;
dsu(){
res=0;
for(int i=1;i<maxn;i++){
par[i]=i;
sz[i]=1;
}
}
int root(int u){
if(u==par[u]){
return u;
}
return par[u]=root(par[u]);
}
void con(int u,int v){
u=root(u),v=root(v);
//cout<<u<<" "<<v<<" con:\n";
if(sz[u]>sz[v]){
swap(u,v);
}
vector<pair<int,int>>allq;
for(auto x:kho[u]){
res-=sz[x.first];
vorod[x.first].erase(x.second);
allq.push_back(make_pair(x.second,x.first));
}
res+=sz[u]*(2*sz[v]+vorod[v].size());
res-=sz[u]*vorod[u].size();
for(auto x:vorod[u]){
int rootx=root(x);
kho[rootx].erase(make_pair(u,x));
allq.push_back(make_pair(x,u));
}
vorod[u].clear();
kho[u].clear();
par[u]=v;
sz[v]+=sz[u];
//cout<<"wtf: "<<allq.size()<<" "<<u<<" "<<v<<" "<<sz[u]<<" "<<sz[v]<<"\n";
for(auto x:allq){
//cout<<"injy: "<<x.first<<" "<<x.second<<" "<<res<<"\n";
add(x.first,x.second);
}
}
void add(int u,int v){
int rootv=root(v),rootu=root(u);
//cout<<"add: "<<u<<" "<<v<<" "<<rootu<<" "<<rootv<<"\n";
if(rootu==rootv||vorod[rootv].count(u)!=0){
//cout<<"salam"<<endl;
return ;
}
auto x=kho[rootv].lower_bound(make_pair(rootu,-1));
if(x!=kho[rootv].end()&&(*x).first==rootu){
return con(u,v);
}
//cout<<"nababa: "<<sz[rootv]<<'\n';
res+=sz[rootv];
kho[rootu].insert(make_pair(rootv,u));
vorod[rootv].insert(u);
}
}ds;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
ds.add(u,v);
cout<<ds.res<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10440 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10636 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10572 KB |
Output is correct |
22 |
Correct |
3 ms |
10584 KB |
Output is correct |
23 |
Correct |
3 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10672 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10652 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10440 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10636 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10572 KB |
Output is correct |
22 |
Correct |
3 ms |
10584 KB |
Output is correct |
23 |
Correct |
3 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10672 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10652 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
4 ms |
10588 KB |
Output is correct |
35 |
Correct |
56 ms |
16196 KB |
Output is correct |
36 |
Correct |
71 ms |
18516 KB |
Output is correct |
37 |
Correct |
71 ms |
18492 KB |
Output is correct |
38 |
Correct |
70 ms |
18260 KB |
Output is correct |
39 |
Correct |
4 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
5 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10584 KB |
Output is correct |
44 |
Correct |
4 ms |
10648 KB |
Output is correct |
45 |
Correct |
5 ms |
10588 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
6 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10588 KB |
Output is correct |
49 |
Correct |
9 ms |
11356 KB |
Output is correct |
50 |
Correct |
73 ms |
18496 KB |
Output is correct |
51 |
Correct |
6 ms |
10840 KB |
Output is correct |
52 |
Correct |
78 ms |
17420 KB |
Output is correct |
53 |
Correct |
8 ms |
11356 KB |
Output is correct |
54 |
Correct |
66 ms |
18000 KB |
Output is correct |
55 |
Correct |
5 ms |
10840 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11100 KB |
Output is correct |
58 |
Correct |
5 ms |
11100 KB |
Output is correct |
59 |
Correct |
4 ms |
10688 KB |
Output is correct |
60 |
Correct |
56 ms |
15444 KB |
Output is correct |
61 |
Correct |
4 ms |
10588 KB |
Output is correct |
62 |
Correct |
70 ms |
18256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10440 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10636 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10572 KB |
Output is correct |
22 |
Correct |
3 ms |
10584 KB |
Output is correct |
23 |
Correct |
3 ms |
10584 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10672 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10652 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
3 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
3 ms |
10588 KB |
Output is correct |
34 |
Correct |
4 ms |
10588 KB |
Output is correct |
35 |
Correct |
56 ms |
16196 KB |
Output is correct |
36 |
Correct |
71 ms |
18516 KB |
Output is correct |
37 |
Correct |
71 ms |
18492 KB |
Output is correct |
38 |
Correct |
70 ms |
18260 KB |
Output is correct |
39 |
Correct |
4 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
5 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10584 KB |
Output is correct |
44 |
Correct |
4 ms |
10648 KB |
Output is correct |
45 |
Correct |
5 ms |
10588 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
6 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10588 KB |
Output is correct |
49 |
Correct |
9 ms |
11356 KB |
Output is correct |
50 |
Correct |
73 ms |
18496 KB |
Output is correct |
51 |
Correct |
6 ms |
10840 KB |
Output is correct |
52 |
Correct |
78 ms |
17420 KB |
Output is correct |
53 |
Correct |
8 ms |
11356 KB |
Output is correct |
54 |
Correct |
66 ms |
18000 KB |
Output is correct |
55 |
Correct |
5 ms |
10840 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11100 KB |
Output is correct |
58 |
Correct |
5 ms |
11100 KB |
Output is correct |
59 |
Correct |
4 ms |
10688 KB |
Output is correct |
60 |
Correct |
56 ms |
15444 KB |
Output is correct |
61 |
Correct |
4 ms |
10588 KB |
Output is correct |
62 |
Correct |
70 ms |
18256 KB |
Output is correct |
63 |
Correct |
237 ms |
44016 KB |
Output is correct |
64 |
Correct |
242 ms |
44400 KB |
Output is correct |
65 |
Correct |
239 ms |
44112 KB |
Output is correct |
66 |
Correct |
61 ms |
14800 KB |
Output is correct |
67 |
Correct |
105 ms |
18880 KB |
Output is correct |
68 |
Correct |
61 ms |
14928 KB |
Output is correct |
69 |
Correct |
133 ms |
19540 KB |
Output is correct |
70 |
Correct |
64 ms |
14788 KB |
Output is correct |
71 |
Correct |
64 ms |
15060 KB |
Output is correct |
72 |
Correct |
117 ms |
19028 KB |
Output is correct |
73 |
Correct |
118 ms |
18900 KB |
Output is correct |
74 |
Correct |
400 ms |
29776 KB |
Output is correct |
75 |
Correct |
199 ms |
24296 KB |
Output is correct |
76 |
Correct |
293 ms |
27460 KB |
Output is correct |
77 |
Correct |
274 ms |
27432 KB |
Output is correct |
78 |
Correct |
108 ms |
18536 KB |
Output is correct |
79 |
Correct |
158 ms |
22072 KB |
Output is correct |
80 |
Correct |
96 ms |
18488 KB |
Output is correct |
81 |
Correct |
169 ms |
21812 KB |
Output is correct |
82 |
Correct |
294 ms |
33020 KB |
Output is correct |
83 |
Correct |
288 ms |
32804 KB |
Output is correct |
84 |
Correct |
258 ms |
33360 KB |
Output is correct |
85 |
Correct |
300 ms |
33620 KB |
Output is correct |
86 |
Correct |
69 ms |
14164 KB |
Output is correct |
87 |
Correct |
92 ms |
16476 KB |
Output is correct |
88 |
Correct |
156 ms |
19024 KB |
Output is correct |
89 |
Correct |
368 ms |
26892 KB |
Output is correct |