#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
int n, m;
int lab[N];
set<int> s[N];
set<array<int, 2>> t[N];
queue<array<int, 2>> q;
int size(int u) {
return -lab[u];
}
int get(int u) {
return lab[u] < 0 ? u : lab[u] = get(lab[u]);
}
bool check(set<array<int, 2>> &st, int x) {
auto it = st.lower_bound(array<int, 2>{x});
return it != st.end() && (*it)[0] == x;
}
int count(set<array<int, 2>> &st, int x) {
auto it = st.lower_bound(array<int, 2>{x});
int cnt = 0;
for (; it != st.end() && (*it)[0] == x; it = st.erase(it)) {
++cnt;
}
return cnt;
}
long long res;
void mrg(int u, int v) {
u = get(u), v = get(v);
if (u == v) {
return;
}
int cnt = count(t[u], v);
res -= (long long) cnt * size(u);
cnt = count(t[v], u);
res -= (long long) cnt * size(v);
res += (long long) 2 * size(u) * size(v);
if (s[u].size() + t[u].size() < s[v].size() + t[v].size()) {
swap(u, v);
}
for (int x : s[v]) {
if (check(t[u], x)) {
q.push({x, u});
}
}
for (auto x : t[v]) {
if (s[u].find(x[0]) != s[u].end()) {
q.push({x[0], u});
}
}
for (int x : s[v]) {
s[u].insert(x);
auto it = t[x].lower_bound(array<int, 2>{v});
for (; it != t[x].end() && (*it)[0] == v; it = t[x].erase(it)) {
t[x].insert({u, (*it)[1]});
}
}
cnt = t[u].size();
for (auto [x, y] : t[v]) {
if (t[u].insert({x, y}).second) {
res += size(u);
} else {
--cnt;
}
s[x].erase(v);
s[x].insert(u);
}
res += (long long) cnt * size(v);
lab[u] += lab[v];
lab[v] = u;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
fill(lab + 1, lab + n + 1, -1);
for (int i = 1; i <= m; ++i) {
int a, b; cin >> a >> b;
int u = get(a), v = get(b);
if (u != v) {
s[u].insert(v);
res += t[v].insert({u, a}).second * size(v);
if (s[v].find(u) != s[v].end()) {
q.push({u, v});
}
for (; q.size(); q.pop()) {
mrg(q.front()[0], q.front()[1]);
}
}
cout << res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9856 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9892 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9816 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
2 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
2 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9884 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
10076 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
2 ms |
9652 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
2 ms |
9820 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9856 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9892 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9816 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
2 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
2 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9884 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
10076 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
2 ms |
9652 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
2 ms |
9820 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
2 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9872 KB |
Output is correct |
35 |
Correct |
46 ms |
15104 KB |
Output is correct |
36 |
Correct |
63 ms |
17236 KB |
Output is correct |
37 |
Correct |
63 ms |
17236 KB |
Output is correct |
38 |
Correct |
62 ms |
16928 KB |
Output is correct |
39 |
Correct |
3 ms |
10072 KB |
Output is correct |
40 |
Correct |
4 ms |
10328 KB |
Output is correct |
41 |
Correct |
5 ms |
10332 KB |
Output is correct |
42 |
Correct |
3 ms |
10076 KB |
Output is correct |
43 |
Correct |
4 ms |
10076 KB |
Output is correct |
44 |
Correct |
3 ms |
10076 KB |
Output is correct |
45 |
Correct |
4 ms |
9924 KB |
Output is correct |
46 |
Correct |
3 ms |
10076 KB |
Output is correct |
47 |
Correct |
4 ms |
10328 KB |
Output is correct |
48 |
Correct |
4 ms |
10076 KB |
Output is correct |
49 |
Correct |
8 ms |
10588 KB |
Output is correct |
50 |
Correct |
64 ms |
17236 KB |
Output is correct |
51 |
Correct |
6 ms |
10332 KB |
Output is correct |
52 |
Correct |
54 ms |
16024 KB |
Output is correct |
53 |
Correct |
8 ms |
10588 KB |
Output is correct |
54 |
Correct |
58 ms |
16644 KB |
Output is correct |
55 |
Correct |
5 ms |
10332 KB |
Output is correct |
56 |
Correct |
4 ms |
10332 KB |
Output is correct |
57 |
Correct |
5 ms |
10124 KB |
Output is correct |
58 |
Correct |
4 ms |
10332 KB |
Output is correct |
59 |
Correct |
4 ms |
10332 KB |
Output is correct |
60 |
Correct |
47 ms |
15352 KB |
Output is correct |
61 |
Correct |
5 ms |
10076 KB |
Output is correct |
62 |
Correct |
61 ms |
16772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9856 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9892 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9816 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
2 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
2 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9884 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
10076 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
2 ms |
9652 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
2 ms |
9820 KB |
Output is correct |
30 |
Correct |
2 ms |
9816 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
2 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9872 KB |
Output is correct |
35 |
Correct |
46 ms |
15104 KB |
Output is correct |
36 |
Correct |
63 ms |
17236 KB |
Output is correct |
37 |
Correct |
63 ms |
17236 KB |
Output is correct |
38 |
Correct |
62 ms |
16928 KB |
Output is correct |
39 |
Correct |
3 ms |
10072 KB |
Output is correct |
40 |
Correct |
4 ms |
10328 KB |
Output is correct |
41 |
Correct |
5 ms |
10332 KB |
Output is correct |
42 |
Correct |
3 ms |
10076 KB |
Output is correct |
43 |
Correct |
4 ms |
10076 KB |
Output is correct |
44 |
Correct |
3 ms |
10076 KB |
Output is correct |
45 |
Correct |
4 ms |
9924 KB |
Output is correct |
46 |
Correct |
3 ms |
10076 KB |
Output is correct |
47 |
Correct |
4 ms |
10328 KB |
Output is correct |
48 |
Correct |
4 ms |
10076 KB |
Output is correct |
49 |
Correct |
8 ms |
10588 KB |
Output is correct |
50 |
Correct |
64 ms |
17236 KB |
Output is correct |
51 |
Correct |
6 ms |
10332 KB |
Output is correct |
52 |
Correct |
54 ms |
16024 KB |
Output is correct |
53 |
Correct |
8 ms |
10588 KB |
Output is correct |
54 |
Correct |
58 ms |
16644 KB |
Output is correct |
55 |
Correct |
5 ms |
10332 KB |
Output is correct |
56 |
Correct |
4 ms |
10332 KB |
Output is correct |
57 |
Correct |
5 ms |
10124 KB |
Output is correct |
58 |
Correct |
4 ms |
10332 KB |
Output is correct |
59 |
Correct |
4 ms |
10332 KB |
Output is correct |
60 |
Correct |
47 ms |
15352 KB |
Output is correct |
61 |
Correct |
5 ms |
10076 KB |
Output is correct |
62 |
Correct |
61 ms |
16772 KB |
Output is correct |
63 |
Correct |
214 ms |
43856 KB |
Output is correct |
64 |
Correct |
196 ms |
43652 KB |
Output is correct |
65 |
Correct |
194 ms |
43860 KB |
Output is correct |
66 |
Correct |
94 ms |
23892 KB |
Output is correct |
67 |
Correct |
205 ms |
51024 KB |
Output is correct |
68 |
Correct |
88 ms |
23892 KB |
Output is correct |
69 |
Correct |
172 ms |
23972 KB |
Output is correct |
70 |
Correct |
93 ms |
23972 KB |
Output is correct |
71 |
Correct |
90 ms |
24144 KB |
Output is correct |
72 |
Correct |
202 ms |
34752 KB |
Output is correct |
73 |
Correct |
206 ms |
36432 KB |
Output is correct |
74 |
Correct |
457 ms |
50636 KB |
Output is correct |
75 |
Correct |
225 ms |
28704 KB |
Output is correct |
76 |
Correct |
327 ms |
38224 KB |
Output is correct |
77 |
Correct |
338 ms |
38244 KB |
Output is correct |
78 |
Correct |
117 ms |
23920 KB |
Output is correct |
79 |
Correct |
133 ms |
28752 KB |
Output is correct |
80 |
Correct |
110 ms |
24052 KB |
Output is correct |
81 |
Correct |
200 ms |
27988 KB |
Output is correct |
82 |
Correct |
298 ms |
35156 KB |
Output is correct |
83 |
Correct |
286 ms |
35152 KB |
Output is correct |
84 |
Correct |
230 ms |
34128 KB |
Output is correct |
85 |
Correct |
230 ms |
34128 KB |
Output is correct |
86 |
Correct |
169 ms |
58944 KB |
Output is correct |
87 |
Correct |
181 ms |
61264 KB |
Output is correct |
88 |
Correct |
206 ms |
35668 KB |
Output is correct |
89 |
Correct |
292 ms |
37184 KB |
Output is correct |