#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);
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];
for(auto x:allq){
add(x.first,x.second);
}
}
void add(int u,int v){
int rootv=root(v),rootu=root(u);
if(rootu==rootv||vorod[rootv].count(u)!=0){
return ;
}
auto x=kho[rootv].lower_bound(make_pair(rootu,-1));
if(x!=kho[rootv].end()&&(*x).first==rootu){
return con(u,v);
}
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 |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10420 KB |
Output is correct |
9 |
Correct |
3 ms |
10476 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 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 |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10432 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10616 KB |
Output is correct |
21 |
Correct |
3 ms |
10432 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
2 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 |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10420 KB |
Output is correct |
9 |
Correct |
3 ms |
10476 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 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 |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10432 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10616 KB |
Output is correct |
21 |
Correct |
3 ms |
10432 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
2 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 |
10584 KB |
Output is correct |
35 |
Correct |
56 ms |
13904 KB |
Output is correct |
36 |
Correct |
70 ms |
15700 KB |
Output is correct |
37 |
Correct |
71 ms |
15952 KB |
Output is correct |
38 |
Correct |
68 ms |
15608 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10596 KB |
Output is correct |
41 |
Correct |
4 ms |
10836 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10588 KB |
Output is correct |
45 |
Correct |
3 ms |
10432 KB |
Output is correct |
46 |
Correct |
3 ms |
10588 KB |
Output is correct |
47 |
Correct |
4 ms |
10584 KB |
Output is correct |
48 |
Correct |
4 ms |
10584 KB |
Output is correct |
49 |
Correct |
8 ms |
11096 KB |
Output is correct |
50 |
Correct |
71 ms |
15872 KB |
Output is correct |
51 |
Correct |
7 ms |
10844 KB |
Output is correct |
52 |
Correct |
62 ms |
14676 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
66 ms |
15436 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11076 KB |
Output is correct |
58 |
Correct |
5 ms |
11100 KB |
Output is correct |
59 |
Correct |
3 ms |
10428 KB |
Output is correct |
60 |
Correct |
54 ms |
12804 KB |
Output is correct |
61 |
Correct |
7 ms |
10588 KB |
Output is correct |
62 |
Correct |
68 ms |
15652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10420 KB |
Output is correct |
9 |
Correct |
3 ms |
10476 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 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 |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10432 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10616 KB |
Output is correct |
21 |
Correct |
3 ms |
10432 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10840 KB |
Output is correct |
29 |
Correct |
2 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 |
10584 KB |
Output is correct |
35 |
Correct |
56 ms |
13904 KB |
Output is correct |
36 |
Correct |
70 ms |
15700 KB |
Output is correct |
37 |
Correct |
71 ms |
15952 KB |
Output is correct |
38 |
Correct |
68 ms |
15608 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10596 KB |
Output is correct |
41 |
Correct |
4 ms |
10836 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10588 KB |
Output is correct |
45 |
Correct |
3 ms |
10432 KB |
Output is correct |
46 |
Correct |
3 ms |
10588 KB |
Output is correct |
47 |
Correct |
4 ms |
10584 KB |
Output is correct |
48 |
Correct |
4 ms |
10584 KB |
Output is correct |
49 |
Correct |
8 ms |
11096 KB |
Output is correct |
50 |
Correct |
71 ms |
15872 KB |
Output is correct |
51 |
Correct |
7 ms |
10844 KB |
Output is correct |
52 |
Correct |
62 ms |
14676 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
66 ms |
15436 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
5 ms |
10844 KB |
Output is correct |
57 |
Correct |
5 ms |
11076 KB |
Output is correct |
58 |
Correct |
5 ms |
11100 KB |
Output is correct |
59 |
Correct |
3 ms |
10428 KB |
Output is correct |
60 |
Correct |
54 ms |
12804 KB |
Output is correct |
61 |
Correct |
7 ms |
10588 KB |
Output is correct |
62 |
Correct |
68 ms |
15652 KB |
Output is correct |
63 |
Correct |
229 ms |
40772 KB |
Output is correct |
64 |
Correct |
236 ms |
40672 KB |
Output is correct |
65 |
Correct |
244 ms |
40584 KB |
Output is correct |
66 |
Correct |
61 ms |
12628 KB |
Output is correct |
67 |
Correct |
89 ms |
16476 KB |
Output is correct |
68 |
Correct |
62 ms |
12668 KB |
Output is correct |
69 |
Correct |
133 ms |
17232 KB |
Output is correct |
70 |
Correct |
56 ms |
12624 KB |
Output is correct |
71 |
Correct |
67 ms |
12996 KB |
Output is correct |
72 |
Correct |
113 ms |
16816 KB |
Output is correct |
73 |
Correct |
117 ms |
16720 KB |
Output is correct |
74 |
Correct |
400 ms |
26460 KB |
Output is correct |
75 |
Correct |
205 ms |
20900 KB |
Output is correct |
76 |
Correct |
303 ms |
23960 KB |
Output is correct |
77 |
Correct |
276 ms |
24144 KB |
Output is correct |
78 |
Correct |
93 ms |
16800 KB |
Output is correct |
79 |
Correct |
170 ms |
18740 KB |
Output is correct |
80 |
Correct |
93 ms |
16700 KB |
Output is correct |
81 |
Correct |
146 ms |
18344 KB |
Output is correct |
82 |
Correct |
286 ms |
30680 KB |
Output is correct |
83 |
Correct |
282 ms |
30504 KB |
Output is correct |
84 |
Correct |
269 ms |
31264 KB |
Output is correct |
85 |
Correct |
260 ms |
31180 KB |
Output is correct |
86 |
Correct |
57 ms |
11892 KB |
Output is correct |
87 |
Correct |
80 ms |
13032 KB |
Output is correct |
88 |
Correct |
125 ms |
16616 KB |
Output is correct |
89 |
Correct |
292 ms |
23232 KB |
Output is correct |