#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 4e5 + 10;
set <int> in[MAXN], out_set[MAXN], in_set[MAXN];
set <pair<int, int>> out_edge[MAXN], in_edge[MAXN];
set <int>::iterator it;
set <pair<int, int>>::iterator itt;
int n, m, ptr[MAXN], p[MAXN], sz[MAXN];
ll ans;
void make_set() {
for (int i = 1; i <= n; i++) {
p[i] = i; sz[i] = 1;
in[i].clear(); out_set[i].clear(); in_set[i].clear();
out_edge[i].clear(); in_edge[i].clear();
ptr[i] = i;
}
ans = 0;
}
int getp(int u) {
if (p[u] == u)
return u;
return p[u] = getp(p[u]);
}
void un(int u, int v) {
u = getp(u); v = getp(v);
if (u == v)
return;
if (sz[u] + in_edge[ptr[u]].size() + out_edge[ptr[u]].size() > sz[v] + in_edge[ptr[v]].size() + out_edge[ptr[v]].size())
swap(u, v);
int cu = 0, cv = in_edge[ptr[v]].size();
ans += (ll)sz[u] * (ll)sz[v] * 2ll;
for (itt = out_edge[ptr[u]].begin(); itt != out_edge[ptr[u]].end(); itt++) {
pair<int, int> tmp = *itt;
int fr = tmp.fi, to = tmp.se;
if (getp(to) == v) {
ans -= (ll)sz[v];
cv--;
in_edge[ptr[v]].erase(tmp);
} else {
out_edge[ptr[v]].insert(tmp);
}
}
for (itt = in_edge[ptr[u]].begin(); itt != in_edge[ptr[u]].end(); itt++) {
pair<int, int> tmp = *itt;
int fr = tmp.fi, to = tmp.se;
if (getp(fr) == v) {
ans -= (ll)sz[u];
out_edge[ptr[v]].erase(tmp);
} else {
if (in[ptr[v]].count(fr)) {
cv--;
out_edge[ptr[getp(fr)]].erase(tmp);
} else {
cu++;
in_edge[ptr[v]].insert(tmp);
}
}
}
for (it = in[ptr[u]].begin(); it != in[ptr[u]].end(); it++) {
int fr = *it;
if (getp(fr) != v)
in[ptr[v]].insert(fr);
}
for (itt = out_edge[ptr[u]].begin(); itt != out_edge[ptr[u]].end(); itt++) {
pair<int, int> tmp = *itt;
int fr = tmp.fi, to = tmp.se;
if (getp(to) == v) {
in[ptr[v]].erase(fr);
}
}
ans += (ll)cu * (ll)sz[v] + (ll)cv * (ll)sz[u];
set <pair<int, int>> tocon; tocon.clear();
for (it = in_set[ptr[u]].begin(); it != in_set[ptr[u]].end(); it++) {
int fr = *it;
if (fr == v)
continue;
in_set[ptr[v]].insert(fr);
out_set[ptr[fr]].insert(v);
out_set[ptr[fr]].erase(u);
if (in_set[ptr[fr]].count(v))
tocon.insert({v, fr});
}
for (it = out_set[ptr[u]].begin(); it != out_set[ptr[u]].end(); it++) {
int to = *it;
if (to == v)
continue;
out_set[ptr[v]].insert(to);
in_set[ptr[to]].insert(v);
in_set[ptr[to]].erase(u);
if (out_set[ptr[to]].count(v))
tocon.insert({v, to});
}
in[ptr[u]].clear(); in_set[ptr[u]].clear(); out_set[ptr[u]].clear();
in_edge[ptr[u]].clear(); out_edge[ptr[u]].clear();
p[u] = v;
sz[v] += sz[u];
for (set <pair<int, int>>::iterator itt = tocon.begin(); itt != tocon.end(); itt++) {
pair<int, int> tmp = *itt;
un(tmp.fi, tmp.se);
}
}
void connect(int u, int v) {
int ru = u, rv = v;
u = getp(u); v = getp(v);
if (u == v || in[ptr[v]].count(ru))
return;
ans += (ll)sz[v];
out_edge[ptr[u]].insert({ru, rv});
in_edge[ptr[v]].insert({ru, rv});
in[ptr[v]].insert(ru);
out_set[ptr[u]].insert(v);
in_set[ptr[v]].insert(u);
if (in_set[ptr[u]].count(v))
un(u, v);
}
main() {
// freopen("input.txt", "r", stdin);
scanf("%d %d", &n, &m);
make_set();
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
connect(u, v);
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'void un(int, int)':
joitter2.cpp:43:9: warning: unused variable 'fr' [-Wunused-variable]
43 | int fr = tmp.fi, to = tmp.se;
| ^~
joitter2.cpp:54:22: warning: unused variable 'to' [-Wunused-variable]
54 | int fr = tmp.fi, to = tmp.se;
| ^~
joitter2.cpp: At global scope:
joitter2.cpp:127:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
127 | main() {
| ^
joitter2.cpp: In function 'int main()':
joitter2.cpp:129:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
129 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
133 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
94316 KB |
Output is correct |
2 |
Correct |
56 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
54 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
56 ms |
94316 KB |
Output is correct |
8 |
Correct |
59 ms |
94316 KB |
Output is correct |
9 |
Correct |
56 ms |
94316 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
54 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
54 ms |
94316 KB |
Output is correct |
15 |
Correct |
55 ms |
94316 KB |
Output is correct |
16 |
Correct |
54 ms |
94316 KB |
Output is correct |
17 |
Correct |
55 ms |
94444 KB |
Output is correct |
18 |
Correct |
55 ms |
94316 KB |
Output is correct |
19 |
Correct |
54 ms |
94316 KB |
Output is correct |
20 |
Correct |
55 ms |
94316 KB |
Output is correct |
21 |
Correct |
56 ms |
94316 KB |
Output is correct |
22 |
Correct |
55 ms |
94316 KB |
Output is correct |
23 |
Correct |
56 ms |
94316 KB |
Output is correct |
24 |
Correct |
56 ms |
94316 KB |
Output is correct |
25 |
Correct |
55 ms |
94316 KB |
Output is correct |
26 |
Correct |
55 ms |
94296 KB |
Output is correct |
27 |
Correct |
57 ms |
94316 KB |
Output is correct |
28 |
Correct |
56 ms |
94316 KB |
Output is correct |
29 |
Correct |
54 ms |
94316 KB |
Output is correct |
30 |
Correct |
55 ms |
94316 KB |
Output is correct |
31 |
Correct |
55 ms |
94316 KB |
Output is correct |
32 |
Correct |
55 ms |
94316 KB |
Output is correct |
33 |
Correct |
56 ms |
94316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
94316 KB |
Output is correct |
2 |
Correct |
56 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
54 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
56 ms |
94316 KB |
Output is correct |
8 |
Correct |
59 ms |
94316 KB |
Output is correct |
9 |
Correct |
56 ms |
94316 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
54 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
54 ms |
94316 KB |
Output is correct |
15 |
Correct |
55 ms |
94316 KB |
Output is correct |
16 |
Correct |
54 ms |
94316 KB |
Output is correct |
17 |
Correct |
55 ms |
94444 KB |
Output is correct |
18 |
Correct |
55 ms |
94316 KB |
Output is correct |
19 |
Correct |
54 ms |
94316 KB |
Output is correct |
20 |
Correct |
55 ms |
94316 KB |
Output is correct |
21 |
Correct |
56 ms |
94316 KB |
Output is correct |
22 |
Correct |
55 ms |
94316 KB |
Output is correct |
23 |
Correct |
56 ms |
94316 KB |
Output is correct |
24 |
Correct |
56 ms |
94316 KB |
Output is correct |
25 |
Correct |
55 ms |
94316 KB |
Output is correct |
26 |
Correct |
55 ms |
94296 KB |
Output is correct |
27 |
Correct |
57 ms |
94316 KB |
Output is correct |
28 |
Correct |
56 ms |
94316 KB |
Output is correct |
29 |
Correct |
54 ms |
94316 KB |
Output is correct |
30 |
Correct |
55 ms |
94316 KB |
Output is correct |
31 |
Correct |
55 ms |
94316 KB |
Output is correct |
32 |
Correct |
55 ms |
94316 KB |
Output is correct |
33 |
Correct |
56 ms |
94316 KB |
Output is correct |
34 |
Correct |
60 ms |
94444 KB |
Output is correct |
35 |
Correct |
178 ms |
100972 KB |
Output is correct |
36 |
Correct |
212 ms |
105068 KB |
Output is correct |
37 |
Correct |
220 ms |
105324 KB |
Output is correct |
38 |
Correct |
206 ms |
104684 KB |
Output is correct |
39 |
Correct |
59 ms |
94700 KB |
Output is correct |
40 |
Correct |
63 ms |
94828 KB |
Output is correct |
41 |
Correct |
64 ms |
94828 KB |
Output is correct |
42 |
Correct |
59 ms |
94572 KB |
Output is correct |
43 |
Correct |
61 ms |
94700 KB |
Output is correct |
44 |
Correct |
70 ms |
94700 KB |
Output is correct |
45 |
Correct |
60 ms |
94572 KB |
Output is correct |
46 |
Correct |
60 ms |
94572 KB |
Output is correct |
47 |
Correct |
64 ms |
94700 KB |
Output is correct |
48 |
Correct |
62 ms |
94700 KB |
Output is correct |
49 |
Correct |
72 ms |
95852 KB |
Output is correct |
50 |
Correct |
242 ms |
105580 KB |
Output is correct |
51 |
Correct |
66 ms |
95084 KB |
Output is correct |
52 |
Correct |
189 ms |
102764 KB |
Output is correct |
53 |
Correct |
72 ms |
95724 KB |
Output is correct |
54 |
Correct |
202 ms |
104044 KB |
Output is correct |
55 |
Correct |
65 ms |
95340 KB |
Output is correct |
56 |
Correct |
64 ms |
95340 KB |
Output is correct |
57 |
Correct |
64 ms |
95688 KB |
Output is correct |
58 |
Correct |
65 ms |
95596 KB |
Output is correct |
59 |
Correct |
64 ms |
94828 KB |
Output is correct |
60 |
Correct |
168 ms |
99820 KB |
Output is correct |
61 |
Correct |
63 ms |
94828 KB |
Output is correct |
62 |
Correct |
204 ms |
104428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
94316 KB |
Output is correct |
2 |
Correct |
56 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
54 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
56 ms |
94316 KB |
Output is correct |
8 |
Correct |
59 ms |
94316 KB |
Output is correct |
9 |
Correct |
56 ms |
94316 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
54 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
54 ms |
94316 KB |
Output is correct |
15 |
Correct |
55 ms |
94316 KB |
Output is correct |
16 |
Correct |
54 ms |
94316 KB |
Output is correct |
17 |
Correct |
55 ms |
94444 KB |
Output is correct |
18 |
Correct |
55 ms |
94316 KB |
Output is correct |
19 |
Correct |
54 ms |
94316 KB |
Output is correct |
20 |
Correct |
55 ms |
94316 KB |
Output is correct |
21 |
Correct |
56 ms |
94316 KB |
Output is correct |
22 |
Correct |
55 ms |
94316 KB |
Output is correct |
23 |
Correct |
56 ms |
94316 KB |
Output is correct |
24 |
Correct |
56 ms |
94316 KB |
Output is correct |
25 |
Correct |
55 ms |
94316 KB |
Output is correct |
26 |
Correct |
55 ms |
94296 KB |
Output is correct |
27 |
Correct |
57 ms |
94316 KB |
Output is correct |
28 |
Correct |
56 ms |
94316 KB |
Output is correct |
29 |
Correct |
54 ms |
94316 KB |
Output is correct |
30 |
Correct |
55 ms |
94316 KB |
Output is correct |
31 |
Correct |
55 ms |
94316 KB |
Output is correct |
32 |
Correct |
55 ms |
94316 KB |
Output is correct |
33 |
Correct |
56 ms |
94316 KB |
Output is correct |
34 |
Correct |
60 ms |
94444 KB |
Output is correct |
35 |
Correct |
178 ms |
100972 KB |
Output is correct |
36 |
Correct |
212 ms |
105068 KB |
Output is correct |
37 |
Correct |
220 ms |
105324 KB |
Output is correct |
38 |
Correct |
206 ms |
104684 KB |
Output is correct |
39 |
Correct |
59 ms |
94700 KB |
Output is correct |
40 |
Correct |
63 ms |
94828 KB |
Output is correct |
41 |
Correct |
64 ms |
94828 KB |
Output is correct |
42 |
Correct |
59 ms |
94572 KB |
Output is correct |
43 |
Correct |
61 ms |
94700 KB |
Output is correct |
44 |
Correct |
70 ms |
94700 KB |
Output is correct |
45 |
Correct |
60 ms |
94572 KB |
Output is correct |
46 |
Correct |
60 ms |
94572 KB |
Output is correct |
47 |
Correct |
64 ms |
94700 KB |
Output is correct |
48 |
Correct |
62 ms |
94700 KB |
Output is correct |
49 |
Correct |
72 ms |
95852 KB |
Output is correct |
50 |
Correct |
242 ms |
105580 KB |
Output is correct |
51 |
Correct |
66 ms |
95084 KB |
Output is correct |
52 |
Correct |
189 ms |
102764 KB |
Output is correct |
53 |
Correct |
72 ms |
95724 KB |
Output is correct |
54 |
Correct |
202 ms |
104044 KB |
Output is correct |
55 |
Correct |
65 ms |
95340 KB |
Output is correct |
56 |
Correct |
64 ms |
95340 KB |
Output is correct |
57 |
Correct |
64 ms |
95688 KB |
Output is correct |
58 |
Correct |
65 ms |
95596 KB |
Output is correct |
59 |
Correct |
64 ms |
94828 KB |
Output is correct |
60 |
Correct |
168 ms |
99820 KB |
Output is correct |
61 |
Correct |
63 ms |
94828 KB |
Output is correct |
62 |
Correct |
204 ms |
104428 KB |
Output is correct |
63 |
Correct |
850 ms |
171372 KB |
Output is correct |
64 |
Correct |
853 ms |
171372 KB |
Output is correct |
65 |
Correct |
846 ms |
171372 KB |
Output is correct |
66 |
Correct |
405 ms |
109292 KB |
Output is correct |
67 |
Correct |
1433 ms |
122732 KB |
Output is correct |
68 |
Correct |
396 ms |
109356 KB |
Output is correct |
69 |
Correct |
789 ms |
114668 KB |
Output is correct |
70 |
Correct |
450 ms |
109328 KB |
Output is correct |
71 |
Correct |
396 ms |
109420 KB |
Output is correct |
72 |
Correct |
1065 ms |
117724 KB |
Output is correct |
73 |
Correct |
1110 ms |
119404 KB |
Output is correct |
74 |
Correct |
2251 ms |
140020 KB |
Output is correct |
75 |
Correct |
1099 ms |
122392 KB |
Output is correct |
76 |
Correct |
1543 ms |
133612 KB |
Output is correct |
77 |
Correct |
1520 ms |
133876 KB |
Output is correct |
78 |
Correct |
414 ms |
115436 KB |
Output is correct |
79 |
Correct |
760 ms |
119816 KB |
Output is correct |
80 |
Correct |
435 ms |
115236 KB |
Output is correct |
81 |
Correct |
709 ms |
118644 KB |
Output is correct |
82 |
Correct |
1280 ms |
146188 KB |
Output is correct |
83 |
Correct |
1265 ms |
146284 KB |
Output is correct |
84 |
Correct |
1178 ms |
163484 KB |
Output is correct |
85 |
Correct |
1213 ms |
163368 KB |
Output is correct |
86 |
Correct |
1126 ms |
123128 KB |
Output is correct |
87 |
Correct |
1194 ms |
125168 KB |
Output is correct |
88 |
Correct |
1180 ms |
118252 KB |
Output is correct |
89 |
Correct |
1489 ms |
131424 KB |
Output is correct |