#include<bits/stdc++.h>
using namespace std;
int n, m, i, j, k, a, b, sz[100010], par[100010];
set<int>in[100010];
set<pair<int, int> >out[100010];
long long ans;
int Find(int v){return v==par[v]?v:par[v]=Find(par[v]);}
void f(int a, int b)
{
//printf("%d %d!!\n", a, b);
int x=Find(a), y=Find(b);
//printf("%d %d %d %d!\n", x, y, a, b);
if(x==y)return;
auto it=out[y].lower_bound({x, -2e9});
vector<pair<int, int> >V;//recursion
if(it!=out[y].end()&&(*it).first==x)
{
if(in[x].size()+out[x].size()<in[y].size()+out[y].size())swap(x, y);
for(auto p:out[y])
{
in[p.first].erase(p.second);//cout<<sz[p.first]<<"!!!";
if(p.first!=x)V.push_back({p.second, p.first});
ans-=sz[p.first];
}//cout<<ans;
int prev=sz[x];//printf("%d %d\n", sz[x], sz[y]);
ans+=2LL*sz[x]*sz[y];
//printf("%lld\n", ans);
ans-=1LL*sz[y]*(int)in[y].size();
//printf("%lld\n", ans);
sz[x]+=sz[y];
ans+=1LL*(sz[x]-prev)*(int)in[x].size();
//printf("%lld!\n", ans);
for(auto p:in[y])
{
out[Find(p)].erase({y, p});
//out[Find(p)].insert({x, p});
V.push_back({p, x});
}
par[y]=x;
for(auto p:V)f(p.first, p.second);
}
else
{
if(in[y].find(a)==in[y].end())
{
out[x].insert({y, a});
in[y].insert(a);
ans+=sz[y];
//printf("%lld\n", ans);
}
}
}
main()
{
scanf("%d %d", &n, &m);
for(i=0;i++<n;)sz[i]=1,par[i]=i;
while(m--)
{
scanf("%d %d", &a, &b);
f(a, b);
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main()
| ^~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
6 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9672 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9604 KB |
Output is correct |
16 |
Correct |
5 ms |
9648 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9632 KB |
Output is correct |
20 |
Correct |
6 ms |
9700 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9676 KB |
Output is correct |
23 |
Correct |
6 ms |
9620 KB |
Output is correct |
24 |
Correct |
5 ms |
9676 KB |
Output is correct |
25 |
Correct |
5 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9696 KB |
Output is correct |
27 |
Correct |
5 ms |
9672 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
5 ms |
9692 KB |
Output is correct |
30 |
Correct |
5 ms |
9676 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
6 ms |
9804 KB |
Output is correct |
33 |
Correct |
6 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
6 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9672 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9604 KB |
Output is correct |
16 |
Correct |
5 ms |
9648 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9632 KB |
Output is correct |
20 |
Correct |
6 ms |
9700 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9676 KB |
Output is correct |
23 |
Correct |
6 ms |
9620 KB |
Output is correct |
24 |
Correct |
5 ms |
9676 KB |
Output is correct |
25 |
Correct |
5 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9696 KB |
Output is correct |
27 |
Correct |
5 ms |
9672 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
5 ms |
9692 KB |
Output is correct |
30 |
Correct |
5 ms |
9676 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
6 ms |
9804 KB |
Output is correct |
33 |
Correct |
6 ms |
9708 KB |
Output is correct |
34 |
Correct |
8 ms |
9804 KB |
Output is correct |
35 |
Correct |
100 ms |
15172 KB |
Output is correct |
36 |
Correct |
114 ms |
17536 KB |
Output is correct |
37 |
Correct |
111 ms |
17624 KB |
Output is correct |
38 |
Correct |
110 ms |
17224 KB |
Output is correct |
39 |
Correct |
6 ms |
9804 KB |
Output is correct |
40 |
Correct |
7 ms |
9832 KB |
Output is correct |
41 |
Correct |
7 ms |
9848 KB |
Output is correct |
42 |
Correct |
7 ms |
9804 KB |
Output is correct |
43 |
Correct |
7 ms |
9836 KB |
Output is correct |
44 |
Correct |
7 ms |
9804 KB |
Output is correct |
45 |
Correct |
7 ms |
9804 KB |
Output is correct |
46 |
Correct |
7 ms |
9796 KB |
Output is correct |
47 |
Correct |
7 ms |
9804 KB |
Output is correct |
48 |
Correct |
8 ms |
9836 KB |
Output is correct |
49 |
Correct |
13 ms |
10572 KB |
Output is correct |
50 |
Correct |
122 ms |
17812 KB |
Output is correct |
51 |
Correct |
11 ms |
10060 KB |
Output is correct |
52 |
Correct |
106 ms |
16240 KB |
Output is correct |
53 |
Correct |
15 ms |
10420 KB |
Output is correct |
54 |
Correct |
106 ms |
16956 KB |
Output is correct |
55 |
Correct |
8 ms |
10212 KB |
Output is correct |
56 |
Correct |
10 ms |
10072 KB |
Output is correct |
57 |
Correct |
8 ms |
10080 KB |
Output is correct |
58 |
Correct |
9 ms |
10088 KB |
Output is correct |
59 |
Correct |
6 ms |
9804 KB |
Output is correct |
60 |
Correct |
113 ms |
14816 KB |
Output is correct |
61 |
Correct |
7 ms |
9952 KB |
Output is correct |
62 |
Correct |
119 ms |
17232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
6 ms |
9696 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9672 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9604 KB |
Output is correct |
16 |
Correct |
5 ms |
9648 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9632 KB |
Output is correct |
20 |
Correct |
6 ms |
9700 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
5 ms |
9676 KB |
Output is correct |
23 |
Correct |
6 ms |
9620 KB |
Output is correct |
24 |
Correct |
5 ms |
9676 KB |
Output is correct |
25 |
Correct |
5 ms |
9676 KB |
Output is correct |
26 |
Correct |
5 ms |
9696 KB |
Output is correct |
27 |
Correct |
5 ms |
9672 KB |
Output is correct |
28 |
Correct |
5 ms |
9676 KB |
Output is correct |
29 |
Correct |
5 ms |
9692 KB |
Output is correct |
30 |
Correct |
5 ms |
9676 KB |
Output is correct |
31 |
Correct |
5 ms |
9676 KB |
Output is correct |
32 |
Correct |
6 ms |
9804 KB |
Output is correct |
33 |
Correct |
6 ms |
9708 KB |
Output is correct |
34 |
Correct |
8 ms |
9804 KB |
Output is correct |
35 |
Correct |
100 ms |
15172 KB |
Output is correct |
36 |
Correct |
114 ms |
17536 KB |
Output is correct |
37 |
Correct |
111 ms |
17624 KB |
Output is correct |
38 |
Correct |
110 ms |
17224 KB |
Output is correct |
39 |
Correct |
6 ms |
9804 KB |
Output is correct |
40 |
Correct |
7 ms |
9832 KB |
Output is correct |
41 |
Correct |
7 ms |
9848 KB |
Output is correct |
42 |
Correct |
7 ms |
9804 KB |
Output is correct |
43 |
Correct |
7 ms |
9836 KB |
Output is correct |
44 |
Correct |
7 ms |
9804 KB |
Output is correct |
45 |
Correct |
7 ms |
9804 KB |
Output is correct |
46 |
Correct |
7 ms |
9796 KB |
Output is correct |
47 |
Correct |
7 ms |
9804 KB |
Output is correct |
48 |
Correct |
8 ms |
9836 KB |
Output is correct |
49 |
Correct |
13 ms |
10572 KB |
Output is correct |
50 |
Correct |
122 ms |
17812 KB |
Output is correct |
51 |
Correct |
11 ms |
10060 KB |
Output is correct |
52 |
Correct |
106 ms |
16240 KB |
Output is correct |
53 |
Correct |
15 ms |
10420 KB |
Output is correct |
54 |
Correct |
106 ms |
16956 KB |
Output is correct |
55 |
Correct |
8 ms |
10212 KB |
Output is correct |
56 |
Correct |
10 ms |
10072 KB |
Output is correct |
57 |
Correct |
8 ms |
10080 KB |
Output is correct |
58 |
Correct |
9 ms |
10088 KB |
Output is correct |
59 |
Correct |
6 ms |
9804 KB |
Output is correct |
60 |
Correct |
113 ms |
14816 KB |
Output is correct |
61 |
Correct |
7 ms |
9952 KB |
Output is correct |
62 |
Correct |
119 ms |
17232 KB |
Output is correct |
63 |
Correct |
331 ms |
44096 KB |
Output is correct |
64 |
Correct |
347 ms |
44136 KB |
Output is correct |
65 |
Correct |
307 ms |
44020 KB |
Output is correct |
66 |
Correct |
97 ms |
19556 KB |
Output is correct |
67 |
Correct |
172 ms |
21736 KB |
Output is correct |
68 |
Correct |
95 ms |
19516 KB |
Output is correct |
69 |
Correct |
209 ms |
21020 KB |
Output is correct |
70 |
Correct |
104 ms |
19528 KB |
Output is correct |
71 |
Correct |
109 ms |
19480 KB |
Output is correct |
72 |
Correct |
196 ms |
22008 KB |
Output is correct |
73 |
Correct |
210 ms |
22200 KB |
Output is correct |
74 |
Correct |
601 ms |
43036 KB |
Output is correct |
75 |
Correct |
330 ms |
26644 KB |
Output is correct |
76 |
Correct |
506 ms |
33008 KB |
Output is correct |
77 |
Correct |
474 ms |
33148 KB |
Output is correct |
78 |
Correct |
143 ms |
21888 KB |
Output is correct |
79 |
Correct |
241 ms |
26812 KB |
Output is correct |
80 |
Correct |
128 ms |
21884 KB |
Output is correct |
81 |
Correct |
243 ms |
25304 KB |
Output is correct |
82 |
Correct |
402 ms |
34004 KB |
Output is correct |
83 |
Correct |
450 ms |
33876 KB |
Output is correct |
84 |
Correct |
356 ms |
33768 KB |
Output is correct |
85 |
Correct |
371 ms |
33856 KB |
Output is correct |
86 |
Correct |
114 ms |
18732 KB |
Output is correct |
87 |
Correct |
163 ms |
20988 KB |
Output is correct |
88 |
Correct |
217 ms |
22264 KB |
Output is correct |
89 |
Correct |
472 ms |
32196 KB |
Output is correct |