#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];
set<tuple<int,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)].erase({F(x),x,u});
out[F(x)].erase({F(u),x,u});
}
void ad(int x, int u) {
++cost[F(x)], ++cost[F(u)];
ans += sz[ F(u) ];
++cntin[ F(u) ];
in[F(u)].insert({F(x),x,u});
out[F(x)].insert({F(u),x,u});
}
void kick(int a, int b) {
auto it = out[F(a)].lower_bound({F(b), -1, -1});
vector<pair<int,int>> vec;
while (it != end(out[F(a)]) && get<0>(*it) == F(b)) {
auto [q, w, e] = *it;
vec.pb(w, e);
it = out[F(a)].erase(it);
}
for (auto [x, y] : vec) rm(x, y);
}
void add_edge(int a, int b) {
if (F(a) == F(b)) return;
// if a already connected to group(b), then nothing happen
auto it = in[F(b)].lower_bound({F(a), a, -1});
if (it != end(in[F(b)])) {
auto [g, x, y] = *it;
DE(g, x, y);
}
if (it != end(in[F(b)]) && get<0>(*it) == F(a) && get<1>(*it) == a) return;
ad(a, b);
it = out[F(b)].lower_bound({F(a), -1, -1});
if (it == end(out[F(b)]) || get<0>(*it) != F(a)) return;
a = F(a), b = F(b);
if (cost[a] > cost[b] * 2) swap(a, b);
assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());
kick(a, b), kick(b, a);
vector<pair<int,int>> kl;
for (auto [_, x, y] : in[a]) kl.pb(x, y);
for (auto [_, x, y] : out[a]) kl.pb(x, y);
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;
vector<pair<int,int>> alle(m);
for (auto &[a, b] : alle) cin >> a >> b;
vector<ll> res;
init();
for (auto [a, b] : alle) {
add_edge(a, b);
res.pb(ans);
}
for (auto i : res) cout << i << '\n';
}
Compilation message
joitter2.cpp: In function 'void add_edge(int, int)':
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
joitter2.cpp:73:3: note: in expansion of macro 'DE'
73 | DE(g, x, y);
| ^~
joitter2.cpp:72:8: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
72 | auto [g, x, y] = *it;
| ^~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from joitter2.cpp:1:
joitter2.cpp:84:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:84:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9816 KB |
Output is correct |
8 |
Correct |
7 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9752 KB |
Output is correct |
10 |
Correct |
6 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
8 ms |
9672 KB |
Output is correct |
21 |
Correct |
8 ms |
9804 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9856 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9804 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
7 ms |
9616 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9736 KB |
Output is correct |
32 |
Correct |
6 ms |
9688 KB |
Output is correct |
33 |
Correct |
9 ms |
9800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9816 KB |
Output is correct |
8 |
Correct |
7 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9752 KB |
Output is correct |
10 |
Correct |
6 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
8 ms |
9672 KB |
Output is correct |
21 |
Correct |
8 ms |
9804 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9856 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9804 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
7 ms |
9616 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9736 KB |
Output is correct |
32 |
Correct |
6 ms |
9688 KB |
Output is correct |
33 |
Correct |
9 ms |
9800 KB |
Output is correct |
34 |
Correct |
9 ms |
10112 KB |
Output is correct |
35 |
Correct |
129 ms |
17928 KB |
Output is correct |
36 |
Correct |
135 ms |
20072 KB |
Output is correct |
37 |
Correct |
176 ms |
20200 KB |
Output is correct |
38 |
Correct |
137 ms |
19876 KB |
Output is correct |
39 |
Correct |
9 ms |
9856 KB |
Output is correct |
40 |
Correct |
10 ms |
9984 KB |
Output is correct |
41 |
Correct |
9 ms |
9992 KB |
Output is correct |
42 |
Correct |
10 ms |
9804 KB |
Output is correct |
43 |
Correct |
11 ms |
9932 KB |
Output is correct |
44 |
Correct |
13 ms |
9932 KB |
Output is correct |
45 |
Correct |
10 ms |
9904 KB |
Output is correct |
46 |
Correct |
8 ms |
9804 KB |
Output is correct |
47 |
Correct |
10 ms |
9932 KB |
Output is correct |
48 |
Correct |
10 ms |
10004 KB |
Output is correct |
49 |
Correct |
21 ms |
10748 KB |
Output is correct |
50 |
Correct |
154 ms |
20076 KB |
Output is correct |
51 |
Correct |
16 ms |
10316 KB |
Output is correct |
52 |
Correct |
123 ms |
18692 KB |
Output is correct |
53 |
Correct |
21 ms |
10628 KB |
Output is correct |
54 |
Correct |
150 ms |
19456 KB |
Output is correct |
55 |
Correct |
12 ms |
10316 KB |
Output is correct |
56 |
Correct |
13 ms |
10316 KB |
Output is correct |
57 |
Correct |
15 ms |
10316 KB |
Output is correct |
58 |
Correct |
12 ms |
10316 KB |
Output is correct |
59 |
Correct |
10 ms |
9900 KB |
Output is correct |
60 |
Correct |
118 ms |
16960 KB |
Output is correct |
61 |
Correct |
15 ms |
10012 KB |
Output is correct |
62 |
Correct |
163 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9816 KB |
Output is correct |
8 |
Correct |
7 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9752 KB |
Output is correct |
10 |
Correct |
6 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9696 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
8 ms |
9672 KB |
Output is correct |
21 |
Correct |
8 ms |
9804 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
8 ms |
9856 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9804 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
7 ms |
9616 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9736 KB |
Output is correct |
32 |
Correct |
6 ms |
9688 KB |
Output is correct |
33 |
Correct |
9 ms |
9800 KB |
Output is correct |
34 |
Correct |
9 ms |
10112 KB |
Output is correct |
35 |
Correct |
129 ms |
17928 KB |
Output is correct |
36 |
Correct |
135 ms |
20072 KB |
Output is correct |
37 |
Correct |
176 ms |
20200 KB |
Output is correct |
38 |
Correct |
137 ms |
19876 KB |
Output is correct |
39 |
Correct |
9 ms |
9856 KB |
Output is correct |
40 |
Correct |
10 ms |
9984 KB |
Output is correct |
41 |
Correct |
9 ms |
9992 KB |
Output is correct |
42 |
Correct |
10 ms |
9804 KB |
Output is correct |
43 |
Correct |
11 ms |
9932 KB |
Output is correct |
44 |
Correct |
13 ms |
9932 KB |
Output is correct |
45 |
Correct |
10 ms |
9904 KB |
Output is correct |
46 |
Correct |
8 ms |
9804 KB |
Output is correct |
47 |
Correct |
10 ms |
9932 KB |
Output is correct |
48 |
Correct |
10 ms |
10004 KB |
Output is correct |
49 |
Correct |
21 ms |
10748 KB |
Output is correct |
50 |
Correct |
154 ms |
20076 KB |
Output is correct |
51 |
Correct |
16 ms |
10316 KB |
Output is correct |
52 |
Correct |
123 ms |
18692 KB |
Output is correct |
53 |
Correct |
21 ms |
10628 KB |
Output is correct |
54 |
Correct |
150 ms |
19456 KB |
Output is correct |
55 |
Correct |
12 ms |
10316 KB |
Output is correct |
56 |
Correct |
13 ms |
10316 KB |
Output is correct |
57 |
Correct |
15 ms |
10316 KB |
Output is correct |
58 |
Correct |
12 ms |
10316 KB |
Output is correct |
59 |
Correct |
10 ms |
9900 KB |
Output is correct |
60 |
Correct |
118 ms |
16960 KB |
Output is correct |
61 |
Correct |
15 ms |
10012 KB |
Output is correct |
62 |
Correct |
163 ms |
19796 KB |
Output is correct |
63 |
Correct |
587 ms |
55860 KB |
Output is correct |
64 |
Correct |
524 ms |
55820 KB |
Output is correct |
65 |
Correct |
552 ms |
55852 KB |
Output is correct |
66 |
Correct |
205 ms |
16964 KB |
Output is correct |
67 |
Correct |
343 ms |
22372 KB |
Output is correct |
68 |
Correct |
173 ms |
17024 KB |
Output is correct |
69 |
Correct |
489 ms |
23104 KB |
Output is correct |
70 |
Correct |
161 ms |
16944 KB |
Output is correct |
71 |
Correct |
180 ms |
17028 KB |
Output is correct |
72 |
Correct |
486 ms |
22488 KB |
Output is correct |
73 |
Correct |
496 ms |
22456 KB |
Output is correct |
74 |
Correct |
1350 ms |
34004 KB |
Output is correct |
75 |
Correct |
805 ms |
29092 KB |
Output is correct |
76 |
Correct |
1008 ms |
33020 KB |
Output is correct |
77 |
Correct |
1037 ms |
33080 KB |
Output is correct |
78 |
Correct |
263 ms |
21844 KB |
Output is correct |
79 |
Correct |
453 ms |
26112 KB |
Output is correct |
80 |
Correct |
304 ms |
21728 KB |
Output is correct |
81 |
Correct |
449 ms |
25616 KB |
Output is correct |
82 |
Correct |
834 ms |
41132 KB |
Output is correct |
83 |
Correct |
854 ms |
41092 KB |
Output is correct |
84 |
Correct |
899 ms |
41312 KB |
Output is correct |
85 |
Correct |
911 ms |
41376 KB |
Output is correct |
86 |
Correct |
197 ms |
16192 KB |
Output is correct |
87 |
Correct |
218 ms |
18940 KB |
Output is correct |
88 |
Correct |
471 ms |
22472 KB |
Output is correct |
89 |
Correct |
836 ms |
32436 KB |
Output is correct |