#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];
h[u].merge(h[v]);
}
cout << edge_count << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15180 KB |
Output is correct |
2 |
Correct |
9 ms |
15180 KB |
Output is correct |
3 |
Correct |
9 ms |
15180 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
9 ms |
15148 KB |
Output is correct |
6 |
Correct |
9 ms |
15180 KB |
Output is correct |
7 |
Correct |
10 ms |
15180 KB |
Output is correct |
8 |
Correct |
10 ms |
15180 KB |
Output is correct |
9 |
Correct |
10 ms |
15240 KB |
Output is correct |
10 |
Correct |
9 ms |
15180 KB |
Output is correct |
11 |
Correct |
11 ms |
15224 KB |
Output is correct |
12 |
Correct |
9 ms |
15180 KB |
Output is correct |
13 |
Correct |
10 ms |
15204 KB |
Output is correct |
14 |
Correct |
9 ms |
15204 KB |
Output is correct |
15 |
Correct |
9 ms |
15180 KB |
Output is correct |
16 |
Correct |
9 ms |
15204 KB |
Output is correct |
17 |
Correct |
10 ms |
15180 KB |
Output is correct |
18 |
Correct |
10 ms |
15180 KB |
Output is correct |
19 |
Correct |
9 ms |
15180 KB |
Output is correct |
20 |
Correct |
9 ms |
15180 KB |
Output is correct |
21 |
Correct |
10 ms |
15204 KB |
Output is correct |
22 |
Correct |
11 ms |
15180 KB |
Output is correct |
23 |
Correct |
10 ms |
15180 KB |
Output is correct |
24 |
Correct |
10 ms |
15180 KB |
Output is correct |
25 |
Correct |
11 ms |
15236 KB |
Output is correct |
26 |
Correct |
9 ms |
15200 KB |
Output is correct |
27 |
Correct |
9 ms |
15204 KB |
Output is correct |
28 |
Correct |
9 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15200 KB |
Output is correct |
30 |
Correct |
10 ms |
15224 KB |
Output is correct |
31 |
Correct |
10 ms |
15180 KB |
Output is correct |
32 |
Correct |
9 ms |
15196 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15180 KB |
Output is correct |
2 |
Correct |
9 ms |
15180 KB |
Output is correct |
3 |
Correct |
9 ms |
15180 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
9 ms |
15148 KB |
Output is correct |
6 |
Correct |
9 ms |
15180 KB |
Output is correct |
7 |
Correct |
10 ms |
15180 KB |
Output is correct |
8 |
Correct |
10 ms |
15180 KB |
Output is correct |
9 |
Correct |
10 ms |
15240 KB |
Output is correct |
10 |
Correct |
9 ms |
15180 KB |
Output is correct |
11 |
Correct |
11 ms |
15224 KB |
Output is correct |
12 |
Correct |
9 ms |
15180 KB |
Output is correct |
13 |
Correct |
10 ms |
15204 KB |
Output is correct |
14 |
Correct |
9 ms |
15204 KB |
Output is correct |
15 |
Correct |
9 ms |
15180 KB |
Output is correct |
16 |
Correct |
9 ms |
15204 KB |
Output is correct |
17 |
Correct |
10 ms |
15180 KB |
Output is correct |
18 |
Correct |
10 ms |
15180 KB |
Output is correct |
19 |
Correct |
9 ms |
15180 KB |
Output is correct |
20 |
Correct |
9 ms |
15180 KB |
Output is correct |
21 |
Correct |
10 ms |
15204 KB |
Output is correct |
22 |
Correct |
11 ms |
15180 KB |
Output is correct |
23 |
Correct |
10 ms |
15180 KB |
Output is correct |
24 |
Correct |
10 ms |
15180 KB |
Output is correct |
25 |
Correct |
11 ms |
15236 KB |
Output is correct |
26 |
Correct |
9 ms |
15200 KB |
Output is correct |
27 |
Correct |
9 ms |
15204 KB |
Output is correct |
28 |
Correct |
9 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15200 KB |
Output is correct |
30 |
Correct |
10 ms |
15224 KB |
Output is correct |
31 |
Correct |
10 ms |
15180 KB |
Output is correct |
32 |
Correct |
9 ms |
15196 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
13 ms |
15336 KB |
Output is correct |
35 |
Correct |
108 ms |
21168 KB |
Output is correct |
36 |
Correct |
149 ms |
24060 KB |
Output is correct |
37 |
Correct |
136 ms |
24132 KB |
Output is correct |
38 |
Correct |
135 ms |
23836 KB |
Output is correct |
39 |
Correct |
15 ms |
16076 KB |
Output is correct |
40 |
Correct |
18 ms |
16432 KB |
Output is correct |
41 |
Correct |
17 ms |
16460 KB |
Output is correct |
42 |
Correct |
13 ms |
15948 KB |
Output is correct |
43 |
Correct |
14 ms |
16044 KB |
Output is correct |
44 |
Correct |
13 ms |
15948 KB |
Output is correct |
45 |
Correct |
13 ms |
15948 KB |
Output is correct |
46 |
Correct |
14 ms |
15948 KB |
Output is correct |
47 |
Correct |
17 ms |
16304 KB |
Output is correct |
48 |
Correct |
15 ms |
16204 KB |
Output is correct |
49 |
Correct |
22 ms |
16992 KB |
Output is correct |
50 |
Correct |
138 ms |
24292 KB |
Output is correct |
51 |
Correct |
20 ms |
16492 KB |
Output is correct |
52 |
Correct |
125 ms |
22492 KB |
Output is correct |
53 |
Correct |
24 ms |
16816 KB |
Output is correct |
54 |
Correct |
128 ms |
23328 KB |
Output is correct |
55 |
Correct |
15 ms |
16332 KB |
Output is correct |
56 |
Correct |
16 ms |
16348 KB |
Output is correct |
57 |
Correct |
16 ms |
16332 KB |
Output is correct |
58 |
Correct |
16 ms |
16332 KB |
Output is correct |
59 |
Correct |
17 ms |
16488 KB |
Output is correct |
60 |
Correct |
109 ms |
21356 KB |
Output is correct |
61 |
Correct |
18 ms |
16332 KB |
Output is correct |
62 |
Correct |
168 ms |
23544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15180 KB |
Output is correct |
2 |
Correct |
9 ms |
15180 KB |
Output is correct |
3 |
Correct |
9 ms |
15180 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
9 ms |
15148 KB |
Output is correct |
6 |
Correct |
9 ms |
15180 KB |
Output is correct |
7 |
Correct |
10 ms |
15180 KB |
Output is correct |
8 |
Correct |
10 ms |
15180 KB |
Output is correct |
9 |
Correct |
10 ms |
15240 KB |
Output is correct |
10 |
Correct |
9 ms |
15180 KB |
Output is correct |
11 |
Correct |
11 ms |
15224 KB |
Output is correct |
12 |
Correct |
9 ms |
15180 KB |
Output is correct |
13 |
Correct |
10 ms |
15204 KB |
Output is correct |
14 |
Correct |
9 ms |
15204 KB |
Output is correct |
15 |
Correct |
9 ms |
15180 KB |
Output is correct |
16 |
Correct |
9 ms |
15204 KB |
Output is correct |
17 |
Correct |
10 ms |
15180 KB |
Output is correct |
18 |
Correct |
10 ms |
15180 KB |
Output is correct |
19 |
Correct |
9 ms |
15180 KB |
Output is correct |
20 |
Correct |
9 ms |
15180 KB |
Output is correct |
21 |
Correct |
10 ms |
15204 KB |
Output is correct |
22 |
Correct |
11 ms |
15180 KB |
Output is correct |
23 |
Correct |
10 ms |
15180 KB |
Output is correct |
24 |
Correct |
10 ms |
15180 KB |
Output is correct |
25 |
Correct |
11 ms |
15236 KB |
Output is correct |
26 |
Correct |
9 ms |
15200 KB |
Output is correct |
27 |
Correct |
9 ms |
15204 KB |
Output is correct |
28 |
Correct |
9 ms |
15180 KB |
Output is correct |
29 |
Correct |
10 ms |
15200 KB |
Output is correct |
30 |
Correct |
10 ms |
15224 KB |
Output is correct |
31 |
Correct |
10 ms |
15180 KB |
Output is correct |
32 |
Correct |
9 ms |
15196 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
13 ms |
15336 KB |
Output is correct |
35 |
Correct |
108 ms |
21168 KB |
Output is correct |
36 |
Correct |
149 ms |
24060 KB |
Output is correct |
37 |
Correct |
136 ms |
24132 KB |
Output is correct |
38 |
Correct |
135 ms |
23836 KB |
Output is correct |
39 |
Correct |
15 ms |
16076 KB |
Output is correct |
40 |
Correct |
18 ms |
16432 KB |
Output is correct |
41 |
Correct |
17 ms |
16460 KB |
Output is correct |
42 |
Correct |
13 ms |
15948 KB |
Output is correct |
43 |
Correct |
14 ms |
16044 KB |
Output is correct |
44 |
Correct |
13 ms |
15948 KB |
Output is correct |
45 |
Correct |
13 ms |
15948 KB |
Output is correct |
46 |
Correct |
14 ms |
15948 KB |
Output is correct |
47 |
Correct |
17 ms |
16304 KB |
Output is correct |
48 |
Correct |
15 ms |
16204 KB |
Output is correct |
49 |
Correct |
22 ms |
16992 KB |
Output is correct |
50 |
Correct |
138 ms |
24292 KB |
Output is correct |
51 |
Correct |
20 ms |
16492 KB |
Output is correct |
52 |
Correct |
125 ms |
22492 KB |
Output is correct |
53 |
Correct |
24 ms |
16816 KB |
Output is correct |
54 |
Correct |
128 ms |
23328 KB |
Output is correct |
55 |
Correct |
15 ms |
16332 KB |
Output is correct |
56 |
Correct |
16 ms |
16348 KB |
Output is correct |
57 |
Correct |
16 ms |
16332 KB |
Output is correct |
58 |
Correct |
16 ms |
16332 KB |
Output is correct |
59 |
Correct |
17 ms |
16488 KB |
Output is correct |
60 |
Correct |
109 ms |
21356 KB |
Output is correct |
61 |
Correct |
18 ms |
16332 KB |
Output is correct |
62 |
Correct |
168 ms |
23544 KB |
Output is correct |
63 |
Correct |
544 ms |
70116 KB |
Output is correct |
64 |
Correct |
565 ms |
70072 KB |
Output is correct |
65 |
Correct |
563 ms |
70068 KB |
Output is correct |
66 |
Correct |
427 ms |
58992 KB |
Output is correct |
67 |
Correct |
918 ms |
88608 KB |
Output is correct |
68 |
Correct |
415 ms |
58876 KB |
Output is correct |
69 |
Correct |
553 ms |
58772 KB |
Output is correct |
70 |
Correct |
510 ms |
58888 KB |
Output is correct |
71 |
Correct |
471 ms |
58996 KB |
Output is correct |
72 |
Correct |
935 ms |
71744 KB |
Output is correct |
73 |
Correct |
949 ms |
73516 KB |
Output is correct |
74 |
Correct |
1699 ms |
102580 KB |
Output is correct |
75 |
Correct |
802 ms |
65548 KB |
Output is correct |
76 |
Correct |
1108 ms |
80748 KB |
Output is correct |
77 |
Correct |
1227 ms |
81068 KB |
Output is correct |
78 |
Correct |
351 ms |
48516 KB |
Output is correct |
79 |
Correct |
698 ms |
62172 KB |
Output is correct |
80 |
Correct |
358 ms |
53036 KB |
Output is correct |
81 |
Correct |
660 ms |
61848 KB |
Output is correct |
82 |
Correct |
922 ms |
73524 KB |
Output is correct |
83 |
Correct |
947 ms |
73496 KB |
Output is correct |
84 |
Correct |
1004 ms |
73388 KB |
Output is correct |
85 |
Correct |
880 ms |
73444 KB |
Output is correct |
86 |
Correct |
775 ms |
94288 KB |
Output is correct |
87 |
Correct |
788 ms |
95384 KB |
Output is correct |
88 |
Correct |
916 ms |
72824 KB |
Output is correct |
89 |
Correct |
1120 ms |
78172 KB |
Output is correct |