#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
ll par[100010];
ll sz[100010];
set<ll> in[100010], out[100010], all[100010];
ll Find(ll x) {
if (x == par[x]) return x;
return par[x] = Find(par[x]);
}
ll ans = 0;
vector<pll> mrg;
void add_edge(ll u, ll v) {
in[v].insert(u);
out[u].insert(v);
if (in[u].count(v)) mrg.push_back({u, v});
}
void Union(ll x, ll y) {
x = Find(x);
y = Find(y);
if (x == y) return;
if (all[x].size() < all[y].size()) swap(x, y);
ans += all[x].size() * sz[y] + all[y].size() * sz[x];
sz[x] += sz[y];
par[y] = x;
for (auto &v : all[y]) {
if (all[x].count(v)) ans -= sz[x];
else all[x].insert(v);
}
in[y].erase(x); out[y].erase(x); in[x].erase(y); out[x].erase(y);
for (auto &v : in[y]) {
add_edge(v, x);
}
for (auto &v : out[y]) {
add_edge(x, v);
}
}
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
ll N, M;
cin >> N >> M;
for (ll i=1; i<=N; i++) {
par[i] = i;
sz[i] = 1;
all[i].insert(i);
}
for (ll i=0; i<M; i++) {
ll u, v;
cin >> u >> v;
v = Find(v);
if (all[v].count(u)) {
cout << ans << '\n';
continue;
}
all[v].insert(u);
ans += sz[v];
u = Find(u);
add_edge(u, v);
while (!mrg.empty()) {
auto t = mrg.back();
mrg.pop_back();
Union(t.first, t.second);
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14964 KB |
Output is correct |
10 |
Correct |
4 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14768 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
15000 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14960 KB |
Output is correct |
25 |
Correct |
4 ms |
15008 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14964 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14996 KB |
Output is correct |
33 |
Correct |
4 ms |
15092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14964 KB |
Output is correct |
10 |
Correct |
4 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14768 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
15000 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14960 KB |
Output is correct |
25 |
Correct |
4 ms |
15008 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14964 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14996 KB |
Output is correct |
33 |
Correct |
4 ms |
15092 KB |
Output is correct |
34 |
Correct |
8 ms |
15192 KB |
Output is correct |
35 |
Correct |
81 ms |
21192 KB |
Output is correct |
36 |
Correct |
111 ms |
24276 KB |
Output is correct |
37 |
Correct |
112 ms |
24408 KB |
Output is correct |
38 |
Correct |
137 ms |
24164 KB |
Output is correct |
39 |
Correct |
6 ms |
15196 KB |
Output is correct |
40 |
Correct |
9 ms |
16076 KB |
Output is correct |
41 |
Correct |
8 ms |
16220 KB |
Output is correct |
42 |
Correct |
6 ms |
15196 KB |
Output is correct |
43 |
Correct |
7 ms |
15196 KB |
Output is correct |
44 |
Correct |
9 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15284 KB |
Output is correct |
46 |
Correct |
6 ms |
15192 KB |
Output is correct |
47 |
Correct |
8 ms |
15708 KB |
Output is correct |
48 |
Correct |
8 ms |
15708 KB |
Output is correct |
49 |
Correct |
17 ms |
16728 KB |
Output is correct |
50 |
Correct |
110 ms |
24524 KB |
Output is correct |
51 |
Correct |
10 ms |
15708 KB |
Output is correct |
52 |
Correct |
100 ms |
22820 KB |
Output is correct |
53 |
Correct |
16 ms |
16476 KB |
Output is correct |
54 |
Correct |
103 ms |
23632 KB |
Output is correct |
55 |
Correct |
9 ms |
15964 KB |
Output is correct |
56 |
Correct |
9 ms |
15784 KB |
Output is correct |
57 |
Correct |
10 ms |
15964 KB |
Output is correct |
58 |
Correct |
9 ms |
15964 KB |
Output is correct |
59 |
Correct |
8 ms |
15708 KB |
Output is correct |
60 |
Correct |
81 ms |
20580 KB |
Output is correct |
61 |
Correct |
8 ms |
15704 KB |
Output is correct |
62 |
Correct |
105 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14964 KB |
Output is correct |
10 |
Correct |
4 ms |
14936 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14768 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
15000 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14960 KB |
Output is correct |
25 |
Correct |
4 ms |
15008 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14964 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14996 KB |
Output is correct |
33 |
Correct |
4 ms |
15092 KB |
Output is correct |
34 |
Correct |
8 ms |
15192 KB |
Output is correct |
35 |
Correct |
81 ms |
21192 KB |
Output is correct |
36 |
Correct |
111 ms |
24276 KB |
Output is correct |
37 |
Correct |
112 ms |
24408 KB |
Output is correct |
38 |
Correct |
137 ms |
24164 KB |
Output is correct |
39 |
Correct |
6 ms |
15196 KB |
Output is correct |
40 |
Correct |
9 ms |
16076 KB |
Output is correct |
41 |
Correct |
8 ms |
16220 KB |
Output is correct |
42 |
Correct |
6 ms |
15196 KB |
Output is correct |
43 |
Correct |
7 ms |
15196 KB |
Output is correct |
44 |
Correct |
9 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15284 KB |
Output is correct |
46 |
Correct |
6 ms |
15192 KB |
Output is correct |
47 |
Correct |
8 ms |
15708 KB |
Output is correct |
48 |
Correct |
8 ms |
15708 KB |
Output is correct |
49 |
Correct |
17 ms |
16728 KB |
Output is correct |
50 |
Correct |
110 ms |
24524 KB |
Output is correct |
51 |
Correct |
10 ms |
15708 KB |
Output is correct |
52 |
Correct |
100 ms |
22820 KB |
Output is correct |
53 |
Correct |
16 ms |
16476 KB |
Output is correct |
54 |
Correct |
103 ms |
23632 KB |
Output is correct |
55 |
Correct |
9 ms |
15964 KB |
Output is correct |
56 |
Correct |
9 ms |
15784 KB |
Output is correct |
57 |
Correct |
10 ms |
15964 KB |
Output is correct |
58 |
Correct |
9 ms |
15964 KB |
Output is correct |
59 |
Correct |
8 ms |
15708 KB |
Output is correct |
60 |
Correct |
81 ms |
20580 KB |
Output is correct |
61 |
Correct |
8 ms |
15704 KB |
Output is correct |
62 |
Correct |
105 ms |
23756 KB |
Output is correct |
63 |
Correct |
699 ms |
68656 KB |
Output is correct |
64 |
Correct |
712 ms |
68624 KB |
Output is correct |
65 |
Correct |
694 ms |
68688 KB |
Output is correct |
66 |
Correct |
222 ms |
34492 KB |
Output is correct |
67 |
Correct |
997 ms |
101580 KB |
Output is correct |
68 |
Correct |
200 ms |
34592 KB |
Output is correct |
69 |
Correct |
444 ms |
35908 KB |
Output is correct |
70 |
Correct |
210 ms |
34816 KB |
Output is correct |
71 |
Correct |
220 ms |
34388 KB |
Output is correct |
72 |
Correct |
676 ms |
57140 KB |
Output is correct |
73 |
Correct |
706 ms |
59948 KB |
Output is correct |
74 |
Correct |
2225 ms |
113280 KB |
Output is correct |
75 |
Correct |
755 ms |
47184 KB |
Output is correct |
76 |
Correct |
1286 ms |
72908 KB |
Output is correct |
77 |
Correct |
1292 ms |
73024 KB |
Output is correct |
78 |
Correct |
293 ms |
46356 KB |
Output is correct |
79 |
Correct |
573 ms |
52000 KB |
Output is correct |
80 |
Correct |
251 ms |
40552 KB |
Output is correct |
81 |
Correct |
446 ms |
44600 KB |
Output is correct |
82 |
Correct |
998 ms |
66440 KB |
Output is correct |
83 |
Correct |
1036 ms |
66452 KB |
Output is correct |
84 |
Correct |
995 ms |
70596 KB |
Output is correct |
85 |
Correct |
1030 ms |
70968 KB |
Output is correct |
86 |
Correct |
360 ms |
67212 KB |
Output is correct |
87 |
Correct |
380 ms |
69456 KB |
Output is correct |
88 |
Correct |
806 ms |
58600 KB |
Output is correct |
89 |
Correct |
1499 ms |
70100 KB |
Output is correct |