//besmellah
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
const int N = 3e5 + 5;
const long long INF = 1ll << 60;
int ans = 0;
namespace dsu {
int dpr[N];
set<array<int, 2>> in[N], out[N];
//sz[v] => sum of in[v].second
void init() {
memset(dpr, -1, sizeof dpr);
}
int gpr(int x) {
return dpr[x] < 0 ? x : dpr[x] = gpr(dpr[x]);
}
bool ise(int v, int u) {//u <- v
auto it = in[u].lower_bound({v, -1});
return it != in[u].end() && (*it)[0] == v;
}
void merge(int u, int v) {
u = gpr(u), v = gpr(v);
if (u == v) return;
if (in[u].size() + out[u].size() > in[v].size() + out[v].size()) swap(u, v);
auto it = in[u].lower_bound({v, -1});
while (it != in[u].end() && (*it)[0] == v) {
ans -= -dpr[u];
out[v].erase({u, (*it)[1]});
in[u].erase(it);
it = in[u].lower_bound({v, -1});
}
it = in[v].lower_bound({u, -1});
while (it != in[v].end() && (*it)[0] == u) {
ans -= -dpr[v];
out[u].erase({v, (*it)[1]});
in[v].erase(it);
it = in[v].lower_bound({u, -1});
}
ans += dpr[u] * dpr[v] * 2ll;
vector<int> q;
int only_v = in[v].size();
for (auto [rt, x]: in[u]) only_v -= in[v].count({rt, x});
ans += -dpr[u] * only_v;
for (auto [rt, x]: in[u]) if (!in[v].count({rt, x}))
ans += -dpr[v];
for (auto [rt, x]: in[u]) {
assert(rt != v);
in[v].insert({rt, x});
out[rt].erase({u, x});
out[rt].insert({v, x});
if (ise(v, rt) && ise(rt, v)) q.push_back(rt);
}
for (auto [rt, x]: out[u]) {
assert(rt != v);
out[v].insert({rt, x});
in[rt].erase({u, x});
in[rt].insert({v, x});
if (ise(v, rt) && ise(rt, v)) q.push_back(rt);
}
in[u].clear(), out[u].clear();
dpr[v] += dpr[u];
dpr[u] = v;
for (auto x: q) merge(x, v);
}
void E(int u, int v) {
int tu = u;
u = gpr(u), v = gpr(v);
if (u == v || in[v].count({u, tu})) return;
in[v].insert({u, tu});
out[u].insert({v, tu});
ans += -dpr[v];
if (ise(u, v) && ise(v, u)) {
merge(u, v);
}
}
}
int n, m;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
dsu::init();
cin >> n >> m;
FOR(i, 0, m) {
int u, v;
cin >> u >> v;
--u, --v;
dsu::E(u, v);
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
20 ms |
30812 KB |
Output is correct |
4 |
Correct |
16 ms |
30744 KB |
Output is correct |
5 |
Correct |
19 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
20 ms |
30812 KB |
Output is correct |
9 |
Correct |
21 ms |
30960 KB |
Output is correct |
10 |
Correct |
15 ms |
30832 KB |
Output is correct |
11 |
Correct |
21 ms |
30812 KB |
Output is correct |
12 |
Correct |
19 ms |
30812 KB |
Output is correct |
13 |
Correct |
20 ms |
30996 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
19 ms |
30812 KB |
Output is correct |
16 |
Correct |
19 ms |
30984 KB |
Output is correct |
17 |
Correct |
18 ms |
30812 KB |
Output is correct |
18 |
Correct |
24 ms |
30812 KB |
Output is correct |
19 |
Correct |
19 ms |
30812 KB |
Output is correct |
20 |
Correct |
17 ms |
30812 KB |
Output is correct |
21 |
Correct |
16 ms |
30812 KB |
Output is correct |
22 |
Correct |
16 ms |
30812 KB |
Output is correct |
23 |
Correct |
18 ms |
30812 KB |
Output is correct |
24 |
Correct |
20 ms |
30816 KB |
Output is correct |
25 |
Correct |
16 ms |
30812 KB |
Output is correct |
26 |
Correct |
16 ms |
30812 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
19 ms |
30812 KB |
Output is correct |
29 |
Correct |
15 ms |
30812 KB |
Output is correct |
30 |
Correct |
17 ms |
30812 KB |
Output is correct |
31 |
Correct |
19 ms |
30812 KB |
Output is correct |
32 |
Correct |
16 ms |
30812 KB |
Output is correct |
33 |
Correct |
24 ms |
30812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
20 ms |
30812 KB |
Output is correct |
4 |
Correct |
16 ms |
30744 KB |
Output is correct |
5 |
Correct |
19 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
20 ms |
30812 KB |
Output is correct |
9 |
Correct |
21 ms |
30960 KB |
Output is correct |
10 |
Correct |
15 ms |
30832 KB |
Output is correct |
11 |
Correct |
21 ms |
30812 KB |
Output is correct |
12 |
Correct |
19 ms |
30812 KB |
Output is correct |
13 |
Correct |
20 ms |
30996 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
19 ms |
30812 KB |
Output is correct |
16 |
Correct |
19 ms |
30984 KB |
Output is correct |
17 |
Correct |
18 ms |
30812 KB |
Output is correct |
18 |
Correct |
24 ms |
30812 KB |
Output is correct |
19 |
Correct |
19 ms |
30812 KB |
Output is correct |
20 |
Correct |
17 ms |
30812 KB |
Output is correct |
21 |
Correct |
16 ms |
30812 KB |
Output is correct |
22 |
Correct |
16 ms |
30812 KB |
Output is correct |
23 |
Correct |
18 ms |
30812 KB |
Output is correct |
24 |
Correct |
20 ms |
30816 KB |
Output is correct |
25 |
Correct |
16 ms |
30812 KB |
Output is correct |
26 |
Correct |
16 ms |
30812 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
19 ms |
30812 KB |
Output is correct |
29 |
Correct |
15 ms |
30812 KB |
Output is correct |
30 |
Correct |
17 ms |
30812 KB |
Output is correct |
31 |
Correct |
19 ms |
30812 KB |
Output is correct |
32 |
Correct |
16 ms |
30812 KB |
Output is correct |
33 |
Correct |
24 ms |
30812 KB |
Output is correct |
34 |
Correct |
21 ms |
31068 KB |
Output is correct |
35 |
Correct |
113 ms |
36668 KB |
Output is correct |
36 |
Correct |
135 ms |
39228 KB |
Output is correct |
37 |
Correct |
135 ms |
39484 KB |
Output is correct |
38 |
Correct |
142 ms |
39252 KB |
Output is correct |
39 |
Correct |
19 ms |
30896 KB |
Output is correct |
40 |
Correct |
16 ms |
31068 KB |
Output is correct |
41 |
Correct |
16 ms |
31068 KB |
Output is correct |
42 |
Correct |
10 ms |
30812 KB |
Output is correct |
43 |
Correct |
11 ms |
31068 KB |
Output is correct |
44 |
Correct |
20 ms |
31068 KB |
Output is correct |
45 |
Correct |
17 ms |
31260 KB |
Output is correct |
46 |
Correct |
19 ms |
30816 KB |
Output is correct |
47 |
Correct |
17 ms |
31068 KB |
Output is correct |
48 |
Correct |
23 ms |
31068 KB |
Output is correct |
49 |
Correct |
25 ms |
31836 KB |
Output is correct |
50 |
Correct |
122 ms |
39508 KB |
Output is correct |
51 |
Correct |
21 ms |
31324 KB |
Output is correct |
52 |
Correct |
93 ms |
38004 KB |
Output is correct |
53 |
Correct |
31 ms |
31832 KB |
Output is correct |
54 |
Correct |
117 ms |
38812 KB |
Output is correct |
55 |
Correct |
19 ms |
31524 KB |
Output is correct |
56 |
Correct |
18 ms |
31324 KB |
Output is correct |
57 |
Correct |
22 ms |
31836 KB |
Output is correct |
58 |
Correct |
20 ms |
32056 KB |
Output is correct |
59 |
Correct |
16 ms |
30812 KB |
Output is correct |
60 |
Correct |
76 ms |
35940 KB |
Output is correct |
61 |
Correct |
22 ms |
31072 KB |
Output is correct |
62 |
Correct |
106 ms |
39028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
20 ms |
30812 KB |
Output is correct |
4 |
Correct |
16 ms |
30744 KB |
Output is correct |
5 |
Correct |
19 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
20 ms |
30812 KB |
Output is correct |
9 |
Correct |
21 ms |
30960 KB |
Output is correct |
10 |
Correct |
15 ms |
30832 KB |
Output is correct |
11 |
Correct |
21 ms |
30812 KB |
Output is correct |
12 |
Correct |
19 ms |
30812 KB |
Output is correct |
13 |
Correct |
20 ms |
30996 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
19 ms |
30812 KB |
Output is correct |
16 |
Correct |
19 ms |
30984 KB |
Output is correct |
17 |
Correct |
18 ms |
30812 KB |
Output is correct |
18 |
Correct |
24 ms |
30812 KB |
Output is correct |
19 |
Correct |
19 ms |
30812 KB |
Output is correct |
20 |
Correct |
17 ms |
30812 KB |
Output is correct |
21 |
Correct |
16 ms |
30812 KB |
Output is correct |
22 |
Correct |
16 ms |
30812 KB |
Output is correct |
23 |
Correct |
18 ms |
30812 KB |
Output is correct |
24 |
Correct |
20 ms |
30816 KB |
Output is correct |
25 |
Correct |
16 ms |
30812 KB |
Output is correct |
26 |
Correct |
16 ms |
30812 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
19 ms |
30812 KB |
Output is correct |
29 |
Correct |
15 ms |
30812 KB |
Output is correct |
30 |
Correct |
17 ms |
30812 KB |
Output is correct |
31 |
Correct |
19 ms |
30812 KB |
Output is correct |
32 |
Correct |
16 ms |
30812 KB |
Output is correct |
33 |
Correct |
24 ms |
30812 KB |
Output is correct |
34 |
Correct |
21 ms |
31068 KB |
Output is correct |
35 |
Correct |
113 ms |
36668 KB |
Output is correct |
36 |
Correct |
135 ms |
39228 KB |
Output is correct |
37 |
Correct |
135 ms |
39484 KB |
Output is correct |
38 |
Correct |
142 ms |
39252 KB |
Output is correct |
39 |
Correct |
19 ms |
30896 KB |
Output is correct |
40 |
Correct |
16 ms |
31068 KB |
Output is correct |
41 |
Correct |
16 ms |
31068 KB |
Output is correct |
42 |
Correct |
10 ms |
30812 KB |
Output is correct |
43 |
Correct |
11 ms |
31068 KB |
Output is correct |
44 |
Correct |
20 ms |
31068 KB |
Output is correct |
45 |
Correct |
17 ms |
31260 KB |
Output is correct |
46 |
Correct |
19 ms |
30816 KB |
Output is correct |
47 |
Correct |
17 ms |
31068 KB |
Output is correct |
48 |
Correct |
23 ms |
31068 KB |
Output is correct |
49 |
Correct |
25 ms |
31836 KB |
Output is correct |
50 |
Correct |
122 ms |
39508 KB |
Output is correct |
51 |
Correct |
21 ms |
31324 KB |
Output is correct |
52 |
Correct |
93 ms |
38004 KB |
Output is correct |
53 |
Correct |
31 ms |
31832 KB |
Output is correct |
54 |
Correct |
117 ms |
38812 KB |
Output is correct |
55 |
Correct |
19 ms |
31524 KB |
Output is correct |
56 |
Correct |
18 ms |
31324 KB |
Output is correct |
57 |
Correct |
22 ms |
31836 KB |
Output is correct |
58 |
Correct |
20 ms |
32056 KB |
Output is correct |
59 |
Correct |
16 ms |
30812 KB |
Output is correct |
60 |
Correct |
76 ms |
35940 KB |
Output is correct |
61 |
Correct |
22 ms |
31072 KB |
Output is correct |
62 |
Correct |
106 ms |
39028 KB |
Output is correct |
63 |
Correct |
587 ms |
73744 KB |
Output is correct |
64 |
Correct |
506 ms |
74000 KB |
Output is correct |
65 |
Correct |
520 ms |
74004 KB |
Output is correct |
66 |
Correct |
93 ms |
35156 KB |
Output is correct |
67 |
Correct |
199 ms |
40740 KB |
Output is correct |
68 |
Correct |
100 ms |
35412 KB |
Output is correct |
69 |
Correct |
338 ms |
41552 KB |
Output is correct |
70 |
Correct |
104 ms |
35468 KB |
Output is correct |
71 |
Correct |
124 ms |
35244 KB |
Output is correct |
72 |
Correct |
245 ms |
41044 KB |
Output is correct |
73 |
Correct |
261 ms |
40788 KB |
Output is correct |
74 |
Correct |
920 ms |
52960 KB |
Output is correct |
75 |
Correct |
566 ms |
46992 KB |
Output is correct |
76 |
Correct |
644 ms |
51532 KB |
Output is correct |
77 |
Correct |
613 ms |
51628 KB |
Output is correct |
78 |
Correct |
205 ms |
40532 KB |
Output is correct |
79 |
Correct |
331 ms |
44048 KB |
Output is correct |
80 |
Correct |
237 ms |
40276 KB |
Output is correct |
81 |
Correct |
321 ms |
43604 KB |
Output is correct |
82 |
Correct |
657 ms |
59556 KB |
Output is correct |
83 |
Correct |
576 ms |
59552 KB |
Output is correct |
84 |
Correct |
541 ms |
83136 KB |
Output is correct |
85 |
Correct |
505 ms |
83120 KB |
Output is correct |
86 |
Correct |
91 ms |
34456 KB |
Output is correct |
87 |
Correct |
136 ms |
36664 KB |
Output is correct |
88 |
Correct |
229 ms |
40788 KB |
Output is correct |
89 |
Correct |
554 ms |
50408 KB |
Output is correct |