#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#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 |
11052 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 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 |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10436 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
11096 KB |
Output is correct |
23 |
Correct |
3 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 |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10424 KB |
Output is correct |
30 |
Correct |
3 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 |
11052 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 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 |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10436 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
11096 KB |
Output is correct |
23 |
Correct |
3 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 |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10424 KB |
Output is correct |
30 |
Correct |
3 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 |
5 ms |
10588 KB |
Output is correct |
35 |
Correct |
55 ms |
13768 KB |
Output is correct |
36 |
Correct |
73 ms |
15712 KB |
Output is correct |
37 |
Correct |
71 ms |
15788 KB |
Output is correct |
38 |
Correct |
69 ms |
15444 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
4 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10428 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10588 KB |
Output is correct |
45 |
Correct |
4 ms |
10584 KB |
Output is correct |
46 |
Correct |
3 ms |
10584 KB |
Output is correct |
47 |
Correct |
4 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10668 KB |
Output is correct |
49 |
Correct |
8 ms |
11100 KB |
Output is correct |
50 |
Correct |
72 ms |
15844 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
62 ms |
14420 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
66 ms |
15280 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 |
10844 KB |
Output is correct |
58 |
Correct |
5 ms |
11028 KB |
Output is correct |
59 |
Correct |
4 ms |
10588 KB |
Output is correct |
60 |
Correct |
55 ms |
12880 KB |
Output is correct |
61 |
Correct |
6 ms |
10588 KB |
Output is correct |
62 |
Correct |
68 ms |
15336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11052 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 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 |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10584 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10436 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
11096 KB |
Output is correct |
23 |
Correct |
3 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 |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10424 KB |
Output is correct |
30 |
Correct |
3 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 |
5 ms |
10588 KB |
Output is correct |
35 |
Correct |
55 ms |
13768 KB |
Output is correct |
36 |
Correct |
73 ms |
15712 KB |
Output is correct |
37 |
Correct |
71 ms |
15788 KB |
Output is correct |
38 |
Correct |
69 ms |
15444 KB |
Output is correct |
39 |
Correct |
3 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10588 KB |
Output is correct |
41 |
Correct |
4 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10428 KB |
Output is correct |
43 |
Correct |
4 ms |
10588 KB |
Output is correct |
44 |
Correct |
4 ms |
10588 KB |
Output is correct |
45 |
Correct |
4 ms |
10584 KB |
Output is correct |
46 |
Correct |
3 ms |
10584 KB |
Output is correct |
47 |
Correct |
4 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10668 KB |
Output is correct |
49 |
Correct |
8 ms |
11100 KB |
Output is correct |
50 |
Correct |
72 ms |
15844 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
62 ms |
14420 KB |
Output is correct |
53 |
Correct |
8 ms |
11100 KB |
Output is correct |
54 |
Correct |
66 ms |
15280 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 |
10844 KB |
Output is correct |
58 |
Correct |
5 ms |
11028 KB |
Output is correct |
59 |
Correct |
4 ms |
10588 KB |
Output is correct |
60 |
Correct |
55 ms |
12880 KB |
Output is correct |
61 |
Correct |
6 ms |
10588 KB |
Output is correct |
62 |
Correct |
68 ms |
15336 KB |
Output is correct |
63 |
Correct |
230 ms |
40740 KB |
Output is correct |
64 |
Correct |
230 ms |
40928 KB |
Output is correct |
65 |
Correct |
232 ms |
40548 KB |
Output is correct |
66 |
Correct |
59 ms |
12624 KB |
Output is correct |
67 |
Correct |
90 ms |
16468 KB |
Output is correct |
68 |
Correct |
56 ms |
12628 KB |
Output is correct |
69 |
Correct |
132 ms |
17236 KB |
Output is correct |
70 |
Correct |
56 ms |
12720 KB |
Output is correct |
71 |
Correct |
56 ms |
12556 KB |
Output is correct |
72 |
Correct |
115 ms |
16720 KB |
Output is correct |
73 |
Correct |
124 ms |
16692 KB |
Output is correct |
74 |
Correct |
366 ms |
26068 KB |
Output is correct |
75 |
Correct |
200 ms |
20904 KB |
Output is correct |
76 |
Correct |
264 ms |
23800 KB |
Output is correct |
77 |
Correct |
264 ms |
23968 KB |
Output is correct |
78 |
Correct |
87 ms |
16688 KB |
Output is correct |
79 |
Correct |
168 ms |
18736 KB |
Output is correct |
80 |
Correct |
96 ms |
16692 KB |
Output is correct |
81 |
Correct |
151 ms |
18456 KB |
Output is correct |
82 |
Correct |
280 ms |
30548 KB |
Output is correct |
83 |
Correct |
270 ms |
30708 KB |
Output is correct |
84 |
Correct |
246 ms |
31056 KB |
Output is correct |
85 |
Correct |
256 ms |
31060 KB |
Output is correct |
86 |
Correct |
62 ms |
11860 KB |
Output is correct |
87 |
Correct |
76 ms |
12964 KB |
Output is correct |
88 |
Correct |
127 ms |
16800 KB |
Output is correct |
89 |
Correct |
256 ms |
23208 KB |
Output is correct |