#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define sz(Z) ll(size(Z))
const int MAX_N = 100'001;
int N, M;
int P[MAX_N];
ll in[MAX_N], S[MAX_N], edge_count;
set<int> g[MAX_N], h[MAX_N];
unordered_map<int, int> out[MAX_N];
queue<array<int, 2>> Q;
int find(int u){
return P[u] == u ? u : P[u] = find(P[u]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for(int u = 1; u <= N; ++u)
P[u] = u, S[u] = 1;
while(M--){
int A, B;
cin >> A >> B;
int u = find(A), v = find(B);
if(u != v && h[v].find(A) == end(h[v])) {
if(g[v].find(u) != end(g[v]))
Q.push({u, v});
edge_count += S[v];
h[v].insert(A);
g[u].insert(v);
++out[u][v], ++in[v];
}
while(!empty(Q)) {
u = find(Q.front()[0]), v = find(Q.front()[1]);
Q.pop();
if(u == v) continue;
if(sz(g[u]) + sz(h[u]) < sz(g[v]) + sz(h[v]))
swap(u, v);
edge_count += (S[u] - out[u][v]) * S[v];
edge_count += (S[v] - out[v][u]) * S[u];
in[u] -= out[v][u];
in[v] -= out[u][v];
for(int w : g[v]) {
g[u].insert(w);
w = find(w);
if(g[w].find(u) != end(g[w])) Q.push({u, w});
out[u][w] += out[v][w];
out[v][w] = 0;
}
for(int w : h[v]) {
if(find(w) != u && find(w) != v) {
if(g[u].find(find(w)) != end(g[u])) Q.push({u, w});
if(h[u].find(w) != end(h[u])) {
edge_count -= S[u];
--in[u];
} else {
g[find(w)].insert(u);
++out[find(w)][u];
}
}
}
edge_count += in[u] * S[v] + in[v] * S[u];
P[v] = u, S[u] += S[v];
in[u] += in[v];
for(int w : h[v]) h[u].insert(w);
}
cout << edge_count << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15104 KB |
Output is correct |
3 |
Correct |
12 ms |
15148 KB |
Output is correct |
4 |
Correct |
10 ms |
15160 KB |
Output is correct |
5 |
Correct |
13 ms |
15200 KB |
Output is correct |
6 |
Correct |
10 ms |
15180 KB |
Output is correct |
7 |
Correct |
11 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15212 KB |
Output is correct |
9 |
Correct |
10 ms |
15180 KB |
Output is correct |
10 |
Correct |
10 ms |
15180 KB |
Output is correct |
11 |
Correct |
10 ms |
15204 KB |
Output is correct |
12 |
Correct |
10 ms |
15132 KB |
Output is correct |
13 |
Correct |
10 ms |
15180 KB |
Output is correct |
14 |
Correct |
10 ms |
15180 KB |
Output is correct |
15 |
Correct |
11 ms |
15208 KB |
Output is correct |
16 |
Correct |
10 ms |
15180 KB |
Output is correct |
17 |
Correct |
11 ms |
15192 KB |
Output is correct |
18 |
Correct |
11 ms |
15180 KB |
Output is correct |
19 |
Correct |
10 ms |
15180 KB |
Output is correct |
20 |
Correct |
10 ms |
15200 KB |
Output is correct |
21 |
Correct |
11 ms |
15180 KB |
Output is correct |
22 |
Correct |
10 ms |
15196 KB |
Output is correct |
23 |
Correct |
11 ms |
15180 KB |
Output is correct |
24 |
Correct |
11 ms |
15204 KB |
Output is correct |
25 |
Correct |
12 ms |
15208 KB |
Output is correct |
26 |
Correct |
10 ms |
15180 KB |
Output is correct |
27 |
Correct |
10 ms |
15180 KB |
Output is correct |
28 |
Correct |
10 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15152 KB |
Output is correct |
30 |
Correct |
10 ms |
15136 KB |
Output is correct |
31 |
Correct |
12 ms |
15228 KB |
Output is correct |
32 |
Correct |
10 ms |
15308 KB |
Output is correct |
33 |
Correct |
12 ms |
15180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15104 KB |
Output is correct |
3 |
Correct |
12 ms |
15148 KB |
Output is correct |
4 |
Correct |
10 ms |
15160 KB |
Output is correct |
5 |
Correct |
13 ms |
15200 KB |
Output is correct |
6 |
Correct |
10 ms |
15180 KB |
Output is correct |
7 |
Correct |
11 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15212 KB |
Output is correct |
9 |
Correct |
10 ms |
15180 KB |
Output is correct |
10 |
Correct |
10 ms |
15180 KB |
Output is correct |
11 |
Correct |
10 ms |
15204 KB |
Output is correct |
12 |
Correct |
10 ms |
15132 KB |
Output is correct |
13 |
Correct |
10 ms |
15180 KB |
Output is correct |
14 |
Correct |
10 ms |
15180 KB |
Output is correct |
15 |
Correct |
11 ms |
15208 KB |
Output is correct |
16 |
Correct |
10 ms |
15180 KB |
Output is correct |
17 |
Correct |
11 ms |
15192 KB |
Output is correct |
18 |
Correct |
11 ms |
15180 KB |
Output is correct |
19 |
Correct |
10 ms |
15180 KB |
Output is correct |
20 |
Correct |
10 ms |
15200 KB |
Output is correct |
21 |
Correct |
11 ms |
15180 KB |
Output is correct |
22 |
Correct |
10 ms |
15196 KB |
Output is correct |
23 |
Correct |
11 ms |
15180 KB |
Output is correct |
24 |
Correct |
11 ms |
15204 KB |
Output is correct |
25 |
Correct |
12 ms |
15208 KB |
Output is correct |
26 |
Correct |
10 ms |
15180 KB |
Output is correct |
27 |
Correct |
10 ms |
15180 KB |
Output is correct |
28 |
Correct |
10 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15152 KB |
Output is correct |
30 |
Correct |
10 ms |
15136 KB |
Output is correct |
31 |
Correct |
12 ms |
15228 KB |
Output is correct |
32 |
Correct |
10 ms |
15308 KB |
Output is correct |
33 |
Correct |
12 ms |
15180 KB |
Output is correct |
34 |
Correct |
13 ms |
15308 KB |
Output is correct |
35 |
Correct |
105 ms |
19500 KB |
Output is correct |
36 |
Correct |
137 ms |
22164 KB |
Output is correct |
37 |
Correct |
138 ms |
22320 KB |
Output is correct |
38 |
Correct |
137 ms |
21832 KB |
Output is correct |
39 |
Correct |
15 ms |
16060 KB |
Output is correct |
40 |
Correct |
20 ms |
16876 KB |
Output is correct |
41 |
Correct |
19 ms |
16720 KB |
Output is correct |
42 |
Correct |
15 ms |
15980 KB |
Output is correct |
43 |
Correct |
16 ms |
15960 KB |
Output is correct |
44 |
Correct |
15 ms |
16104 KB |
Output is correct |
45 |
Correct |
15 ms |
15948 KB |
Output is correct |
46 |
Correct |
15 ms |
15948 KB |
Output is correct |
47 |
Correct |
18 ms |
16400 KB |
Output is correct |
48 |
Correct |
17 ms |
16440 KB |
Output is correct |
49 |
Correct |
24 ms |
17136 KB |
Output is correct |
50 |
Correct |
143 ms |
22540 KB |
Output is correct |
51 |
Correct |
20 ms |
16364 KB |
Output is correct |
52 |
Correct |
117 ms |
20624 KB |
Output is correct |
53 |
Correct |
24 ms |
17000 KB |
Output is correct |
54 |
Correct |
125 ms |
21448 KB |
Output is correct |
55 |
Correct |
18 ms |
16460 KB |
Output is correct |
56 |
Correct |
17 ms |
16332 KB |
Output is correct |
57 |
Correct |
18 ms |
16332 KB |
Output is correct |
58 |
Correct |
17 ms |
16448 KB |
Output is correct |
59 |
Correct |
19 ms |
16972 KB |
Output is correct |
60 |
Correct |
107 ms |
19924 KB |
Output is correct |
61 |
Correct |
18 ms |
16404 KB |
Output is correct |
62 |
Correct |
132 ms |
21680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15104 KB |
Output is correct |
3 |
Correct |
12 ms |
15148 KB |
Output is correct |
4 |
Correct |
10 ms |
15160 KB |
Output is correct |
5 |
Correct |
13 ms |
15200 KB |
Output is correct |
6 |
Correct |
10 ms |
15180 KB |
Output is correct |
7 |
Correct |
11 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15212 KB |
Output is correct |
9 |
Correct |
10 ms |
15180 KB |
Output is correct |
10 |
Correct |
10 ms |
15180 KB |
Output is correct |
11 |
Correct |
10 ms |
15204 KB |
Output is correct |
12 |
Correct |
10 ms |
15132 KB |
Output is correct |
13 |
Correct |
10 ms |
15180 KB |
Output is correct |
14 |
Correct |
10 ms |
15180 KB |
Output is correct |
15 |
Correct |
11 ms |
15208 KB |
Output is correct |
16 |
Correct |
10 ms |
15180 KB |
Output is correct |
17 |
Correct |
11 ms |
15192 KB |
Output is correct |
18 |
Correct |
11 ms |
15180 KB |
Output is correct |
19 |
Correct |
10 ms |
15180 KB |
Output is correct |
20 |
Correct |
10 ms |
15200 KB |
Output is correct |
21 |
Correct |
11 ms |
15180 KB |
Output is correct |
22 |
Correct |
10 ms |
15196 KB |
Output is correct |
23 |
Correct |
11 ms |
15180 KB |
Output is correct |
24 |
Correct |
11 ms |
15204 KB |
Output is correct |
25 |
Correct |
12 ms |
15208 KB |
Output is correct |
26 |
Correct |
10 ms |
15180 KB |
Output is correct |
27 |
Correct |
10 ms |
15180 KB |
Output is correct |
28 |
Correct |
10 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15152 KB |
Output is correct |
30 |
Correct |
10 ms |
15136 KB |
Output is correct |
31 |
Correct |
12 ms |
15228 KB |
Output is correct |
32 |
Correct |
10 ms |
15308 KB |
Output is correct |
33 |
Correct |
12 ms |
15180 KB |
Output is correct |
34 |
Correct |
13 ms |
15308 KB |
Output is correct |
35 |
Correct |
105 ms |
19500 KB |
Output is correct |
36 |
Correct |
137 ms |
22164 KB |
Output is correct |
37 |
Correct |
138 ms |
22320 KB |
Output is correct |
38 |
Correct |
137 ms |
21832 KB |
Output is correct |
39 |
Correct |
15 ms |
16060 KB |
Output is correct |
40 |
Correct |
20 ms |
16876 KB |
Output is correct |
41 |
Correct |
19 ms |
16720 KB |
Output is correct |
42 |
Correct |
15 ms |
15980 KB |
Output is correct |
43 |
Correct |
16 ms |
15960 KB |
Output is correct |
44 |
Correct |
15 ms |
16104 KB |
Output is correct |
45 |
Correct |
15 ms |
15948 KB |
Output is correct |
46 |
Correct |
15 ms |
15948 KB |
Output is correct |
47 |
Correct |
18 ms |
16400 KB |
Output is correct |
48 |
Correct |
17 ms |
16440 KB |
Output is correct |
49 |
Correct |
24 ms |
17136 KB |
Output is correct |
50 |
Correct |
143 ms |
22540 KB |
Output is correct |
51 |
Correct |
20 ms |
16364 KB |
Output is correct |
52 |
Correct |
117 ms |
20624 KB |
Output is correct |
53 |
Correct |
24 ms |
17000 KB |
Output is correct |
54 |
Correct |
125 ms |
21448 KB |
Output is correct |
55 |
Correct |
18 ms |
16460 KB |
Output is correct |
56 |
Correct |
17 ms |
16332 KB |
Output is correct |
57 |
Correct |
18 ms |
16332 KB |
Output is correct |
58 |
Correct |
17 ms |
16448 KB |
Output is correct |
59 |
Correct |
19 ms |
16972 KB |
Output is correct |
60 |
Correct |
107 ms |
19924 KB |
Output is correct |
61 |
Correct |
18 ms |
16404 KB |
Output is correct |
62 |
Correct |
132 ms |
21680 KB |
Output is correct |
63 |
Correct |
578 ms |
68328 KB |
Output is correct |
64 |
Correct |
568 ms |
68192 KB |
Output is correct |
65 |
Correct |
585 ms |
68348 KB |
Output is correct |
66 |
Correct |
457 ms |
57264 KB |
Output is correct |
67 |
Correct |
838 ms |
112988 KB |
Output is correct |
68 |
Correct |
440 ms |
57264 KB |
Output is correct |
69 |
Correct |
592 ms |
57120 KB |
Output is correct |
70 |
Correct |
501 ms |
57296 KB |
Output is correct |
71 |
Correct |
502 ms |
57292 KB |
Output is correct |
72 |
Correct |
833 ms |
80272 KB |
Output is correct |
73 |
Correct |
858 ms |
83476 KB |
Output is correct |
74 |
Correct |
1529 ms |
114772 KB |
Output is correct |
75 |
Correct |
799 ms |
64308 KB |
Output is correct |
76 |
Correct |
1079 ms |
86532 KB |
Output is correct |
77 |
Correct |
1100 ms |
86816 KB |
Output is correct |
78 |
Correct |
401 ms |
48516 KB |
Output is correct |
79 |
Correct |
633 ms |
62392 KB |
Output is correct |
80 |
Correct |
384 ms |
51832 KB |
Output is correct |
81 |
Correct |
587 ms |
61012 KB |
Output is correct |
82 |
Correct |
970 ms |
76988 KB |
Output is correct |
83 |
Correct |
976 ms |
77012 KB |
Output is correct |
84 |
Correct |
827 ms |
76972 KB |
Output is correct |
85 |
Correct |
868 ms |
77108 KB |
Output is correct |
86 |
Correct |
744 ms |
129124 KB |
Output is correct |
87 |
Correct |
790 ms |
131744 KB |
Output is correct |
88 |
Correct |
950 ms |
83140 KB |
Output is correct |
89 |
Correct |
1236 ms |
85768 KB |
Output is correct |