#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e6 + 5;
ll nc2(ll x) {
return x*(x-1)/2;
}
int n, q;
ll ans = 0;
int par[maxn], siz[maxn];
int parent(int x) {
return par[x]==x?x:par[x]=parent(par[x]);
}
set<int> in[maxn]; //in[par]={node..}
set<pair<int,int>> out[maxn]; //out[par]={(par,node)..}
void join(int u, int v) {
int x = parent(u);
int y = parent(v);
if (x==y) return;
auto iter = out[y].lower_bound({x,0});
if (iter!=out[y].end() && iter->first==x) {
if (in[x].size()+out[x].size()<in[y].size()+out[y].size()) {
swap(x,y);
}
vector<pair<int,int>> vo;
for (auto p: out[y]) {
ans -= siz[p.first];
in[p.first].erase(p.second);
if (p.first != x) vo.push_back(p);
}
out[y].clear();
ans += 1ll*siz[y]*(2*siz[x] + in[x].size() - in[y].size());
// internal into x -> y remove stuff pointing at y
vector<int> vi;
for (int i: in[y]) {
int pi = parent(i);
out[pi].erase({y,i});
if (pi != x) vi.push_back(i);
}
in[y].clear();
par[y]=x;
siz[x] += siz[y];
for (int i: vi) join(i,x);
for (auto p: vo) join(p.second,p.first);
} else if (!in[y].count(u)) {
ans += siz[y];
in[y].insert(u);
out[x].insert({y,u});
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>q;
for (int i=0; i<n; i++) {
par[i]=i;
siz[i]=1;
}
while (q--) {
int u,v;
cin>>u>>v;
--u; --v;
join(u,v);
cout<<ans<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
94316 KB |
Output is correct |
2 |
Correct |
63 ms |
94316 KB |
Output is correct |
3 |
Correct |
63 ms |
94316 KB |
Output is correct |
4 |
Correct |
62 ms |
94444 KB |
Output is correct |
5 |
Correct |
65 ms |
94316 KB |
Output is correct |
6 |
Correct |
62 ms |
94316 KB |
Output is correct |
7 |
Correct |
68 ms |
94316 KB |
Output is correct |
8 |
Correct |
64 ms |
94316 KB |
Output is correct |
9 |
Correct |
62 ms |
94316 KB |
Output is correct |
10 |
Correct |
62 ms |
94316 KB |
Output is correct |
11 |
Correct |
63 ms |
94316 KB |
Output is correct |
12 |
Correct |
63 ms |
94316 KB |
Output is correct |
13 |
Correct |
65 ms |
94316 KB |
Output is correct |
14 |
Correct |
62 ms |
94316 KB |
Output is correct |
15 |
Correct |
63 ms |
94316 KB |
Output is correct |
16 |
Correct |
63 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
62 ms |
94316 KB |
Output is correct |
19 |
Correct |
62 ms |
94316 KB |
Output is correct |
20 |
Correct |
62 ms |
94316 KB |
Output is correct |
21 |
Correct |
64 ms |
94316 KB |
Output is correct |
22 |
Correct |
63 ms |
94316 KB |
Output is correct |
23 |
Correct |
63 ms |
94316 KB |
Output is correct |
24 |
Correct |
62 ms |
94316 KB |
Output is correct |
25 |
Correct |
62 ms |
94316 KB |
Output is correct |
26 |
Correct |
63 ms |
94336 KB |
Output is correct |
27 |
Correct |
64 ms |
94316 KB |
Output is correct |
28 |
Correct |
62 ms |
94316 KB |
Output is correct |
29 |
Correct |
62 ms |
94316 KB |
Output is correct |
30 |
Correct |
63 ms |
94316 KB |
Output is correct |
31 |
Correct |
64 ms |
94316 KB |
Output is correct |
32 |
Correct |
62 ms |
94316 KB |
Output is correct |
33 |
Correct |
64 ms |
94316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
94316 KB |
Output is correct |
2 |
Correct |
63 ms |
94316 KB |
Output is correct |
3 |
Correct |
63 ms |
94316 KB |
Output is correct |
4 |
Correct |
62 ms |
94444 KB |
Output is correct |
5 |
Correct |
65 ms |
94316 KB |
Output is correct |
6 |
Correct |
62 ms |
94316 KB |
Output is correct |
7 |
Correct |
68 ms |
94316 KB |
Output is correct |
8 |
Correct |
64 ms |
94316 KB |
Output is correct |
9 |
Correct |
62 ms |
94316 KB |
Output is correct |
10 |
Correct |
62 ms |
94316 KB |
Output is correct |
11 |
Correct |
63 ms |
94316 KB |
Output is correct |
12 |
Correct |
63 ms |
94316 KB |
Output is correct |
13 |
Correct |
65 ms |
94316 KB |
Output is correct |
14 |
Correct |
62 ms |
94316 KB |
Output is correct |
15 |
Correct |
63 ms |
94316 KB |
Output is correct |
16 |
Correct |
63 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
62 ms |
94316 KB |
Output is correct |
19 |
Correct |
62 ms |
94316 KB |
Output is correct |
20 |
Correct |
62 ms |
94316 KB |
Output is correct |
21 |
Correct |
64 ms |
94316 KB |
Output is correct |
22 |
Correct |
63 ms |
94316 KB |
Output is correct |
23 |
Correct |
63 ms |
94316 KB |
Output is correct |
24 |
Correct |
62 ms |
94316 KB |
Output is correct |
25 |
Correct |
62 ms |
94316 KB |
Output is correct |
26 |
Correct |
63 ms |
94336 KB |
Output is correct |
27 |
Correct |
64 ms |
94316 KB |
Output is correct |
28 |
Correct |
62 ms |
94316 KB |
Output is correct |
29 |
Correct |
62 ms |
94316 KB |
Output is correct |
30 |
Correct |
63 ms |
94316 KB |
Output is correct |
31 |
Correct |
64 ms |
94316 KB |
Output is correct |
32 |
Correct |
62 ms |
94316 KB |
Output is correct |
33 |
Correct |
64 ms |
94316 KB |
Output is correct |
34 |
Correct |
75 ms |
94444 KB |
Output is correct |
35 |
Correct |
147 ms |
99692 KB |
Output is correct |
36 |
Correct |
167 ms |
101868 KB |
Output is correct |
37 |
Correct |
166 ms |
101868 KB |
Output is correct |
38 |
Correct |
167 ms |
101752 KB |
Output is correct |
39 |
Correct |
63 ms |
94444 KB |
Output is correct |
40 |
Correct |
64 ms |
94444 KB |
Output is correct |
41 |
Correct |
66 ms |
94444 KB |
Output is correct |
42 |
Correct |
64 ms |
94444 KB |
Output is correct |
43 |
Correct |
68 ms |
94572 KB |
Output is correct |
44 |
Correct |
65 ms |
94444 KB |
Output is correct |
45 |
Correct |
65 ms |
94444 KB |
Output is correct |
46 |
Correct |
64 ms |
94444 KB |
Output is correct |
47 |
Correct |
66 ms |
94444 KB |
Output is correct |
48 |
Correct |
65 ms |
94572 KB |
Output is correct |
49 |
Correct |
71 ms |
95084 KB |
Output is correct |
50 |
Correct |
170 ms |
101996 KB |
Output is correct |
51 |
Correct |
68 ms |
94700 KB |
Output is correct |
52 |
Correct |
158 ms |
100844 KB |
Output is correct |
53 |
Correct |
70 ms |
94956 KB |
Output is correct |
54 |
Correct |
163 ms |
101420 KB |
Output is correct |
55 |
Correct |
67 ms |
94828 KB |
Output is correct |
56 |
Correct |
66 ms |
94700 KB |
Output is correct |
57 |
Correct |
66 ms |
94828 KB |
Output is correct |
58 |
Correct |
66 ms |
94956 KB |
Output is correct |
59 |
Correct |
64 ms |
94444 KB |
Output is correct |
60 |
Correct |
148 ms |
99308 KB |
Output is correct |
61 |
Correct |
66 ms |
94444 KB |
Output is correct |
62 |
Correct |
162 ms |
101484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
94316 KB |
Output is correct |
2 |
Correct |
63 ms |
94316 KB |
Output is correct |
3 |
Correct |
63 ms |
94316 KB |
Output is correct |
4 |
Correct |
62 ms |
94444 KB |
Output is correct |
5 |
Correct |
65 ms |
94316 KB |
Output is correct |
6 |
Correct |
62 ms |
94316 KB |
Output is correct |
7 |
Correct |
68 ms |
94316 KB |
Output is correct |
8 |
Correct |
64 ms |
94316 KB |
Output is correct |
9 |
Correct |
62 ms |
94316 KB |
Output is correct |
10 |
Correct |
62 ms |
94316 KB |
Output is correct |
11 |
Correct |
63 ms |
94316 KB |
Output is correct |
12 |
Correct |
63 ms |
94316 KB |
Output is correct |
13 |
Correct |
65 ms |
94316 KB |
Output is correct |
14 |
Correct |
62 ms |
94316 KB |
Output is correct |
15 |
Correct |
63 ms |
94316 KB |
Output is correct |
16 |
Correct |
63 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
62 ms |
94316 KB |
Output is correct |
19 |
Correct |
62 ms |
94316 KB |
Output is correct |
20 |
Correct |
62 ms |
94316 KB |
Output is correct |
21 |
Correct |
64 ms |
94316 KB |
Output is correct |
22 |
Correct |
63 ms |
94316 KB |
Output is correct |
23 |
Correct |
63 ms |
94316 KB |
Output is correct |
24 |
Correct |
62 ms |
94316 KB |
Output is correct |
25 |
Correct |
62 ms |
94316 KB |
Output is correct |
26 |
Correct |
63 ms |
94336 KB |
Output is correct |
27 |
Correct |
64 ms |
94316 KB |
Output is correct |
28 |
Correct |
62 ms |
94316 KB |
Output is correct |
29 |
Correct |
62 ms |
94316 KB |
Output is correct |
30 |
Correct |
63 ms |
94316 KB |
Output is correct |
31 |
Correct |
64 ms |
94316 KB |
Output is correct |
32 |
Correct |
62 ms |
94316 KB |
Output is correct |
33 |
Correct |
64 ms |
94316 KB |
Output is correct |
34 |
Correct |
75 ms |
94444 KB |
Output is correct |
35 |
Correct |
147 ms |
99692 KB |
Output is correct |
36 |
Correct |
167 ms |
101868 KB |
Output is correct |
37 |
Correct |
166 ms |
101868 KB |
Output is correct |
38 |
Correct |
167 ms |
101752 KB |
Output is correct |
39 |
Correct |
63 ms |
94444 KB |
Output is correct |
40 |
Correct |
64 ms |
94444 KB |
Output is correct |
41 |
Correct |
66 ms |
94444 KB |
Output is correct |
42 |
Correct |
64 ms |
94444 KB |
Output is correct |
43 |
Correct |
68 ms |
94572 KB |
Output is correct |
44 |
Correct |
65 ms |
94444 KB |
Output is correct |
45 |
Correct |
65 ms |
94444 KB |
Output is correct |
46 |
Correct |
64 ms |
94444 KB |
Output is correct |
47 |
Correct |
66 ms |
94444 KB |
Output is correct |
48 |
Correct |
65 ms |
94572 KB |
Output is correct |
49 |
Correct |
71 ms |
95084 KB |
Output is correct |
50 |
Correct |
170 ms |
101996 KB |
Output is correct |
51 |
Correct |
68 ms |
94700 KB |
Output is correct |
52 |
Correct |
158 ms |
100844 KB |
Output is correct |
53 |
Correct |
70 ms |
94956 KB |
Output is correct |
54 |
Correct |
163 ms |
101420 KB |
Output is correct |
55 |
Correct |
67 ms |
94828 KB |
Output is correct |
56 |
Correct |
66 ms |
94700 KB |
Output is correct |
57 |
Correct |
66 ms |
94828 KB |
Output is correct |
58 |
Correct |
66 ms |
94956 KB |
Output is correct |
59 |
Correct |
64 ms |
94444 KB |
Output is correct |
60 |
Correct |
148 ms |
99308 KB |
Output is correct |
61 |
Correct |
66 ms |
94444 KB |
Output is correct |
62 |
Correct |
162 ms |
101484 KB |
Output is correct |
63 |
Correct |
465 ms |
128876 KB |
Output is correct |
64 |
Correct |
469 ms |
128876 KB |
Output is correct |
65 |
Correct |
468 ms |
128748 KB |
Output is correct |
66 |
Correct |
158 ms |
99636 KB |
Output is correct |
67 |
Correct |
234 ms |
103404 KB |
Output is correct |
68 |
Correct |
158 ms |
99564 KB |
Output is correct |
69 |
Correct |
305 ms |
104320 KB |
Output is correct |
70 |
Correct |
171 ms |
99436 KB |
Output is correct |
71 |
Correct |
166 ms |
99436 KB |
Output is correct |
72 |
Correct |
272 ms |
103532 KB |
Output is correct |
73 |
Correct |
269 ms |
103660 KB |
Output is correct |
74 |
Correct |
753 ms |
112828 KB |
Output is correct |
75 |
Correct |
471 ms |
109036 KB |
Output is correct |
76 |
Correct |
564 ms |
111724 KB |
Output is correct |
77 |
Correct |
563 ms |
111852 KB |
Output is correct |
78 |
Correct |
237 ms |
103020 KB |
Output is correct |
79 |
Correct |
347 ms |
106732 KB |
Output is correct |
80 |
Correct |
212 ms |
103020 KB |
Output is correct |
81 |
Correct |
310 ms |
106220 KB |
Output is correct |
82 |
Correct |
556 ms |
117548 KB |
Output is correct |
83 |
Correct |
539 ms |
117596 KB |
Output is correct |
84 |
Correct |
491 ms |
117996 KB |
Output is correct |
85 |
Correct |
494 ms |
117868 KB |
Output is correct |
86 |
Correct |
165 ms |
98668 KB |
Output is correct |
87 |
Correct |
194 ms |
100972 KB |
Output is correct |
88 |
Correct |
281 ms |
103660 KB |
Output is correct |
89 |
Correct |
552 ms |
111468 KB |
Output is correct |