#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing
const int MAX_N = 100010;
ll ans;
int n, m, g[MAX_N], sz[MAX_N], cost[MAX_N];
ll cntin[MAX_N];
map<int, set<pair<int,int>>> in[MAX_N], out[MAX_N];
void init() {
iota(g, g+n+1, 0);
fill(sz, sz + n + 1, 1);
}
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
void rm(int x, int u) {
--cost[F(x)], --cost[F(u)];
ans -= sz[ F(u) ];
--cntin[ F(u) ];
in[F(u)][F(x)].erase({x, u});
out[F(x)][F(u)].erase({x, u});
}
void ad(int x, int u) {
++cost[F(x)], ++cost[F(u)];
ans += sz[ F(u) ];
++cntin[ F(u) ];
in[F(u)][F(x)].insert({x, u});
out[F(x)][F(u)].insert({x, u});
}
bool mcnt(set<pair<int,int>> &st, int k) {
return st.lower_bound({k, -1}) != st.upper_bound({k+1, -1});
}
void kick(int a, int b) {
auto vec = out[F(a)][F(b)];
for (auto [x, y] : vec) rm(x, y);
}
bool no_merge(int a, int b) {
if (F(a) == F(b)) return true;
// if a already connected to group(b), then nothing happen
if (mcnt(in[F(b)][F(a)], a)) return true;
if (out[F(b)][F(a)].empty()) return true;
return false;
}
void add_edge(int a, int b) {
if (F(a) == F(b)) return;
// if a already connected to group(b), then nothing happen
if (mcnt(in[F(b)][F(a)], a)) return;
ad(a, b);
if (out[F(b)][F(a)].empty()) return;
a = F(a), b = F(b);
if (cost[a] > cost[b]) swap(a, b);
kick(a, b), kick(b, a);
vector<pair<int,int>> kl;
for (auto [_, vec] : in[a])
for (auto p : vec) kl.pb(p);
//kl.insert(end(kl), AI(vec));
for (auto [_, vec] : out[a])
for (auto p : vec) kl.pb(p);
//kl.insert(end(kl), AI(vec));
in[a].clear(), out[a].clear();
for (auto [x, y] : kl) rm(x, y);
ans += 2ll * sz[a] * sz[b] + 1ll * cntin[b] * sz[a];
sz[b] += sz[a], g[a] = b;
for (auto [x, y] : kl) add_edge(x, y);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
init();
while (m--) {
int a, b;
cin >> a >> b;
add_edge(a, b);
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9744 KB |
Output is correct |
6 |
Correct |
8 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9736 KB |
Output is correct |
8 |
Correct |
7 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
6 ms |
9728 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9804 KB |
Output is correct |
25 |
Correct |
8 ms |
9788 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9704 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9744 KB |
Output is correct |
6 |
Correct |
8 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9736 KB |
Output is correct |
8 |
Correct |
7 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
6 ms |
9728 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9804 KB |
Output is correct |
25 |
Correct |
8 ms |
9788 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9704 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
7 ms |
9676 KB |
Output is correct |
34 |
Correct |
10 ms |
9932 KB |
Output is correct |
35 |
Correct |
129 ms |
15680 KB |
Output is correct |
36 |
Correct |
200 ms |
21696 KB |
Output is correct |
37 |
Correct |
203 ms |
21952 KB |
Output is correct |
38 |
Correct |
181 ms |
21000 KB |
Output is correct |
39 |
Correct |
12 ms |
10164 KB |
Output is correct |
40 |
Correct |
11 ms |
10316 KB |
Output is correct |
41 |
Correct |
11 ms |
10308 KB |
Output is correct |
42 |
Correct |
10 ms |
10112 KB |
Output is correct |
43 |
Correct |
11 ms |
10316 KB |
Output is correct |
44 |
Correct |
11 ms |
10300 KB |
Output is correct |
45 |
Correct |
12 ms |
10188 KB |
Output is correct |
46 |
Correct |
13 ms |
10128 KB |
Output is correct |
47 |
Correct |
14 ms |
10316 KB |
Output is correct |
48 |
Correct |
12 ms |
10308 KB |
Output is correct |
49 |
Correct |
25 ms |
12532 KB |
Output is correct |
50 |
Correct |
167 ms |
22336 KB |
Output is correct |
51 |
Correct |
21 ms |
10912 KB |
Output is correct |
52 |
Correct |
130 ms |
17580 KB |
Output is correct |
53 |
Correct |
22 ms |
12204 KB |
Output is correct |
54 |
Correct |
143 ms |
19824 KB |
Output is correct |
55 |
Correct |
16 ms |
11340 KB |
Output is correct |
56 |
Correct |
16 ms |
11364 KB |
Output is correct |
57 |
Correct |
15 ms |
11340 KB |
Output is correct |
58 |
Correct |
18 ms |
11396 KB |
Output is correct |
59 |
Correct |
9 ms |
9932 KB |
Output is correct |
60 |
Correct |
105 ms |
12284 KB |
Output is correct |
61 |
Correct |
13 ms |
10444 KB |
Output is correct |
62 |
Correct |
159 ms |
20636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9744 KB |
Output is correct |
6 |
Correct |
8 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9736 KB |
Output is correct |
8 |
Correct |
7 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
6 ms |
9728 KB |
Output is correct |
16 |
Correct |
6 ms |
9696 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9804 KB |
Output is correct |
25 |
Correct |
8 ms |
9788 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
6 ms |
9704 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
7 ms |
9676 KB |
Output is correct |
34 |
Correct |
10 ms |
9932 KB |
Output is correct |
35 |
Correct |
129 ms |
15680 KB |
Output is correct |
36 |
Correct |
200 ms |
21696 KB |
Output is correct |
37 |
Correct |
203 ms |
21952 KB |
Output is correct |
38 |
Correct |
181 ms |
21000 KB |
Output is correct |
39 |
Correct |
12 ms |
10164 KB |
Output is correct |
40 |
Correct |
11 ms |
10316 KB |
Output is correct |
41 |
Correct |
11 ms |
10308 KB |
Output is correct |
42 |
Correct |
10 ms |
10112 KB |
Output is correct |
43 |
Correct |
11 ms |
10316 KB |
Output is correct |
44 |
Correct |
11 ms |
10300 KB |
Output is correct |
45 |
Correct |
12 ms |
10188 KB |
Output is correct |
46 |
Correct |
13 ms |
10128 KB |
Output is correct |
47 |
Correct |
14 ms |
10316 KB |
Output is correct |
48 |
Correct |
12 ms |
10308 KB |
Output is correct |
49 |
Correct |
25 ms |
12532 KB |
Output is correct |
50 |
Correct |
167 ms |
22336 KB |
Output is correct |
51 |
Correct |
21 ms |
10912 KB |
Output is correct |
52 |
Correct |
130 ms |
17580 KB |
Output is correct |
53 |
Correct |
22 ms |
12204 KB |
Output is correct |
54 |
Correct |
143 ms |
19824 KB |
Output is correct |
55 |
Correct |
16 ms |
11340 KB |
Output is correct |
56 |
Correct |
16 ms |
11364 KB |
Output is correct |
57 |
Correct |
15 ms |
11340 KB |
Output is correct |
58 |
Correct |
18 ms |
11396 KB |
Output is correct |
59 |
Correct |
9 ms |
9932 KB |
Output is correct |
60 |
Correct |
105 ms |
12284 KB |
Output is correct |
61 |
Correct |
13 ms |
10444 KB |
Output is correct |
62 |
Correct |
159 ms |
20636 KB |
Output is correct |
63 |
Correct |
852 ms |
126280 KB |
Output is correct |
64 |
Correct |
814 ms |
126368 KB |
Output is correct |
65 |
Correct |
839 ms |
126412 KB |
Output is correct |
66 |
Correct |
465 ms |
32524 KB |
Output is correct |
67 |
Correct |
620 ms |
39720 KB |
Output is correct |
68 |
Correct |
425 ms |
32496 KB |
Output is correct |
69 |
Correct |
713 ms |
39388 KB |
Output is correct |
70 |
Correct |
477 ms |
32548 KB |
Output is correct |
71 |
Correct |
478 ms |
32528 KB |
Output is correct |
72 |
Correct |
731 ms |
40864 KB |
Output is correct |
73 |
Correct |
769 ms |
40876 KB |
Output is correct |
74 |
Correct |
2050 ms |
100928 KB |
Output is correct |
75 |
Correct |
992 ms |
51828 KB |
Output is correct |
76 |
Correct |
1399 ms |
77648 KB |
Output is correct |
77 |
Correct |
1466 ms |
78120 KB |
Output is correct |
78 |
Correct |
420 ms |
43928 KB |
Output is correct |
79 |
Correct |
721 ms |
46716 KB |
Output is correct |
80 |
Correct |
392 ms |
43768 KB |
Output is correct |
81 |
Correct |
612 ms |
46080 KB |
Output is correct |
82 |
Correct |
1317 ms |
93904 KB |
Output is correct |
83 |
Correct |
1431 ms |
93988 KB |
Output is correct |
84 |
Correct |
1463 ms |
92264 KB |
Output is correct |
85 |
Correct |
1364 ms |
92272 KB |
Output is correct |
86 |
Correct |
248 ms |
22332 KB |
Output is correct |
87 |
Correct |
282 ms |
23364 KB |
Output is correct |
88 |
Correct |
854 ms |
41056 KB |
Output is correct |
89 |
Correct |
1546 ms |
73480 KB |
Output is correct |