#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
#define pb push_back
using pi = array<int, 2>;
const int N = 1e5 + 5;
int par[N], cnt[N];
set<int> in[N], out[N], inf[N];
set<pi> outf[N];
int ans = 0;
int get_cost(int u) {
int cost = 0;
// between
cost += cnt[u] * (cnt[u] - 1);
// coming in
cost += (int)inf[u].size() * cnt[u];
return cost;
}
int find(int u) {
if (par[par[u]] != par[u]) par[u] = find(par[u]);
return par[u];
}
void unite(int u, int v) {
u = find(u); v = find(v);
if (u == v) return;
if (cnt[u] < cnt[v]) swap(u, v);
in[u].erase(v);
in[v].erase(u);
out[u].erase(v);
out[v].erase(u);
for (int x : in[v]) {
in[u].insert(x);
out[x].erase(v);
out[x].insert(u);
}
for (int x : out[v]) {
out[u].insert(x);
in[x].erase(v);
in[x].insert(u);
}
for (auto x : outf[v]) {
//cout << u << " " << v << " || " << x[0] << " " << x[1] << endl;
if (find(x[0]) == u) {
inf[u].erase(x[1]);
continue;
}
outf[u].insert(x);
}
for (int x : inf[v]) {
//cout << u << " " << v << " | " << x << endl;
if (find(x) == u) {
continue;
}
inf[u].insert(x);
}
cnt[u] += cnt[v];
par[v] = u;
for (int x : in[v]) {
x = find(x);
if (in[u].count(x) && out[u].count(x)) {
ans -= get_cost(x);
unite(u, x);
}
}
for (int x : out[v]) {
x = find(x);
if (in[u].count(x) && out[u].count(x)) {
ans -= get_cost(x);
unite(u, x);
}
}
in[v].clear();
out[v].clear();
inf[v].clear();
cnt[v] = 0;
/*cout << inf[u].size() << "!\n";
for (int y : inf[u]) cout << y << " ";
cout << endl << endl;*/
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
par[i] = i;
cnt[i] = 1;
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
int fu = u, fv = v;
u = find(u); v = find(v);
if (u == v) {
cout << ans << endl;
continue;
}
ans -= get_cost(u) + get_cost(v);
inf[v].insert(fu);
outf[u].insert({fv, fu});
out[u].insert(v);
in[v].insert(u);
if (in[u].count(v) && out[u].count(v)) unite(u, v);
u = find(u); v = find(v);
ans += get_cost(u);
if (u != v) ans += get_cost(v);
cout << ans << endl;
}
}
/*
4 4
2 3
3 4
2 4
4 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19252 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19260 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
5 ms |
19264 KB |
Output is correct |
7 |
Correct |
5 ms |
19292 KB |
Output is correct |
8 |
Correct |
5 ms |
19288 KB |
Output is correct |
9 |
Correct |
6 ms |
19292 KB |
Output is correct |
10 |
Correct |
5 ms |
19032 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19052 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19268 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
5 ms |
19288 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
4 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19264 KB |
Output is correct |
26 |
Correct |
5 ms |
19128 KB |
Output is correct |
27 |
Correct |
4 ms |
19032 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19252 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19260 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
5 ms |
19264 KB |
Output is correct |
7 |
Correct |
5 ms |
19292 KB |
Output is correct |
8 |
Correct |
5 ms |
19288 KB |
Output is correct |
9 |
Correct |
6 ms |
19292 KB |
Output is correct |
10 |
Correct |
5 ms |
19032 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19052 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19268 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
5 ms |
19288 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
4 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19264 KB |
Output is correct |
26 |
Correct |
5 ms |
19128 KB |
Output is correct |
27 |
Correct |
4 ms |
19032 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
34 |
Correct |
8 ms |
19292 KB |
Output is correct |
35 |
Correct |
68 ms |
25872 KB |
Output is correct |
36 |
Correct |
88 ms |
29972 KB |
Output is correct |
37 |
Correct |
86 ms |
30036 KB |
Output is correct |
38 |
Correct |
89 ms |
29428 KB |
Output is correct |
39 |
Correct |
6 ms |
19548 KB |
Output is correct |
40 |
Correct |
8 ms |
20060 KB |
Output is correct |
41 |
Correct |
7 ms |
19804 KB |
Output is correct |
42 |
Correct |
6 ms |
19548 KB |
Output is correct |
43 |
Correct |
8 ms |
19548 KB |
Output is correct |
44 |
Correct |
7 ms |
19548 KB |
Output is correct |
45 |
Correct |
6 ms |
19548 KB |
Output is correct |
46 |
Correct |
6 ms |
19548 KB |
Output is correct |
47 |
Correct |
8 ms |
19776 KB |
Output is correct |
48 |
Correct |
7 ms |
19800 KB |
Output is correct |
49 |
Correct |
14 ms |
20828 KB |
Output is correct |
50 |
Correct |
89 ms |
30104 KB |
Output is correct |
51 |
Correct |
11 ms |
20060 KB |
Output is correct |
52 |
Correct |
74 ms |
27620 KB |
Output is correct |
53 |
Correct |
15 ms |
20796 KB |
Output is correct |
54 |
Correct |
81 ms |
28724 KB |
Output is correct |
55 |
Correct |
9 ms |
20060 KB |
Output is correct |
56 |
Correct |
10 ms |
20060 KB |
Output is correct |
57 |
Correct |
11 ms |
20572 KB |
Output is correct |
58 |
Correct |
10 ms |
20572 KB |
Output is correct |
59 |
Correct |
7 ms |
20060 KB |
Output is correct |
60 |
Correct |
58 ms |
24916 KB |
Output is correct |
61 |
Correct |
9 ms |
19804 KB |
Output is correct |
62 |
Correct |
96 ms |
29232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19252 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19260 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
5 ms |
19264 KB |
Output is correct |
7 |
Correct |
5 ms |
19292 KB |
Output is correct |
8 |
Correct |
5 ms |
19288 KB |
Output is correct |
9 |
Correct |
6 ms |
19292 KB |
Output is correct |
10 |
Correct |
5 ms |
19032 KB |
Output is correct |
11 |
Correct |
5 ms |
19036 KB |
Output is correct |
12 |
Correct |
5 ms |
19052 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
4 ms |
19036 KB |
Output is correct |
16 |
Correct |
4 ms |
19268 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
4 ms |
19036 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
5 ms |
19288 KB |
Output is correct |
21 |
Correct |
5 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
4 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19264 KB |
Output is correct |
26 |
Correct |
5 ms |
19128 KB |
Output is correct |
27 |
Correct |
4 ms |
19032 KB |
Output is correct |
28 |
Correct |
4 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
4 ms |
19036 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19036 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
34 |
Correct |
8 ms |
19292 KB |
Output is correct |
35 |
Correct |
68 ms |
25872 KB |
Output is correct |
36 |
Correct |
88 ms |
29972 KB |
Output is correct |
37 |
Correct |
86 ms |
30036 KB |
Output is correct |
38 |
Correct |
89 ms |
29428 KB |
Output is correct |
39 |
Correct |
6 ms |
19548 KB |
Output is correct |
40 |
Correct |
8 ms |
20060 KB |
Output is correct |
41 |
Correct |
7 ms |
19804 KB |
Output is correct |
42 |
Correct |
6 ms |
19548 KB |
Output is correct |
43 |
Correct |
8 ms |
19548 KB |
Output is correct |
44 |
Correct |
7 ms |
19548 KB |
Output is correct |
45 |
Correct |
6 ms |
19548 KB |
Output is correct |
46 |
Correct |
6 ms |
19548 KB |
Output is correct |
47 |
Correct |
8 ms |
19776 KB |
Output is correct |
48 |
Correct |
7 ms |
19800 KB |
Output is correct |
49 |
Correct |
14 ms |
20828 KB |
Output is correct |
50 |
Correct |
89 ms |
30104 KB |
Output is correct |
51 |
Correct |
11 ms |
20060 KB |
Output is correct |
52 |
Correct |
74 ms |
27620 KB |
Output is correct |
53 |
Correct |
15 ms |
20796 KB |
Output is correct |
54 |
Correct |
81 ms |
28724 KB |
Output is correct |
55 |
Correct |
9 ms |
20060 KB |
Output is correct |
56 |
Correct |
10 ms |
20060 KB |
Output is correct |
57 |
Correct |
11 ms |
20572 KB |
Output is correct |
58 |
Correct |
10 ms |
20572 KB |
Output is correct |
59 |
Correct |
7 ms |
20060 KB |
Output is correct |
60 |
Correct |
58 ms |
24916 KB |
Output is correct |
61 |
Correct |
9 ms |
19804 KB |
Output is correct |
62 |
Correct |
96 ms |
29232 KB |
Output is correct |
63 |
Correct |
563 ms |
87124 KB |
Output is correct |
64 |
Correct |
575 ms |
87284 KB |
Output is correct |
65 |
Correct |
574 ms |
87304 KB |
Output is correct |
66 |
Correct |
189 ms |
37744 KB |
Output is correct |
67 |
Correct |
399 ms |
72228 KB |
Output is correct |
68 |
Correct |
173 ms |
37752 KB |
Output is correct |
69 |
Correct |
367 ms |
41304 KB |
Output is correct |
70 |
Correct |
172 ms |
37516 KB |
Output is correct |
71 |
Correct |
179 ms |
37880 KB |
Output is correct |
72 |
Correct |
410 ms |
51352 KB |
Output is correct |
73 |
Correct |
422 ms |
53616 KB |
Output is correct |
74 |
Correct |
1054 ms |
79220 KB |
Output is correct |
75 |
Correct |
534 ms |
49380 KB |
Output is correct |
76 |
Correct |
744 ms |
63236 KB |
Output is correct |
77 |
Correct |
727 ms |
63380 KB |
Output is correct |
78 |
Correct |
175 ms |
40532 KB |
Output is correct |
79 |
Correct |
340 ms |
46716 KB |
Output is correct |
80 |
Correct |
191 ms |
42072 KB |
Output is correct |
81 |
Correct |
336 ms |
47700 KB |
Output is correct |
82 |
Correct |
676 ms |
66020 KB |
Output is correct |
83 |
Correct |
764 ms |
66016 KB |
Output is correct |
84 |
Correct |
606 ms |
87076 KB |
Output is correct |
85 |
Correct |
603 ms |
87224 KB |
Output is correct |
86 |
Correct |
287 ms |
81632 KB |
Output is correct |
87 |
Correct |
280 ms |
83852 KB |
Output is correct |
88 |
Correct |
441 ms |
52332 KB |
Output is correct |
89 |
Correct |
741 ms |
59220 KB |
Output is correct |