#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define fi first
#define se second
#define int long long
typedef pair<int, int> ii;
int n, m, res, siz[N], par[N];
set<int> child[N], adj[N], r_adj[N];
queue<ii> q;
int find(int u){
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void addeg(int u, int v){
adj[u].insert(v);
r_adj[v].insert(u);
if (adj[v].count(u)) q.push({u, v});
}
int sum(int u){
return child[u].size() + adj[u].size() + r_adj[u].size();
}
void unin(int u, int v){
u = find(u);
v = find(v);
if (u == v) return;
res += child[u].size() * siz[v] + child[v].size() * siz[u];
if (sum(u) < sum(v)) swap(u, v);
siz[u] += siz[v];
par[v] = u;
for (auto x : child[v]){
if (child[u].find(x) != child[u].end()) res -= siz[u];
else child[u].insert(x);
}
adj[u].erase(v);
r_adj[u].erase(v);
adj[v].erase(u);
r_adj[v].erase(u);
for (auto x : adj[v]){
r_adj[x].erase(v);
addeg(u, x);
}
for (auto x : r_adj[v]){
adj[x].erase(v);
addeg(x, u);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++){
siz[i] = 1;
par[i] = i;
child[i].insert(i);
}
for (int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
v = find(v);
if (find(u) != v && child[v].find(u) == child[v].end()){
child[v].insert(u);
res += siz[v];
u = find(u);
addeg(u, v);
while (!q.empty()){
int u = q.front().fi;
int v = q.front().se;
unin(u, v);
q.pop();
}
}
cout << res << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
15008 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
15012 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
15000 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
2 ms |
15000 KB |
Output is correct |
16 |
Correct |
2 ms |
14936 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14940 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
2 ms |
14940 KB |
Output is correct |
26 |
Correct |
2 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
2 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
15008 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
15012 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
15000 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
2 ms |
15000 KB |
Output is correct |
16 |
Correct |
2 ms |
14936 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14940 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
2 ms |
14940 KB |
Output is correct |
26 |
Correct |
2 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
2 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
57 ms |
19240 KB |
Output is correct |
36 |
Correct |
78 ms |
22168 KB |
Output is correct |
37 |
Correct |
73 ms |
22352 KB |
Output is correct |
38 |
Correct |
70 ms |
21840 KB |
Output is correct |
39 |
Correct |
8 ms |
14940 KB |
Output is correct |
40 |
Correct |
8 ms |
15192 KB |
Output is correct |
41 |
Correct |
12 ms |
15192 KB |
Output is correct |
42 |
Correct |
8 ms |
14940 KB |
Output is correct |
43 |
Correct |
9 ms |
14856 KB |
Output is correct |
44 |
Correct |
9 ms |
14936 KB |
Output is correct |
45 |
Correct |
8 ms |
14744 KB |
Output is correct |
46 |
Correct |
8 ms |
14940 KB |
Output is correct |
47 |
Correct |
10 ms |
14940 KB |
Output is correct |
48 |
Correct |
10 ms |
14940 KB |
Output is correct |
49 |
Correct |
14 ms |
15708 KB |
Output is correct |
50 |
Correct |
72 ms |
22480 KB |
Output is correct |
51 |
Correct |
9 ms |
15060 KB |
Output is correct |
52 |
Correct |
69 ms |
20556 KB |
Output is correct |
53 |
Correct |
13 ms |
15708 KB |
Output is correct |
54 |
Correct |
71 ms |
21480 KB |
Output is correct |
55 |
Correct |
10 ms |
15196 KB |
Output is correct |
56 |
Correct |
9 ms |
15364 KB |
Output is correct |
57 |
Correct |
8 ms |
15312 KB |
Output is correct |
58 |
Correct |
9 ms |
15196 KB |
Output is correct |
59 |
Correct |
9 ms |
15212 KB |
Output is correct |
60 |
Correct |
52 ms |
19188 KB |
Output is correct |
61 |
Correct |
10 ms |
14936 KB |
Output is correct |
62 |
Correct |
72 ms |
21580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
15008 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
15012 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
15000 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
2 ms |
15000 KB |
Output is correct |
16 |
Correct |
2 ms |
14936 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14940 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
2 ms |
14940 KB |
Output is correct |
26 |
Correct |
2 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
2 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
57 ms |
19240 KB |
Output is correct |
36 |
Correct |
78 ms |
22168 KB |
Output is correct |
37 |
Correct |
73 ms |
22352 KB |
Output is correct |
38 |
Correct |
70 ms |
21840 KB |
Output is correct |
39 |
Correct |
8 ms |
14940 KB |
Output is correct |
40 |
Correct |
8 ms |
15192 KB |
Output is correct |
41 |
Correct |
12 ms |
15192 KB |
Output is correct |
42 |
Correct |
8 ms |
14940 KB |
Output is correct |
43 |
Correct |
9 ms |
14856 KB |
Output is correct |
44 |
Correct |
9 ms |
14936 KB |
Output is correct |
45 |
Correct |
8 ms |
14744 KB |
Output is correct |
46 |
Correct |
8 ms |
14940 KB |
Output is correct |
47 |
Correct |
10 ms |
14940 KB |
Output is correct |
48 |
Correct |
10 ms |
14940 KB |
Output is correct |
49 |
Correct |
14 ms |
15708 KB |
Output is correct |
50 |
Correct |
72 ms |
22480 KB |
Output is correct |
51 |
Correct |
9 ms |
15060 KB |
Output is correct |
52 |
Correct |
69 ms |
20556 KB |
Output is correct |
53 |
Correct |
13 ms |
15708 KB |
Output is correct |
54 |
Correct |
71 ms |
21480 KB |
Output is correct |
55 |
Correct |
10 ms |
15196 KB |
Output is correct |
56 |
Correct |
9 ms |
15364 KB |
Output is correct |
57 |
Correct |
8 ms |
15312 KB |
Output is correct |
58 |
Correct |
9 ms |
15196 KB |
Output is correct |
59 |
Correct |
9 ms |
15212 KB |
Output is correct |
60 |
Correct |
52 ms |
19188 KB |
Output is correct |
61 |
Correct |
10 ms |
14936 KB |
Output is correct |
62 |
Correct |
72 ms |
21580 KB |
Output is correct |
63 |
Correct |
363 ms |
66968 KB |
Output is correct |
64 |
Correct |
376 ms |
67060 KB |
Output is correct |
65 |
Correct |
349 ms |
66900 KB |
Output is correct |
66 |
Correct |
148 ms |
33360 KB |
Output is correct |
67 |
Correct |
271 ms |
60340 KB |
Output is correct |
68 |
Correct |
143 ms |
33616 KB |
Output is correct |
69 |
Correct |
311 ms |
34740 KB |
Output is correct |
70 |
Correct |
191 ms |
33416 KB |
Output is correct |
71 |
Correct |
160 ms |
33484 KB |
Output is correct |
72 |
Correct |
297 ms |
44496 KB |
Output is correct |
73 |
Correct |
305 ms |
46128 KB |
Output is correct |
74 |
Correct |
707 ms |
68176 KB |
Output is correct |
75 |
Correct |
439 ms |
40832 KB |
Output is correct |
76 |
Correct |
504 ms |
53092 KB |
Output is correct |
77 |
Correct |
467 ms |
53276 KB |
Output is correct |
78 |
Correct |
153 ms |
37456 KB |
Output is correct |
79 |
Correct |
264 ms |
41044 KB |
Output is correct |
80 |
Correct |
169 ms |
36436 KB |
Output is correct |
81 |
Correct |
233 ms |
39252 KB |
Output is correct |
82 |
Correct |
569 ms |
54084 KB |
Output is correct |
83 |
Correct |
536 ms |
54188 KB |
Output is correct |
84 |
Correct |
421 ms |
52720 KB |
Output is correct |
85 |
Correct |
464 ms |
52820 KB |
Output is correct |
86 |
Correct |
225 ms |
66148 KB |
Output is correct |
87 |
Correct |
275 ms |
68092 KB |
Output is correct |
88 |
Correct |
283 ms |
45396 KB |
Output is correct |
89 |
Correct |
474 ms |
51024 KB |
Output is correct |