#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=1e5+5;
int n,m,p[N],sz[N];
set<int>in[N];
set<pii>out[N];
long long res;
int Find(int x)
{
if(x==p[x]) return x;
return p[x]=Find(p[x]);
}
void Union(int u, int v)
{
int x=Find(u);
int y=Find(v);
if(x==y) return;
auto it=out[y].lower_bound(mp(x,0));
if(it!=out[y].end()&&(*it).fi==x)
{
if(in[x].size()+out[x].size()<in[y].size()+out[y].size()) swap(x,y);
vector<pii>vo;
for(auto&i:out[y])
{
res-=sz[i.fi];
in[i.fi].erase(i.se);
if(i.fi!=x) vo.push_back(i);
}
out[y].clear();
res+=1ll*sz[y]*(2*sz[x]+in[x].size()-in[y].size());
vector<int>vi;
for(auto&i:in[y])
{
int j=Find(i);
out[j].erase(mp(y,i));
if(j!=x) vi.push_back(i);
}
in[y].clear();
p[y]=x;
sz[x]+=sz[y];
for(auto&i:vi) Union(i,x);
for(auto&i:vo) Union(i.se,i.fi);
}
else if(!in[y].count(u))
{
res+=sz[y];
in[y].insert(u);
out[x].insert(mp(y,u));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
p[i]=i;
sz[i]=1;
}
while(m--)
{
int u,v;
cin>>u>>v;
Union(u,v);
cout<<res<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
8 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9856 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
8 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9856 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
34 |
Correct |
9 ms |
9856 KB |
Output is correct |
35 |
Correct |
88 ms |
12792 KB |
Output is correct |
36 |
Correct |
108 ms |
14712 KB |
Output is correct |
37 |
Correct |
108 ms |
14712 KB |
Output is correct |
38 |
Correct |
104 ms |
14456 KB |
Output is correct |
39 |
Correct |
8 ms |
9728 KB |
Output is correct |
40 |
Correct |
9 ms |
9856 KB |
Output is correct |
41 |
Correct |
9 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9728 KB |
Output is correct |
43 |
Correct |
10 ms |
9856 KB |
Output is correct |
44 |
Correct |
11 ms |
9856 KB |
Output is correct |
45 |
Correct |
8 ms |
9728 KB |
Output is correct |
46 |
Correct |
9 ms |
9728 KB |
Output is correct |
47 |
Correct |
9 ms |
9856 KB |
Output is correct |
48 |
Correct |
9 ms |
9856 KB |
Output is correct |
49 |
Correct |
15 ms |
10368 KB |
Output is correct |
50 |
Correct |
109 ms |
14800 KB |
Output is correct |
51 |
Correct |
14 ms |
10112 KB |
Output is correct |
52 |
Correct |
99 ms |
13688 KB |
Output is correct |
53 |
Correct |
15 ms |
10368 KB |
Output is correct |
54 |
Correct |
103 ms |
14328 KB |
Output is correct |
55 |
Correct |
12 ms |
10112 KB |
Output is correct |
56 |
Correct |
11 ms |
10112 KB |
Output is correct |
57 |
Correct |
11 ms |
10112 KB |
Output is correct |
58 |
Correct |
12 ms |
10240 KB |
Output is correct |
59 |
Correct |
10 ms |
9728 KB |
Output is correct |
60 |
Correct |
93 ms |
12280 KB |
Output is correct |
61 |
Correct |
10 ms |
9856 KB |
Output is correct |
62 |
Correct |
104 ms |
14312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
8 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
8 ms |
9856 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
7 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
34 |
Correct |
9 ms |
9856 KB |
Output is correct |
35 |
Correct |
88 ms |
12792 KB |
Output is correct |
36 |
Correct |
108 ms |
14712 KB |
Output is correct |
37 |
Correct |
108 ms |
14712 KB |
Output is correct |
38 |
Correct |
104 ms |
14456 KB |
Output is correct |
39 |
Correct |
8 ms |
9728 KB |
Output is correct |
40 |
Correct |
9 ms |
9856 KB |
Output is correct |
41 |
Correct |
9 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9728 KB |
Output is correct |
43 |
Correct |
10 ms |
9856 KB |
Output is correct |
44 |
Correct |
11 ms |
9856 KB |
Output is correct |
45 |
Correct |
8 ms |
9728 KB |
Output is correct |
46 |
Correct |
9 ms |
9728 KB |
Output is correct |
47 |
Correct |
9 ms |
9856 KB |
Output is correct |
48 |
Correct |
9 ms |
9856 KB |
Output is correct |
49 |
Correct |
15 ms |
10368 KB |
Output is correct |
50 |
Correct |
109 ms |
14800 KB |
Output is correct |
51 |
Correct |
14 ms |
10112 KB |
Output is correct |
52 |
Correct |
99 ms |
13688 KB |
Output is correct |
53 |
Correct |
15 ms |
10368 KB |
Output is correct |
54 |
Correct |
103 ms |
14328 KB |
Output is correct |
55 |
Correct |
12 ms |
10112 KB |
Output is correct |
56 |
Correct |
11 ms |
10112 KB |
Output is correct |
57 |
Correct |
11 ms |
10112 KB |
Output is correct |
58 |
Correct |
12 ms |
10240 KB |
Output is correct |
59 |
Correct |
10 ms |
9728 KB |
Output is correct |
60 |
Correct |
93 ms |
12280 KB |
Output is correct |
61 |
Correct |
10 ms |
9856 KB |
Output is correct |
62 |
Correct |
104 ms |
14312 KB |
Output is correct |
63 |
Correct |
418 ms |
40716 KB |
Output is correct |
64 |
Correct |
438 ms |
40696 KB |
Output is correct |
65 |
Correct |
418 ms |
40884 KB |
Output is correct |
66 |
Correct |
101 ms |
12664 KB |
Output is correct |
67 |
Correct |
188 ms |
16632 KB |
Output is correct |
68 |
Correct |
100 ms |
14200 KB |
Output is correct |
69 |
Correct |
276 ms |
19576 KB |
Output is correct |
70 |
Correct |
113 ms |
14972 KB |
Output is correct |
71 |
Correct |
115 ms |
15096 KB |
Output is correct |
72 |
Correct |
222 ms |
19064 KB |
Output is correct |
73 |
Correct |
226 ms |
19192 KB |
Output is correct |
74 |
Correct |
737 ms |
28116 KB |
Output is correct |
75 |
Correct |
422 ms |
24492 KB |
Output is correct |
76 |
Correct |
546 ms |
27256 KB |
Output is correct |
77 |
Correct |
557 ms |
27128 KB |
Output is correct |
78 |
Correct |
161 ms |
18424 KB |
Output is correct |
79 |
Correct |
296 ms |
22136 KB |
Output is correct |
80 |
Correct |
163 ms |
18552 KB |
Output is correct |
81 |
Correct |
270 ms |
21624 KB |
Output is correct |
82 |
Correct |
488 ms |
32888 KB |
Output is correct |
83 |
Correct |
520 ms |
33004 KB |
Output is correct |
84 |
Correct |
435 ms |
33144 KB |
Output is correct |
85 |
Correct |
438 ms |
33272 KB |
Output is correct |
86 |
Correct |
115 ms |
14200 KB |
Output is correct |
87 |
Correct |
140 ms |
16380 KB |
Output is correct |
88 |
Correct |
223 ms |
19068 KB |
Output is correct |
89 |
Correct |
513 ms |
26744 KB |
Output is correct |