#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-bg(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-bg(a); }
int n, m;
queue<array<int, 2>> to_merge;
const int max_n = 1e5;
int e[max_n], ans;
set<int> C[max_n], g[max_n], gr[max_n];
int dsu_size(int A) { return C[A].size() + g[A].size() + gr[A].size(); }
int find(int u) { return e[u] < 0 ? u : e[u]=find(e[u]); }
void weak_connect(int A, int B) {
// if (A == B) return;
g[A].insert(B); gr[B].insert(A);
if (g[B].count(A)) to_merge.push({A, B});
}
void onion(int A, int B) {
if (A == B) return;
if (dsu_size(A) < dsu_size(B)) swap(A, B);
ans += -e[A]*C[B].size() + -e[B]*C[A].size();
e[A] += e[B];
e[B] = A;
for (int b : C[B]) {
if (C[A].count(b)) ans += e[A];
else C[A].insert(b);
}
g[A].erase(B), gr[B].erase(A);
g[B].erase(A), gr[A].erase(B);
for (int b : g[B]) {
gr[b].erase(B);
weak_connect(A, b);
}
for (int b : gr[B]) {
g[b].erase(B);
weak_connect(b, A);
}
}
void solve() {
cin >> n >> m;
memset(e, -1, sizeof e);
for (int i = 0; i < n; i++) C[i].insert(i);
while (m--) {
int u, v; cin >> u >> v; u--; v--;
if (find(u) != find(v) && !C[find(v)].count(u)) {
C[find(v)].insert(u);
ans -= e[find(v)];
weak_connect(find(u), find(v));
while (to_merge.size()) {
onion(find(to_merge.front()[0]), find(to_merge.front()[1]));
to_merge.pop();
}
}
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14768 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
15192 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14808 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14764 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14796 KB |
Output is correct |
30 |
Correct |
4 ms |
14936 KB |
Output is correct |
31 |
Correct |
4 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14768 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
15192 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14808 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14764 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14796 KB |
Output is correct |
30 |
Correct |
4 ms |
14936 KB |
Output is correct |
31 |
Correct |
4 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
15136 KB |
Output is correct |
35 |
Correct |
64 ms |
19636 KB |
Output is correct |
36 |
Correct |
81 ms |
22468 KB |
Output is correct |
37 |
Correct |
80 ms |
22352 KB |
Output is correct |
38 |
Correct |
78 ms |
22100 KB |
Output is correct |
39 |
Correct |
6 ms |
15192 KB |
Output is correct |
40 |
Correct |
7 ms |
15452 KB |
Output is correct |
41 |
Correct |
7 ms |
15644 KB |
Output is correct |
42 |
Correct |
5 ms |
15192 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15176 KB |
Output is correct |
46 |
Correct |
5 ms |
15080 KB |
Output is correct |
47 |
Correct |
7 ms |
15448 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16220 KB |
Output is correct |
50 |
Correct |
84 ms |
22704 KB |
Output is correct |
51 |
Correct |
9 ms |
15448 KB |
Output is correct |
52 |
Correct |
69 ms |
20848 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
73 ms |
21748 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
10 ms |
15844 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
8 ms |
15452 KB |
Output is correct |
59 |
Correct |
7 ms |
15708 KB |
Output is correct |
60 |
Correct |
58 ms |
19536 KB |
Output is correct |
61 |
Correct |
7 ms |
15452 KB |
Output is correct |
62 |
Correct |
76 ms |
21844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14768 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
15192 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14808 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14820 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14764 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14796 KB |
Output is correct |
30 |
Correct |
4 ms |
14936 KB |
Output is correct |
31 |
Correct |
4 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
15136 KB |
Output is correct |
35 |
Correct |
64 ms |
19636 KB |
Output is correct |
36 |
Correct |
81 ms |
22468 KB |
Output is correct |
37 |
Correct |
80 ms |
22352 KB |
Output is correct |
38 |
Correct |
78 ms |
22100 KB |
Output is correct |
39 |
Correct |
6 ms |
15192 KB |
Output is correct |
40 |
Correct |
7 ms |
15452 KB |
Output is correct |
41 |
Correct |
7 ms |
15644 KB |
Output is correct |
42 |
Correct |
5 ms |
15192 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15176 KB |
Output is correct |
46 |
Correct |
5 ms |
15080 KB |
Output is correct |
47 |
Correct |
7 ms |
15448 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16220 KB |
Output is correct |
50 |
Correct |
84 ms |
22704 KB |
Output is correct |
51 |
Correct |
9 ms |
15448 KB |
Output is correct |
52 |
Correct |
69 ms |
20848 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
73 ms |
21748 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
10 ms |
15844 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
8 ms |
15452 KB |
Output is correct |
59 |
Correct |
7 ms |
15708 KB |
Output is correct |
60 |
Correct |
58 ms |
19536 KB |
Output is correct |
61 |
Correct |
7 ms |
15452 KB |
Output is correct |
62 |
Correct |
76 ms |
21844 KB |
Output is correct |
63 |
Correct |
506 ms |
65944 KB |
Output is correct |
64 |
Correct |
481 ms |
66132 KB |
Output is correct |
65 |
Correct |
470 ms |
65684 KB |
Output is correct |
66 |
Incorrect |
186 ms |
33540 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |