/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
int N, M;
int root[N_MAX + 2];
int cnt[N_MAX + 2];
set <int> in[N_MAX + 2];
set <int> ins[N_MAX + 2];
set <pair <int, int>> out[N_MAX + 2];
ll answer;
ll f (int u) {
return (ll) in[u].size() * cnt[u] + (ll) cnt[u] * (cnt[u] - 1);
}
int find_root (int u) {
return (root[u] == 0 ? u : root[u] = find_root(root[u]));
}
queue <pair <int, int>> join_que;
void del_edge (int u, int v) {
int ru = find_root(u), rv = find_root(v);
in[rv].erase(u);
out[ru].erase({u, rv});
}
bool add_edge (int u, int v) {
int ru = find_root(u), rv = find_root(v);
if (ru == rv || out[ru].find({u, rv}) != out[ru].end()) {
return false;
}
in[rv].insert(u);
out[ru].insert({u, rv});
ins[rv].insert(ru);
if (ins[ru].find(rv) != ins[ru].end()) {
join_que.push({ru, rv});
}
return true;
}
void join (int u, int v) {
u = find_root(u); v = find_root(v);
if (u == v) {
return;
}
if ((int) in[u].size() + (int) out[u].size()
> (int) in[v].size() + (int) out[v].size()) {
swap(u, v);
}
answer -= f(u) + f(v);
vector <pair <int, int>> del, add;
for (int x : in[u]) {
del.push_back({x, u});
if (find_root(x) != v) {
add.push_back({x, u});
}
}
for (pair <int, int> e : out[u]) {
del.push_back(e);
if (e.second != v) {
add.push_back(e);
}
}
for (pair <int, int> e : del) {
del_edge(e.first, e.second);
}
root[u] = v;
cnt[v] += cnt[u];
for (pair <int, int> e : add) {
add_edge(e.first, e.second);
}
answer += f(v);
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
fill(cnt + 1, cnt + N + 1, 1);
while (M--) {
int u, v;
cin >> u >> v;
if (add_edge(u, v) == true) {
answer += cnt[find_root(v)];
}
while (join_que.empty() == false) {
pair <int, int> e = join_que.front();
join_que.pop();
join(e.first, e.second);
}
cout << answer << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14984 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14964 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 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 |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14984 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14964 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 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 |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
65 ms |
20692 KB |
Output is correct |
36 |
Correct |
87 ms |
23124 KB |
Output is correct |
37 |
Correct |
86 ms |
23228 KB |
Output is correct |
38 |
Correct |
85 ms |
22740 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15196 KB |
Output is correct |
41 |
Correct |
6 ms |
15260 KB |
Output is correct |
42 |
Correct |
5 ms |
15196 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
5 ms |
15196 KB |
Output is correct |
46 |
Correct |
7 ms |
15196 KB |
Output is correct |
47 |
Correct |
6 ms |
15192 KB |
Output is correct |
48 |
Correct |
7 ms |
15312 KB |
Output is correct |
49 |
Correct |
14 ms |
15844 KB |
Output is correct |
50 |
Correct |
90 ms |
23132 KB |
Output is correct |
51 |
Correct |
9 ms |
15448 KB |
Output is correct |
52 |
Correct |
74 ms |
21584 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
22260 KB |
Output is correct |
55 |
Correct |
8 ms |
15704 KB |
Output is correct |
56 |
Correct |
8 ms |
15448 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
7 ms |
15452 KB |
Output is correct |
59 |
Correct |
5 ms |
15196 KB |
Output is correct |
60 |
Correct |
62 ms |
19680 KB |
Output is correct |
61 |
Correct |
7 ms |
15196 KB |
Output is correct |
62 |
Correct |
80 ms |
22612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14984 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14964 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 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 |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
65 ms |
20692 KB |
Output is correct |
36 |
Correct |
87 ms |
23124 KB |
Output is correct |
37 |
Correct |
86 ms |
23228 KB |
Output is correct |
38 |
Correct |
85 ms |
22740 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15196 KB |
Output is correct |
41 |
Correct |
6 ms |
15260 KB |
Output is correct |
42 |
Correct |
5 ms |
15196 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
5 ms |
15196 KB |
Output is correct |
46 |
Correct |
7 ms |
15196 KB |
Output is correct |
47 |
Correct |
6 ms |
15192 KB |
Output is correct |
48 |
Correct |
7 ms |
15312 KB |
Output is correct |
49 |
Correct |
14 ms |
15844 KB |
Output is correct |
50 |
Correct |
90 ms |
23132 KB |
Output is correct |
51 |
Correct |
9 ms |
15448 KB |
Output is correct |
52 |
Correct |
74 ms |
21584 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
22260 KB |
Output is correct |
55 |
Correct |
8 ms |
15704 KB |
Output is correct |
56 |
Correct |
8 ms |
15448 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
7 ms |
15452 KB |
Output is correct |
59 |
Correct |
5 ms |
15196 KB |
Output is correct |
60 |
Correct |
62 ms |
19680 KB |
Output is correct |
61 |
Correct |
7 ms |
15196 KB |
Output is correct |
62 |
Correct |
80 ms |
22612 KB |
Output is correct |
63 |
Correct |
432 ms |
62340 KB |
Output is correct |
64 |
Correct |
385 ms |
62356 KB |
Output is correct |
65 |
Correct |
381 ms |
62288 KB |
Output is correct |
66 |
Correct |
136 ms |
28956 KB |
Output is correct |
67 |
Correct |
236 ms |
31148 KB |
Output is correct |
68 |
Correct |
135 ms |
28984 KB |
Output is correct |
69 |
Correct |
280 ms |
30540 KB |
Output is correct |
70 |
Correct |
151 ms |
29096 KB |
Output is correct |
71 |
Correct |
137 ms |
29012 KB |
Output is correct |
72 |
Correct |
295 ms |
31452 KB |
Output is correct |
73 |
Correct |
305 ms |
31544 KB |
Output is correct |
74 |
Correct |
906 ms |
54840 KB |
Output is correct |
75 |
Correct |
418 ms |
36252 KB |
Output is correct |
76 |
Correct |
610 ms |
44764 KB |
Output is correct |
77 |
Correct |
573 ms |
44860 KB |
Output is correct |
78 |
Correct |
179 ms |
31512 KB |
Output is correct |
79 |
Correct |
302 ms |
35856 KB |
Output is correct |
80 |
Correct |
146 ms |
31380 KB |
Output is correct |
81 |
Correct |
215 ms |
34644 KB |
Output is correct |
82 |
Correct |
607 ms |
48272 KB |
Output is correct |
83 |
Correct |
589 ms |
47700 KB |
Output is correct |
84 |
Correct |
493 ms |
47152 KB |
Output is correct |
85 |
Correct |
534 ms |
47316 KB |
Output is correct |
86 |
Correct |
115 ms |
28240 KB |
Output is correct |
87 |
Correct |
139 ms |
30292 KB |
Output is correct |
88 |
Correct |
290 ms |
31572 KB |
Output is correct |
89 |
Correct |
602 ms |
42928 KB |
Output is correct |