#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 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;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14856 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15040 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14792 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14772 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14936 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14804 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
5 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14856 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15040 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14792 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14772 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14936 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14804 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
5 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
34 |
Correct |
6 ms |
15196 KB |
Output is correct |
35 |
Correct |
223 ms |
58120 KB |
Output is correct |
36 |
Correct |
764 ms |
180596 KB |
Output is correct |
37 |
Correct |
771 ms |
180532 KB |
Output is correct |
38 |
Correct |
780 ms |
187472 KB |
Output is correct |
39 |
Correct |
237 ms |
108856 KB |
Output is correct |
40 |
Correct |
7 ms |
15704 KB |
Output is correct |
41 |
Correct |
7 ms |
15708 KB |
Output is correct |
42 |
Correct |
227 ms |
108932 KB |
Output is correct |
43 |
Correct |
272 ms |
111524 KB |
Output is correct |
44 |
Correct |
260 ms |
108172 KB |
Output is correct |
45 |
Correct |
233 ms |
108852 KB |
Output is correct |
46 |
Correct |
225 ms |
108880 KB |
Output is correct |
47 |
Correct |
33 ms |
25180 KB |
Output is correct |
48 |
Correct |
32 ms |
24980 KB |
Output is correct |
49 |
Correct |
195 ms |
71376 KB |
Output is correct |
50 |
Correct |
774 ms |
177468 KB |
Output is correct |
51 |
Correct |
295 ms |
108376 KB |
Output is correct |
52 |
Correct |
656 ms |
167008 KB |
Output is correct |
53 |
Correct |
239 ms |
83524 KB |
Output is correct |
54 |
Correct |
724 ms |
177492 KB |
Output is correct |
55 |
Correct |
331 ms |
117948 KB |
Output is correct |
56 |
Correct |
319 ms |
115380 KB |
Output is correct |
57 |
Correct |
258 ms |
97312 KB |
Output is correct |
58 |
Correct |
272 ms |
99236 KB |
Output is correct |
59 |
Correct |
7 ms |
15704 KB |
Output is correct |
60 |
Correct |
58 ms |
20400 KB |
Output is correct |
61 |
Correct |
84 ms |
42892 KB |
Output is correct |
62 |
Correct |
768 ms |
174952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14856 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15040 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14792 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14772 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14936 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14804 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
5 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
34 |
Correct |
6 ms |
15196 KB |
Output is correct |
35 |
Correct |
223 ms |
58120 KB |
Output is correct |
36 |
Correct |
764 ms |
180596 KB |
Output is correct |
37 |
Correct |
771 ms |
180532 KB |
Output is correct |
38 |
Correct |
780 ms |
187472 KB |
Output is correct |
39 |
Correct |
237 ms |
108856 KB |
Output is correct |
40 |
Correct |
7 ms |
15704 KB |
Output is correct |
41 |
Correct |
7 ms |
15708 KB |
Output is correct |
42 |
Correct |
227 ms |
108932 KB |
Output is correct |
43 |
Correct |
272 ms |
111524 KB |
Output is correct |
44 |
Correct |
260 ms |
108172 KB |
Output is correct |
45 |
Correct |
233 ms |
108852 KB |
Output is correct |
46 |
Correct |
225 ms |
108880 KB |
Output is correct |
47 |
Correct |
33 ms |
25180 KB |
Output is correct |
48 |
Correct |
32 ms |
24980 KB |
Output is correct |
49 |
Correct |
195 ms |
71376 KB |
Output is correct |
50 |
Correct |
774 ms |
177468 KB |
Output is correct |
51 |
Correct |
295 ms |
108376 KB |
Output is correct |
52 |
Correct |
656 ms |
167008 KB |
Output is correct |
53 |
Correct |
239 ms |
83524 KB |
Output is correct |
54 |
Correct |
724 ms |
177492 KB |
Output is correct |
55 |
Correct |
331 ms |
117948 KB |
Output is correct |
56 |
Correct |
319 ms |
115380 KB |
Output is correct |
57 |
Correct |
258 ms |
97312 KB |
Output is correct |
58 |
Correct |
272 ms |
99236 KB |
Output is correct |
59 |
Correct |
7 ms |
15704 KB |
Output is correct |
60 |
Correct |
58 ms |
20400 KB |
Output is correct |
61 |
Correct |
84 ms |
42892 KB |
Output is correct |
62 |
Correct |
768 ms |
174952 KB |
Output is correct |
63 |
Correct |
471 ms |
67412 KB |
Output is correct |
64 |
Correct |
462 ms |
67156 KB |
Output is correct |
65 |
Correct |
486 ms |
67272 KB |
Output is correct |
66 |
Runtime error |
2547 ms |
1048580 KB |
Execution killed with signal 9 |
67 |
Halted |
0 ms |
0 KB |
- |