#include<bits/stdc++.h>
using namespace std;
int par[100100],sz[100100],ans;
set<int>adj[100100],radj[100100],st[100100];
queue<pair<int,int>> ops;
int abp(int a){
return par[a]?par[a]=abp(par[a]):a;
}
void weak(int a,int b){
adj[a].insert(b);
radj[b].insert(a);
if(adj[b].count(a))
ops.push({a,b});
}
inline int ssz(int x){
return st[x].size()+radj[x].size()+adj[x].size();
}
void merge(int a,int b){
if(a==b)return;
ans+=sz[a]*st[b].size()+sz[b]*st[a].size();
if(ssz(a)<ssz(b))swap(a,b);
par[b]=a;sz[a]+=sz[b];
for(auto i:st[b])
if(st[a].count(i))
ans-=sz[a];
else st[a].insert(i);
adj[a].erase(b);
adj[b].erase(a);
radj[a].erase(b);
radj[b].erase(a);
for(auto i:adj[b])
radj[i].erase(b),
weak(a,i);
for(auto i:radj[b])
adj[i].erase(b),
weak(i,a);
}
void dostuf(int a,int b){
b=abp(b);
if(abp(a)!=b&&!st[b].count(a)){
st[b].insert(a);
ans+=sz[b];
a=abp(a);
weak(a,b);
while(ops.size()){
auto[a,b]=ops.front();
ops.pop();
merge(abp(a),abp(b));
}
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
st[i].insert(i),sz[i]=1;
while(m--){
int a,b;
cin>>a>>b;
dostuf(a,b);
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15096 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
4 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
14928 KB |
Output is correct |
11 |
Correct |
4 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14772 KB |
Output is correct |
13 |
Correct |
3 ms |
14928 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
4 ms |
14928 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
3 ms |
14928 KB |
Output is correct |
19 |
Correct |
5 ms |
14928 KB |
Output is correct |
20 |
Correct |
4 ms |
14928 KB |
Output is correct |
21 |
Correct |
3 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14928 KB |
Output is correct |
24 |
Correct |
3 ms |
14928 KB |
Output is correct |
25 |
Correct |
3 ms |
14928 KB |
Output is correct |
26 |
Correct |
3 ms |
15152 KB |
Output is correct |
27 |
Correct |
3 ms |
14928 KB |
Output is correct |
28 |
Correct |
4 ms |
14928 KB |
Output is correct |
29 |
Correct |
4 ms |
14944 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14788 KB |
Output is correct |
33 |
Correct |
4 ms |
15096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15096 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
4 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
14928 KB |
Output is correct |
11 |
Correct |
4 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14772 KB |
Output is correct |
13 |
Correct |
3 ms |
14928 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
4 ms |
14928 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
3 ms |
14928 KB |
Output is correct |
19 |
Correct |
5 ms |
14928 KB |
Output is correct |
20 |
Correct |
4 ms |
14928 KB |
Output is correct |
21 |
Correct |
3 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14928 KB |
Output is correct |
24 |
Correct |
3 ms |
14928 KB |
Output is correct |
25 |
Correct |
3 ms |
14928 KB |
Output is correct |
26 |
Correct |
3 ms |
15152 KB |
Output is correct |
27 |
Correct |
3 ms |
14928 KB |
Output is correct |
28 |
Correct |
4 ms |
14928 KB |
Output is correct |
29 |
Correct |
4 ms |
14944 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14788 KB |
Output is correct |
33 |
Correct |
4 ms |
15096 KB |
Output is correct |
34 |
Correct |
5 ms |
14928 KB |
Output is correct |
35 |
Correct |
56 ms |
20784 KB |
Output is correct |
36 |
Correct |
85 ms |
23428 KB |
Output is correct |
37 |
Correct |
81 ms |
23624 KB |
Output is correct |
38 |
Correct |
75 ms |
23120 KB |
Output is correct |
39 |
Correct |
5 ms |
15184 KB |
Output is correct |
40 |
Correct |
6 ms |
15696 KB |
Output is correct |
41 |
Correct |
7 ms |
15696 KB |
Output is correct |
42 |
Correct |
5 ms |
15320 KB |
Output is correct |
43 |
Correct |
6 ms |
15184 KB |
Output is correct |
44 |
Correct |
6 ms |
15184 KB |
Output is correct |
45 |
Correct |
7 ms |
15184 KB |
Output is correct |
46 |
Correct |
5 ms |
15184 KB |
Output is correct |
47 |
Correct |
6 ms |
15440 KB |
Output is correct |
48 |
Correct |
6 ms |
15440 KB |
Output is correct |
49 |
Correct |
11 ms |
16120 KB |
Output is correct |
50 |
Correct |
75 ms |
23712 KB |
Output is correct |
51 |
Correct |
8 ms |
15440 KB |
Output is correct |
52 |
Correct |
63 ms |
22088 KB |
Output is correct |
53 |
Correct |
11 ms |
15952 KB |
Output is correct |
54 |
Correct |
74 ms |
22896 KB |
Output is correct |
55 |
Correct |
8 ms |
15696 KB |
Output is correct |
56 |
Correct |
7 ms |
15696 KB |
Output is correct |
57 |
Correct |
8 ms |
15696 KB |
Output is correct |
58 |
Correct |
9 ms |
15696 KB |
Output is correct |
59 |
Correct |
7 ms |
15696 KB |
Output is correct |
60 |
Correct |
55 ms |
20584 KB |
Output is correct |
61 |
Correct |
7 ms |
15440 KB |
Output is correct |
62 |
Correct |
77 ms |
23116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15096 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
14928 KB |
Output is correct |
6 |
Correct |
3 ms |
14928 KB |
Output is correct |
7 |
Correct |
4 ms |
14928 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
14928 KB |
Output is correct |
11 |
Correct |
4 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14772 KB |
Output is correct |
13 |
Correct |
3 ms |
14928 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
4 ms |
14928 KB |
Output is correct |
16 |
Correct |
4 ms |
14928 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
3 ms |
14928 KB |
Output is correct |
19 |
Correct |
5 ms |
14928 KB |
Output is correct |
20 |
Correct |
4 ms |
14928 KB |
Output is correct |
21 |
Correct |
3 ms |
14928 KB |
Output is correct |
22 |
Correct |
3 ms |
14928 KB |
Output is correct |
23 |
Correct |
4 ms |
14928 KB |
Output is correct |
24 |
Correct |
3 ms |
14928 KB |
Output is correct |
25 |
Correct |
3 ms |
14928 KB |
Output is correct |
26 |
Correct |
3 ms |
15152 KB |
Output is correct |
27 |
Correct |
3 ms |
14928 KB |
Output is correct |
28 |
Correct |
4 ms |
14928 KB |
Output is correct |
29 |
Correct |
4 ms |
14944 KB |
Output is correct |
30 |
Correct |
3 ms |
14928 KB |
Output is correct |
31 |
Correct |
4 ms |
14928 KB |
Output is correct |
32 |
Correct |
3 ms |
14788 KB |
Output is correct |
33 |
Correct |
4 ms |
15096 KB |
Output is correct |
34 |
Correct |
5 ms |
14928 KB |
Output is correct |
35 |
Correct |
56 ms |
20784 KB |
Output is correct |
36 |
Correct |
85 ms |
23428 KB |
Output is correct |
37 |
Correct |
81 ms |
23624 KB |
Output is correct |
38 |
Correct |
75 ms |
23120 KB |
Output is correct |
39 |
Correct |
5 ms |
15184 KB |
Output is correct |
40 |
Correct |
6 ms |
15696 KB |
Output is correct |
41 |
Correct |
7 ms |
15696 KB |
Output is correct |
42 |
Correct |
5 ms |
15320 KB |
Output is correct |
43 |
Correct |
6 ms |
15184 KB |
Output is correct |
44 |
Correct |
6 ms |
15184 KB |
Output is correct |
45 |
Correct |
7 ms |
15184 KB |
Output is correct |
46 |
Correct |
5 ms |
15184 KB |
Output is correct |
47 |
Correct |
6 ms |
15440 KB |
Output is correct |
48 |
Correct |
6 ms |
15440 KB |
Output is correct |
49 |
Correct |
11 ms |
16120 KB |
Output is correct |
50 |
Correct |
75 ms |
23712 KB |
Output is correct |
51 |
Correct |
8 ms |
15440 KB |
Output is correct |
52 |
Correct |
63 ms |
22088 KB |
Output is correct |
53 |
Correct |
11 ms |
15952 KB |
Output is correct |
54 |
Correct |
74 ms |
22896 KB |
Output is correct |
55 |
Correct |
8 ms |
15696 KB |
Output is correct |
56 |
Correct |
7 ms |
15696 KB |
Output is correct |
57 |
Correct |
8 ms |
15696 KB |
Output is correct |
58 |
Correct |
9 ms |
15696 KB |
Output is correct |
59 |
Correct |
7 ms |
15696 KB |
Output is correct |
60 |
Correct |
55 ms |
20584 KB |
Output is correct |
61 |
Correct |
7 ms |
15440 KB |
Output is correct |
62 |
Correct |
77 ms |
23116 KB |
Output is correct |
63 |
Correct |
428 ms |
67400 KB |
Output is correct |
64 |
Correct |
392 ms |
67404 KB |
Output is correct |
65 |
Correct |
386 ms |
67400 KB |
Output is correct |
66 |
Incorrect |
161 ms |
33608 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |