#include <bits/stdc++.h>
using i64 = long long;
constexpr int maxN = 1E5 + 5;
std::set<int> in[maxN], rev[maxN], graph[maxN];
std::queue<std::pair<int, int>> strong;
i64 ans = 0;
int par[maxN], sz[maxN];
int find(int a) {
if(par[a] == a) {
return a;
}
return par[a] = find(par[a]);
}
void connect(int a, int b) {
graph[a].emplace(b);
rev[b].emplace(a);
if(graph[b].count(a)) {
strong.emplace(a, b);
}
}
void unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) {
return;
}
if(sz[a] < sz[b]) {
std::swap(a, b);
}
ans += 1LL * in[b].size() * sz[a] + 1LL * in[a].size() * sz[b];
sz[a] += sz[b];
par[b] = a;
for(int v : in[b]) {
if(!in[a].count(v)) {
in[a].emplace(v);
} else {
ans -= sz[a];
}
}
graph[a].erase(b);
graph[b].erase(a);
rev[a].erase(b);
rev[b].erase(a);
for(int v : graph[b]) {
rev[v].erase(b);
connect(a, v);
}
for(int v : rev[b]) {
graph[v].erase(b);
connect(v, a);
}
}
void solve() {
int n, m;
std::cin >> n >> m;
for(int _ = 1; _ <= m; _++) {
int a, b;
std::cin >> a >> b;
if(find(a) != find(b) && !in[find(b)].count(a)) {
in[find(b)].emplace(a);
ans += sz[find(b)];
connect(find(a), find(b));
while(!strong.empty()) {
auto [u, v] = strong.front();
strong.pop();
unite(u, v);
}
}
std::cout << ans << "\n";
}
return;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::iota(par, par + maxN, 0);
std::fill(sz, sz + maxN, 1);
for(int i = 0; i < maxN; i++) {
in[i].emplace(i);
}
int t = 1; //std::cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19800 KB |
Output is correct |
2 |
Correct |
10 ms |
20060 KB |
Output is correct |
3 |
Correct |
9 ms |
19800 KB |
Output is correct |
4 |
Correct |
9 ms |
19804 KB |
Output is correct |
5 |
Correct |
10 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
9 ms |
20060 KB |
Output is correct |
8 |
Correct |
9 ms |
20060 KB |
Output is correct |
9 |
Correct |
9 ms |
20056 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
8 ms |
19804 KB |
Output is correct |
12 |
Correct |
10 ms |
19804 KB |
Output is correct |
13 |
Correct |
9 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19800 KB |
Output is correct |
15 |
Correct |
11 ms |
19800 KB |
Output is correct |
16 |
Correct |
9 ms |
19804 KB |
Output is correct |
17 |
Correct |
8 ms |
19800 KB |
Output is correct |
18 |
Correct |
8 ms |
19808 KB |
Output is correct |
19 |
Correct |
8 ms |
19864 KB |
Output is correct |
20 |
Correct |
8 ms |
19800 KB |
Output is correct |
21 |
Correct |
9 ms |
20064 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
8 ms |
20064 KB |
Output is correct |
24 |
Correct |
8 ms |
19804 KB |
Output is correct |
25 |
Correct |
9 ms |
20056 KB |
Output is correct |
26 |
Correct |
8 ms |
19804 KB |
Output is correct |
27 |
Correct |
9 ms |
20032 KB |
Output is correct |
28 |
Correct |
9 ms |
19812 KB |
Output is correct |
29 |
Correct |
10 ms |
20060 KB |
Output is correct |
30 |
Correct |
8 ms |
19804 KB |
Output is correct |
31 |
Correct |
8 ms |
19932 KB |
Output is correct |
32 |
Correct |
8 ms |
19800 KB |
Output is correct |
33 |
Correct |
9 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19800 KB |
Output is correct |
2 |
Correct |
10 ms |
20060 KB |
Output is correct |
3 |
Correct |
9 ms |
19800 KB |
Output is correct |
4 |
Correct |
9 ms |
19804 KB |
Output is correct |
5 |
Correct |
10 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
9 ms |
20060 KB |
Output is correct |
8 |
Correct |
9 ms |
20060 KB |
Output is correct |
9 |
Correct |
9 ms |
20056 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
8 ms |
19804 KB |
Output is correct |
12 |
Correct |
10 ms |
19804 KB |
Output is correct |
13 |
Correct |
9 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19800 KB |
Output is correct |
15 |
Correct |
11 ms |
19800 KB |
Output is correct |
16 |
Correct |
9 ms |
19804 KB |
Output is correct |
17 |
Correct |
8 ms |
19800 KB |
Output is correct |
18 |
Correct |
8 ms |
19808 KB |
Output is correct |
19 |
Correct |
8 ms |
19864 KB |
Output is correct |
20 |
Correct |
8 ms |
19800 KB |
Output is correct |
21 |
Correct |
9 ms |
20064 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
8 ms |
20064 KB |
Output is correct |
24 |
Correct |
8 ms |
19804 KB |
Output is correct |
25 |
Correct |
9 ms |
20056 KB |
Output is correct |
26 |
Correct |
8 ms |
19804 KB |
Output is correct |
27 |
Correct |
9 ms |
20032 KB |
Output is correct |
28 |
Correct |
9 ms |
19812 KB |
Output is correct |
29 |
Correct |
10 ms |
20060 KB |
Output is correct |
30 |
Correct |
8 ms |
19804 KB |
Output is correct |
31 |
Correct |
8 ms |
19932 KB |
Output is correct |
32 |
Correct |
8 ms |
19800 KB |
Output is correct |
33 |
Correct |
9 ms |
20060 KB |
Output is correct |
34 |
Correct |
10 ms |
20060 KB |
Output is correct |
35 |
Correct |
63 ms |
25668 KB |
Output is correct |
36 |
Correct |
83 ms |
28336 KB |
Output is correct |
37 |
Correct |
82 ms |
28500 KB |
Output is correct |
38 |
Correct |
85 ms |
28252 KB |
Output is correct |
39 |
Correct |
10 ms |
20060 KB |
Output is correct |
40 |
Correct |
11 ms |
20556 KB |
Output is correct |
41 |
Correct |
13 ms |
20572 KB |
Output is correct |
42 |
Correct |
10 ms |
20060 KB |
Output is correct |
43 |
Correct |
10 ms |
20060 KB |
Output is correct |
44 |
Correct |
11 ms |
20056 KB |
Output is correct |
45 |
Correct |
10 ms |
20060 KB |
Output is correct |
46 |
Correct |
11 ms |
20060 KB |
Output is correct |
47 |
Correct |
11 ms |
20316 KB |
Output is correct |
48 |
Correct |
11 ms |
20364 KB |
Output is correct |
49 |
Correct |
16 ms |
21084 KB |
Output is correct |
50 |
Correct |
85 ms |
28680 KB |
Output is correct |
51 |
Correct |
13 ms |
20568 KB |
Output is correct |
52 |
Correct |
83 ms |
26856 KB |
Output is correct |
53 |
Correct |
16 ms |
21084 KB |
Output is correct |
54 |
Correct |
77 ms |
27660 KB |
Output is correct |
55 |
Correct |
12 ms |
20572 KB |
Output is correct |
56 |
Correct |
12 ms |
20552 KB |
Output is correct |
57 |
Correct |
12 ms |
20572 KB |
Output is correct |
58 |
Correct |
12 ms |
20572 KB |
Output is correct |
59 |
Correct |
11 ms |
20572 KB |
Output is correct |
60 |
Correct |
62 ms |
25528 KB |
Output is correct |
61 |
Correct |
12 ms |
20316 KB |
Output is correct |
62 |
Correct |
78 ms |
27888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19800 KB |
Output is correct |
2 |
Correct |
10 ms |
20060 KB |
Output is correct |
3 |
Correct |
9 ms |
19800 KB |
Output is correct |
4 |
Correct |
9 ms |
19804 KB |
Output is correct |
5 |
Correct |
10 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
9 ms |
20060 KB |
Output is correct |
8 |
Correct |
9 ms |
20060 KB |
Output is correct |
9 |
Correct |
9 ms |
20056 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
8 ms |
19804 KB |
Output is correct |
12 |
Correct |
10 ms |
19804 KB |
Output is correct |
13 |
Correct |
9 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19800 KB |
Output is correct |
15 |
Correct |
11 ms |
19800 KB |
Output is correct |
16 |
Correct |
9 ms |
19804 KB |
Output is correct |
17 |
Correct |
8 ms |
19800 KB |
Output is correct |
18 |
Correct |
8 ms |
19808 KB |
Output is correct |
19 |
Correct |
8 ms |
19864 KB |
Output is correct |
20 |
Correct |
8 ms |
19800 KB |
Output is correct |
21 |
Correct |
9 ms |
20064 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
8 ms |
20064 KB |
Output is correct |
24 |
Correct |
8 ms |
19804 KB |
Output is correct |
25 |
Correct |
9 ms |
20056 KB |
Output is correct |
26 |
Correct |
8 ms |
19804 KB |
Output is correct |
27 |
Correct |
9 ms |
20032 KB |
Output is correct |
28 |
Correct |
9 ms |
19812 KB |
Output is correct |
29 |
Correct |
10 ms |
20060 KB |
Output is correct |
30 |
Correct |
8 ms |
19804 KB |
Output is correct |
31 |
Correct |
8 ms |
19932 KB |
Output is correct |
32 |
Correct |
8 ms |
19800 KB |
Output is correct |
33 |
Correct |
9 ms |
20060 KB |
Output is correct |
34 |
Correct |
10 ms |
20060 KB |
Output is correct |
35 |
Correct |
63 ms |
25668 KB |
Output is correct |
36 |
Correct |
83 ms |
28336 KB |
Output is correct |
37 |
Correct |
82 ms |
28500 KB |
Output is correct |
38 |
Correct |
85 ms |
28252 KB |
Output is correct |
39 |
Correct |
10 ms |
20060 KB |
Output is correct |
40 |
Correct |
11 ms |
20556 KB |
Output is correct |
41 |
Correct |
13 ms |
20572 KB |
Output is correct |
42 |
Correct |
10 ms |
20060 KB |
Output is correct |
43 |
Correct |
10 ms |
20060 KB |
Output is correct |
44 |
Correct |
11 ms |
20056 KB |
Output is correct |
45 |
Correct |
10 ms |
20060 KB |
Output is correct |
46 |
Correct |
11 ms |
20060 KB |
Output is correct |
47 |
Correct |
11 ms |
20316 KB |
Output is correct |
48 |
Correct |
11 ms |
20364 KB |
Output is correct |
49 |
Correct |
16 ms |
21084 KB |
Output is correct |
50 |
Correct |
85 ms |
28680 KB |
Output is correct |
51 |
Correct |
13 ms |
20568 KB |
Output is correct |
52 |
Correct |
83 ms |
26856 KB |
Output is correct |
53 |
Correct |
16 ms |
21084 KB |
Output is correct |
54 |
Correct |
77 ms |
27660 KB |
Output is correct |
55 |
Correct |
12 ms |
20572 KB |
Output is correct |
56 |
Correct |
12 ms |
20552 KB |
Output is correct |
57 |
Correct |
12 ms |
20572 KB |
Output is correct |
58 |
Correct |
12 ms |
20572 KB |
Output is correct |
59 |
Correct |
11 ms |
20572 KB |
Output is correct |
60 |
Correct |
62 ms |
25528 KB |
Output is correct |
61 |
Correct |
12 ms |
20316 KB |
Output is correct |
62 |
Correct |
78 ms |
27888 KB |
Output is correct |
63 |
Correct |
457 ms |
68028 KB |
Output is correct |
64 |
Correct |
470 ms |
67668 KB |
Output is correct |
65 |
Correct |
476 ms |
67540 KB |
Output is correct |
66 |
Correct |
169 ms |
33804 KB |
Output is correct |
67 |
Correct |
381 ms |
61788 KB |
Output is correct |
68 |
Correct |
173 ms |
33664 KB |
Output is correct |
69 |
Correct |
323 ms |
35200 KB |
Output is correct |
70 |
Correct |
179 ms |
33624 KB |
Output is correct |
71 |
Correct |
186 ms |
33688 KB |
Output is correct |
72 |
Correct |
379 ms |
46152 KB |
Output is correct |
73 |
Correct |
378 ms |
47700 KB |
Output is correct |
74 |
Correct |
901 ms |
70736 KB |
Output is correct |
75 |
Correct |
451 ms |
41552 KB |
Output is correct |
76 |
Correct |
646 ms |
55152 KB |
Output is correct |
77 |
Correct |
661 ms |
55428 KB |
Output is correct |
78 |
Correct |
184 ms |
37272 KB |
Output is correct |
79 |
Correct |
332 ms |
41640 KB |
Output is correct |
80 |
Correct |
180 ms |
36168 KB |
Output is correct |
81 |
Correct |
271 ms |
40044 KB |
Output is correct |
82 |
Correct |
709 ms |
54332 KB |
Output is correct |
83 |
Correct |
700 ms |
54356 KB |
Output is correct |
84 |
Correct |
526 ms |
53108 KB |
Output is correct |
85 |
Correct |
550 ms |
53332 KB |
Output is correct |
86 |
Correct |
281 ms |
66448 KB |
Output is correct |
87 |
Correct |
292 ms |
68688 KB |
Output is correct |
88 |
Correct |
412 ms |
47104 KB |
Output is correct |
89 |
Correct |
632 ms |
53056 KB |
Output is correct |