# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5;
int t,n,a[N],p[N],m;
long long ans, sz[N];
set <int> g[N], rg[N],v[N],comp[N];
int get(int sz) {
return sz * (sz - 1);
}
int get_col(int a) {
if (a == p[a]) return a;
else return p[a] = get_col(p[a]);
}
void col(int a, int b) {
a = get_col(a); b = get_col(b);
if (a == b) return ;
if (comp[a].size() < comp[b].size()) swap(a,b);
ans -= get(sz[a]);
ans -= get(sz[b]);
ans -= v[a].size() * sz[a];
ans -= v[b].size() * sz[b];
ans += get(sz[a] + sz[b]);
for (int x : v[b]) {
if (get_col(x) != a) v[a].insert(x);
}
for (int x : comp[b]) {
if (v[a].count(x)) {
v[a].erase(x);
}
comp[a].insert(x); p[x] = a;
}
comp[b].clear();
ans += v[a].size() * (sz[a] + sz[b]);
v[b].clear();
p[b] = a;
sz[a] += sz[b]; sz[b] = 0;
vector <pii> vec;
for (int to : g[b]) {
rg[to].erase(b); if (get_col(to) != a) { rg[to].insert(a), g[a].insert(to); if (g[to].count(a)) vec.pb({to, a}); }
}
for (int to : rg[b]) {
g[to].erase(b); if (get_col(to) != a) { g[to].insert(a), rg[a].insert(to); if (g[a].count(to)) vec.pb({to, a}); }
}
for (pii sth : vec) {
col(sth.f, sth.s);
}
/*
while (s[x].lower_bound({y, 0}) != s[x].end()){
pii sth = *s[x].lowesr_bound({y, 0});
if (sth.f != y) break;
s[x].erase(sth);
}
while (s[y].lower_bound({x, 0}) != s[y].end()) {
pii sth = *s[y].lower_bound({x, 0});
if (sth.f != x) break;
s[y].erase(sth);
}
*/
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
for (int i = 1; i <= n; i++) {
p[i] = i; sz[i] = 1; comp[i].insert(i);
}
for (int i = 1; i <= m; i++) {
int a, b;
cin>>a>>b;
int x = get_col(a), y = get_col(b);
if (x == y) {
cout<<ans<<'\n';
continue;
}
if (v[y].count(a)) {
cout<<ans<<'\n';
continue;
}
g[x].insert(y); rg[y].insert(x); v[y].insert(a);
ans += sz[y];
if (g[x].count(y) && g[y].count(x)) {
col(x, y);
}
cout<<ans<<'\n';
}
}
Compilation message
joitter2.cpp:69:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
69 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56644 KB |
Output is correct |
3 |
Correct |
25 ms |
56700 KB |
Output is correct |
4 |
Correct |
28 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56688 KB |
Output is correct |
6 |
Correct |
31 ms |
56752 KB |
Output is correct |
7 |
Correct |
26 ms |
56692 KB |
Output is correct |
8 |
Correct |
27 ms |
56660 KB |
Output is correct |
9 |
Correct |
27 ms |
56700 KB |
Output is correct |
10 |
Correct |
25 ms |
56668 KB |
Output is correct |
11 |
Correct |
27 ms |
56716 KB |
Output is correct |
12 |
Correct |
25 ms |
56636 KB |
Output is correct |
13 |
Correct |
25 ms |
56584 KB |
Output is correct |
14 |
Correct |
25 ms |
56784 KB |
Output is correct |
15 |
Correct |
26 ms |
56648 KB |
Output is correct |
16 |
Correct |
26 ms |
56664 KB |
Output is correct |
17 |
Correct |
26 ms |
56660 KB |
Output is correct |
18 |
Correct |
28 ms |
56668 KB |
Output is correct |
19 |
Correct |
29 ms |
56604 KB |
Output is correct |
20 |
Correct |
26 ms |
56720 KB |
Output is correct |
21 |
Correct |
27 ms |
56660 KB |
Output is correct |
22 |
Correct |
26 ms |
56720 KB |
Output is correct |
23 |
Correct |
26 ms |
56684 KB |
Output is correct |
24 |
Correct |
26 ms |
56644 KB |
Output is correct |
25 |
Correct |
26 ms |
56652 KB |
Output is correct |
26 |
Correct |
27 ms |
56704 KB |
Output is correct |
27 |
Correct |
25 ms |
56708 KB |
Output is correct |
28 |
Correct |
27 ms |
56660 KB |
Output is correct |
29 |
Correct |
26 ms |
56696 KB |
Output is correct |
30 |
Correct |
28 ms |
56620 KB |
Output is correct |
31 |
Correct |
26 ms |
56644 KB |
Output is correct |
32 |
Correct |
28 ms |
56696 KB |
Output is correct |
33 |
Correct |
26 ms |
56748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56644 KB |
Output is correct |
3 |
Correct |
25 ms |
56700 KB |
Output is correct |
4 |
Correct |
28 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56688 KB |
Output is correct |
6 |
Correct |
31 ms |
56752 KB |
Output is correct |
7 |
Correct |
26 ms |
56692 KB |
Output is correct |
8 |
Correct |
27 ms |
56660 KB |
Output is correct |
9 |
Correct |
27 ms |
56700 KB |
Output is correct |
10 |
Correct |
25 ms |
56668 KB |
Output is correct |
11 |
Correct |
27 ms |
56716 KB |
Output is correct |
12 |
Correct |
25 ms |
56636 KB |
Output is correct |
13 |
Correct |
25 ms |
56584 KB |
Output is correct |
14 |
Correct |
25 ms |
56784 KB |
Output is correct |
15 |
Correct |
26 ms |
56648 KB |
Output is correct |
16 |
Correct |
26 ms |
56664 KB |
Output is correct |
17 |
Correct |
26 ms |
56660 KB |
Output is correct |
18 |
Correct |
28 ms |
56668 KB |
Output is correct |
19 |
Correct |
29 ms |
56604 KB |
Output is correct |
20 |
Correct |
26 ms |
56720 KB |
Output is correct |
21 |
Correct |
27 ms |
56660 KB |
Output is correct |
22 |
Correct |
26 ms |
56720 KB |
Output is correct |
23 |
Correct |
26 ms |
56684 KB |
Output is correct |
24 |
Correct |
26 ms |
56644 KB |
Output is correct |
25 |
Correct |
26 ms |
56652 KB |
Output is correct |
26 |
Correct |
27 ms |
56704 KB |
Output is correct |
27 |
Correct |
25 ms |
56708 KB |
Output is correct |
28 |
Correct |
27 ms |
56660 KB |
Output is correct |
29 |
Correct |
26 ms |
56696 KB |
Output is correct |
30 |
Correct |
28 ms |
56620 KB |
Output is correct |
31 |
Correct |
26 ms |
56644 KB |
Output is correct |
32 |
Correct |
28 ms |
56696 KB |
Output is correct |
33 |
Correct |
26 ms |
56748 KB |
Output is correct |
34 |
Correct |
30 ms |
56772 KB |
Output is correct |
35 |
Correct |
91 ms |
62804 KB |
Output is correct |
36 |
Correct |
116 ms |
65932 KB |
Output is correct |
37 |
Correct |
112 ms |
65960 KB |
Output is correct |
38 |
Correct |
111 ms |
65532 KB |
Output is correct |
39 |
Correct |
28 ms |
57044 KB |
Output is correct |
40 |
Correct |
31 ms |
57148 KB |
Output is correct |
41 |
Correct |
33 ms |
57116 KB |
Output is correct |
42 |
Correct |
28 ms |
57032 KB |
Output is correct |
43 |
Correct |
32 ms |
57012 KB |
Output is correct |
44 |
Correct |
30 ms |
57088 KB |
Output is correct |
45 |
Correct |
28 ms |
57036 KB |
Output is correct |
46 |
Correct |
29 ms |
56948 KB |
Output is correct |
47 |
Correct |
29 ms |
57064 KB |
Output is correct |
48 |
Correct |
29 ms |
57164 KB |
Output is correct |
49 |
Correct |
35 ms |
58060 KB |
Output is correct |
50 |
Correct |
119 ms |
66008 KB |
Output is correct |
51 |
Correct |
33 ms |
57300 KB |
Output is correct |
52 |
Correct |
100 ms |
64236 KB |
Output is correct |
53 |
Correct |
35 ms |
57976 KB |
Output is correct |
54 |
Correct |
103 ms |
65048 KB |
Output is correct |
55 |
Correct |
36 ms |
57420 KB |
Output is correct |
56 |
Correct |
31 ms |
57428 KB |
Output is correct |
57 |
Correct |
31 ms |
57764 KB |
Output is correct |
58 |
Correct |
33 ms |
57848 KB |
Output is correct |
59 |
Correct |
28 ms |
56992 KB |
Output is correct |
60 |
Correct |
87 ms |
61964 KB |
Output is correct |
61 |
Correct |
29 ms |
57140 KB |
Output is correct |
62 |
Correct |
108 ms |
65364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56644 KB |
Output is correct |
3 |
Correct |
25 ms |
56700 KB |
Output is correct |
4 |
Correct |
28 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56688 KB |
Output is correct |
6 |
Correct |
31 ms |
56752 KB |
Output is correct |
7 |
Correct |
26 ms |
56692 KB |
Output is correct |
8 |
Correct |
27 ms |
56660 KB |
Output is correct |
9 |
Correct |
27 ms |
56700 KB |
Output is correct |
10 |
Correct |
25 ms |
56668 KB |
Output is correct |
11 |
Correct |
27 ms |
56716 KB |
Output is correct |
12 |
Correct |
25 ms |
56636 KB |
Output is correct |
13 |
Correct |
25 ms |
56584 KB |
Output is correct |
14 |
Correct |
25 ms |
56784 KB |
Output is correct |
15 |
Correct |
26 ms |
56648 KB |
Output is correct |
16 |
Correct |
26 ms |
56664 KB |
Output is correct |
17 |
Correct |
26 ms |
56660 KB |
Output is correct |
18 |
Correct |
28 ms |
56668 KB |
Output is correct |
19 |
Correct |
29 ms |
56604 KB |
Output is correct |
20 |
Correct |
26 ms |
56720 KB |
Output is correct |
21 |
Correct |
27 ms |
56660 KB |
Output is correct |
22 |
Correct |
26 ms |
56720 KB |
Output is correct |
23 |
Correct |
26 ms |
56684 KB |
Output is correct |
24 |
Correct |
26 ms |
56644 KB |
Output is correct |
25 |
Correct |
26 ms |
56652 KB |
Output is correct |
26 |
Correct |
27 ms |
56704 KB |
Output is correct |
27 |
Correct |
25 ms |
56708 KB |
Output is correct |
28 |
Correct |
27 ms |
56660 KB |
Output is correct |
29 |
Correct |
26 ms |
56696 KB |
Output is correct |
30 |
Correct |
28 ms |
56620 KB |
Output is correct |
31 |
Correct |
26 ms |
56644 KB |
Output is correct |
32 |
Correct |
28 ms |
56696 KB |
Output is correct |
33 |
Correct |
26 ms |
56748 KB |
Output is correct |
34 |
Correct |
30 ms |
56772 KB |
Output is correct |
35 |
Correct |
91 ms |
62804 KB |
Output is correct |
36 |
Correct |
116 ms |
65932 KB |
Output is correct |
37 |
Correct |
112 ms |
65960 KB |
Output is correct |
38 |
Correct |
111 ms |
65532 KB |
Output is correct |
39 |
Correct |
28 ms |
57044 KB |
Output is correct |
40 |
Correct |
31 ms |
57148 KB |
Output is correct |
41 |
Correct |
33 ms |
57116 KB |
Output is correct |
42 |
Correct |
28 ms |
57032 KB |
Output is correct |
43 |
Correct |
32 ms |
57012 KB |
Output is correct |
44 |
Correct |
30 ms |
57088 KB |
Output is correct |
45 |
Correct |
28 ms |
57036 KB |
Output is correct |
46 |
Correct |
29 ms |
56948 KB |
Output is correct |
47 |
Correct |
29 ms |
57064 KB |
Output is correct |
48 |
Correct |
29 ms |
57164 KB |
Output is correct |
49 |
Correct |
35 ms |
58060 KB |
Output is correct |
50 |
Correct |
119 ms |
66008 KB |
Output is correct |
51 |
Correct |
33 ms |
57300 KB |
Output is correct |
52 |
Correct |
100 ms |
64236 KB |
Output is correct |
53 |
Correct |
35 ms |
57976 KB |
Output is correct |
54 |
Correct |
103 ms |
65048 KB |
Output is correct |
55 |
Correct |
36 ms |
57420 KB |
Output is correct |
56 |
Correct |
31 ms |
57428 KB |
Output is correct |
57 |
Correct |
31 ms |
57764 KB |
Output is correct |
58 |
Correct |
33 ms |
57848 KB |
Output is correct |
59 |
Correct |
28 ms |
56992 KB |
Output is correct |
60 |
Correct |
87 ms |
61964 KB |
Output is correct |
61 |
Correct |
29 ms |
57140 KB |
Output is correct |
62 |
Correct |
108 ms |
65364 KB |
Output is correct |
63 |
Correct |
454 ms |
110696 KB |
Output is correct |
64 |
Correct |
453 ms |
110620 KB |
Output is correct |
65 |
Correct |
457 ms |
110676 KB |
Output is correct |
66 |
Correct |
217 ms |
76828 KB |
Output is correct |
67 |
Correct |
493 ms |
81132 KB |
Output is correct |
68 |
Correct |
245 ms |
76716 KB |
Output is correct |
69 |
Correct |
474 ms |
77864 KB |
Output is correct |
70 |
Correct |
214 ms |
76828 KB |
Output is correct |
71 |
Correct |
210 ms |
76692 KB |
Output is correct |
72 |
Correct |
505 ms |
80400 KB |
Output is correct |
73 |
Correct |
465 ms |
81056 KB |
Output is correct |
74 |
Correct |
974 ms |
105608 KB |
Output is correct |
75 |
Correct |
599 ms |
83948 KB |
Output is correct |
76 |
Correct |
750 ms |
94508 KB |
Output is correct |
77 |
Correct |
748 ms |
94636 KB |
Output is correct |
78 |
Correct |
253 ms |
79300 KB |
Output is correct |
79 |
Correct |
398 ms |
83188 KB |
Output is correct |
80 |
Correct |
245 ms |
79152 KB |
Output is correct |
81 |
Correct |
397 ms |
82668 KB |
Output is correct |
82 |
Correct |
715 ms |
95180 KB |
Output is correct |
83 |
Correct |
717 ms |
95220 KB |
Output is correct |
84 |
Correct |
729 ms |
114064 KB |
Output is correct |
85 |
Correct |
686 ms |
114036 KB |
Output is correct |
86 |
Correct |
352 ms |
77572 KB |
Output is correct |
87 |
Correct |
353 ms |
79820 KB |
Output is correct |
88 |
Correct |
484 ms |
80880 KB |
Output is correct |
89 |
Correct |
726 ms |
91688 KB |
Output is correct |