#include <iostream>
#include <vector>
#include <map>
#include <vector>
#include <array>
#include <queue>
#define ll long long
using namespace std;
vector <ll> V[100000];
queue <array<ll, 2>> Q;
map <ll, ll> adj[100000], radj[100000], cyc[100000], rcyc[100000];
ll n, m, a, b, f, P[100000], sz[100000];
ll dsu(ll u) {
if (P[u] == u) return u;
else return P[u] = dsu(P[u]);
}
void merge(ll a, ll b) {
a = dsu(a), b = dsu(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
for (auto [x, y] : cyc[b]) {
if (x == a) continue;
++cyc[a][x];
++rcyc[x][a];
if (cyc[x].count(a)) Q.push({x, a});
}
for (auto [x, y] : rcyc[b]) {
if (x == a) continue;
++rcyc[a][x];
++cyc[x][a];
if (cyc[a].count(x)) Q.push({x, a});
}
cyc[a].erase(b);
rcyc[a].erase(b);
cyc[b].clear();
rcyc[b].clear();
for (auto [x, y] : radj[b]) {
if (dsu(x) == a || adj[x].count(a)) f -= sz[b];
else {
++adj[x][a];
++radj[a][x];
f += sz[a];
f -= sz[b];
}
adj[x].erase(b);
}
radj[b].clear();
for (auto u : V[b]) {
if (adj[u].count(a)) {
adj[u].erase(a);
radj[a].erase(u);
f -= sz[a];
}
V[a].push_back(u);
}
V[b].clear();
f += radj[a].size() * sz[b];
f += sz[a] * sz[b] * 2;
sz[a] += sz[b];
P[b] = a;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i=0; i<n; ++i) P[i] = i, sz[i] = 1, V[i].push_back(i);
for (int i=0; i<m; ++i) {
cin >> a >> b;
--a, --b;
if (dsu(a) == dsu(b)) {
cout << f << '\n';
continue;
}
b = dsu(b);
if (!adj[a].count(b)) {
++adj[a][b];
++radj[b][a];
f += sz[b];
}
a = dsu(a);
++cyc[a][b];
++rcyc[b][a];
if (!cyc[b].count(a)) {
cout << f << '\n';
continue;
}
Q.push({a, b});
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
merge(x, y);
}
cout << f << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
3 ms |
22988 KB |
Output is correct |
3 |
Correct |
3 ms |
22876 KB |
Output is correct |
4 |
Correct |
3 ms |
22876 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
3 ms |
22984 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
3 ms |
22876 KB |
Output is correct |
10 |
Correct |
3 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22872 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22872 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
3 ms |
22876 KB |
Output is correct |
18 |
Correct |
3 ms |
22876 KB |
Output is correct |
19 |
Correct |
3 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
3 ms |
23000 KB |
Output is correct |
22 |
Correct |
3 ms |
22876 KB |
Output is correct |
23 |
Correct |
3 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
4 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
23132 KB |
Output is correct |
27 |
Correct |
3 ms |
22876 KB |
Output is correct |
28 |
Correct |
3 ms |
22876 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
3 ms |
22984 KB |
Output is correct |
31 |
Correct |
4 ms |
22876 KB |
Output is correct |
32 |
Correct |
4 ms |
22876 KB |
Output is correct |
33 |
Correct |
5 ms |
22996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
3 ms |
22988 KB |
Output is correct |
3 |
Correct |
3 ms |
22876 KB |
Output is correct |
4 |
Correct |
3 ms |
22876 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
3 ms |
22984 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
3 ms |
22876 KB |
Output is correct |
10 |
Correct |
3 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22872 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22872 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
3 ms |
22876 KB |
Output is correct |
18 |
Correct |
3 ms |
22876 KB |
Output is correct |
19 |
Correct |
3 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
3 ms |
23000 KB |
Output is correct |
22 |
Correct |
3 ms |
22876 KB |
Output is correct |
23 |
Correct |
3 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
4 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
23132 KB |
Output is correct |
27 |
Correct |
3 ms |
22876 KB |
Output is correct |
28 |
Correct |
3 ms |
22876 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
3 ms |
22984 KB |
Output is correct |
31 |
Correct |
4 ms |
22876 KB |
Output is correct |
32 |
Correct |
4 ms |
22876 KB |
Output is correct |
33 |
Correct |
5 ms |
22996 KB |
Output is correct |
34 |
Correct |
5 ms |
23132 KB |
Output is correct |
35 |
Correct |
46 ms |
29784 KB |
Output is correct |
36 |
Correct |
68 ms |
34180 KB |
Output is correct |
37 |
Correct |
68 ms |
34384 KB |
Output is correct |
38 |
Correct |
69 ms |
33616 KB |
Output is correct |
39 |
Correct |
4 ms |
23132 KB |
Output is correct |
40 |
Correct |
6 ms |
23644 KB |
Output is correct |
41 |
Correct |
7 ms |
23644 KB |
Output is correct |
42 |
Correct |
4 ms |
23132 KB |
Output is correct |
43 |
Correct |
6 ms |
23388 KB |
Output is correct |
44 |
Correct |
6 ms |
23192 KB |
Output is correct |
45 |
Correct |
5 ms |
23132 KB |
Output is correct |
46 |
Correct |
4 ms |
23132 KB |
Output is correct |
47 |
Correct |
6 ms |
23388 KB |
Output is correct |
48 |
Correct |
8 ms |
23388 KB |
Output is correct |
49 |
Correct |
13 ms |
24668 KB |
Output is correct |
50 |
Correct |
73 ms |
34568 KB |
Output is correct |
51 |
Correct |
8 ms |
23640 KB |
Output is correct |
52 |
Correct |
54 ms |
31312 KB |
Output is correct |
53 |
Correct |
12 ms |
24664 KB |
Output is correct |
54 |
Correct |
60 ms |
32932 KB |
Output is correct |
55 |
Correct |
10 ms |
24152 KB |
Output is correct |
56 |
Correct |
8 ms |
24156 KB |
Output is correct |
57 |
Correct |
7 ms |
24144 KB |
Output is correct |
58 |
Correct |
7 ms |
24156 KB |
Output is correct |
59 |
Correct |
5 ms |
23132 KB |
Output is correct |
60 |
Correct |
40 ms |
28024 KB |
Output is correct |
61 |
Correct |
7 ms |
23388 KB |
Output is correct |
62 |
Correct |
62 ms |
33300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
3 ms |
22988 KB |
Output is correct |
3 |
Correct |
3 ms |
22876 KB |
Output is correct |
4 |
Correct |
3 ms |
22876 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
3 ms |
22984 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
3 ms |
22876 KB |
Output is correct |
10 |
Correct |
3 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22872 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22872 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
3 ms |
22876 KB |
Output is correct |
18 |
Correct |
3 ms |
22876 KB |
Output is correct |
19 |
Correct |
3 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
3 ms |
23000 KB |
Output is correct |
22 |
Correct |
3 ms |
22876 KB |
Output is correct |
23 |
Correct |
3 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
4 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
23132 KB |
Output is correct |
27 |
Correct |
3 ms |
22876 KB |
Output is correct |
28 |
Correct |
3 ms |
22876 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
3 ms |
22984 KB |
Output is correct |
31 |
Correct |
4 ms |
22876 KB |
Output is correct |
32 |
Correct |
4 ms |
22876 KB |
Output is correct |
33 |
Correct |
5 ms |
22996 KB |
Output is correct |
34 |
Correct |
5 ms |
23132 KB |
Output is correct |
35 |
Correct |
46 ms |
29784 KB |
Output is correct |
36 |
Correct |
68 ms |
34180 KB |
Output is correct |
37 |
Correct |
68 ms |
34384 KB |
Output is correct |
38 |
Correct |
69 ms |
33616 KB |
Output is correct |
39 |
Correct |
4 ms |
23132 KB |
Output is correct |
40 |
Correct |
6 ms |
23644 KB |
Output is correct |
41 |
Correct |
7 ms |
23644 KB |
Output is correct |
42 |
Correct |
4 ms |
23132 KB |
Output is correct |
43 |
Correct |
6 ms |
23388 KB |
Output is correct |
44 |
Correct |
6 ms |
23192 KB |
Output is correct |
45 |
Correct |
5 ms |
23132 KB |
Output is correct |
46 |
Correct |
4 ms |
23132 KB |
Output is correct |
47 |
Correct |
6 ms |
23388 KB |
Output is correct |
48 |
Correct |
8 ms |
23388 KB |
Output is correct |
49 |
Correct |
13 ms |
24668 KB |
Output is correct |
50 |
Correct |
73 ms |
34568 KB |
Output is correct |
51 |
Correct |
8 ms |
23640 KB |
Output is correct |
52 |
Correct |
54 ms |
31312 KB |
Output is correct |
53 |
Correct |
12 ms |
24664 KB |
Output is correct |
54 |
Correct |
60 ms |
32932 KB |
Output is correct |
55 |
Correct |
10 ms |
24152 KB |
Output is correct |
56 |
Correct |
8 ms |
24156 KB |
Output is correct |
57 |
Correct |
7 ms |
24144 KB |
Output is correct |
58 |
Correct |
7 ms |
24156 KB |
Output is correct |
59 |
Correct |
5 ms |
23132 KB |
Output is correct |
60 |
Correct |
40 ms |
28024 KB |
Output is correct |
61 |
Correct |
7 ms |
23388 KB |
Output is correct |
62 |
Correct |
62 ms |
33300 KB |
Output is correct |
63 |
Correct |
412 ms |
106664 KB |
Output is correct |
64 |
Correct |
372 ms |
106628 KB |
Output is correct |
65 |
Correct |
406 ms |
106984 KB |
Output is correct |
66 |
Correct |
67 ms |
31552 KB |
Output is correct |
67 |
Correct |
384 ms |
68412 KB |
Output is correct |
68 |
Correct |
78 ms |
31500 KB |
Output is correct |
69 |
Correct |
313 ms |
43980 KB |
Output is correct |
70 |
Correct |
79 ms |
31484 KB |
Output is correct |
71 |
Correct |
83 ms |
31400 KB |
Output is correct |
72 |
Correct |
291 ms |
47176 KB |
Output is correct |
73 |
Correct |
300 ms |
47552 KB |
Output is correct |
74 |
Correct |
1104 ms |
91720 KB |
Output is correct |
75 |
Correct |
406 ms |
53700 KB |
Output is correct |
76 |
Correct |
673 ms |
69840 KB |
Output is correct |
77 |
Correct |
638 ms |
70084 KB |
Output is correct |
78 |
Correct |
154 ms |
45624 KB |
Output is correct |
79 |
Correct |
255 ms |
49472 KB |
Output is correct |
80 |
Correct |
130 ms |
45512 KB |
Output is correct |
81 |
Correct |
210 ms |
49088 KB |
Output is correct |
82 |
Correct |
728 ms |
81264 KB |
Output is correct |
83 |
Correct |
725 ms |
81228 KB |
Output is correct |
84 |
Correct |
542 ms |
81176 KB |
Output is correct |
85 |
Correct |
542 ms |
81028 KB |
Output is correct |
86 |
Correct |
97 ms |
41660 KB |
Output is correct |
87 |
Correct |
96 ms |
43704 KB |
Output is correct |
88 |
Correct |
298 ms |
47528 KB |
Output is correct |
89 |
Correct |
585 ms |
66844 KB |
Output is correct |