#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, m;
vector<int> g[N];
int par[N];
int sz[N];
bool visit[N];
int find(int v) {
if (par[v] != v) {
par[v] = find(par[v]);
}
return par[v];
}
void merge(int v, int u) {
v = find(v);
u = find(u);
if (v == u) {
return;
} else {
par[u] = v;
sz[v] += sz[u];
}
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int v, u;
cin >> v >> u;
g[v].emplace_back(u);
}
iota(par + 1, par + n + 1, 1);
fill(sz + 1, sz + n + 1, 1);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j + 1 < (int) g[i].size(); ++j) {
merge(g[i][j], g[i][j + 1]);
}
}
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (sz[find(i)] > 1) {
visit[i] = true;
q.emplace(i);
}
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto u : g[v]) {
if (!visit[u]) {
visit[u] = true;
q.emplace(u);
}
merge(v, u);
}
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
if (par[i] == i) {
ans += (long long) sz[i] * (sz[i] - 1);
}
}
for (int i = 1; i <= n; ++i) {
for (int j : g[i]) {
if (find(i) != find(j)) {
++ans;
}
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
5 ms |
2728 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
11 ms |
3004 KB |
Output is correct |
4 |
Correct |
44 ms |
3804 KB |
Output is correct |
5 |
Correct |
36 ms |
3576 KB |
Output is correct |
6 |
Correct |
59 ms |
4216 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
7 ms |
2808 KB |
Output is correct |
9 |
Correct |
12 ms |
2936 KB |
Output is correct |
10 |
Correct |
19 ms |
3164 KB |
Output is correct |
11 |
Correct |
59 ms |
4216 KB |
Output is correct |
12 |
Correct |
9 ms |
2936 KB |
Output is correct |
13 |
Correct |
6 ms |
2936 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
8 ms |
2936 KB |
Output is correct |
16 |
Correct |
14 ms |
3064 KB |
Output is correct |
17 |
Correct |
59 ms |
4088 KB |
Output is correct |
18 |
Correct |
6 ms |
2940 KB |
Output is correct |
19 |
Correct |
6 ms |
2936 KB |
Output is correct |
20 |
Correct |
60 ms |
4216 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
6 ms |
2884 KB |
Output is correct |
23 |
Correct |
7 ms |
2936 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2936 KB |
Output is correct |
28 |
Correct |
6 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
3892 KB |
Output is correct |
2 |
Correct |
112 ms |
6812 KB |
Output is correct |
3 |
Correct |
112 ms |
6716 KB |
Output is correct |
4 |
Correct |
56 ms |
5752 KB |
Output is correct |
5 |
Correct |
28 ms |
4992 KB |
Output is correct |
6 |
Correct |
84 ms |
5308 KB |
Output is correct |
7 |
Correct |
82 ms |
6648 KB |
Output is correct |
8 |
Correct |
80 ms |
6816 KB |
Output is correct |
9 |
Correct |
51 ms |
5904 KB |
Output is correct |
10 |
Correct |
70 ms |
6264 KB |
Output is correct |
11 |
Correct |
111 ms |
6776 KB |
Output is correct |
12 |
Correct |
108 ms |
7032 KB |
Output is correct |
13 |
Correct |
43 ms |
6620 KB |
Output is correct |
14 |
Correct |
64 ms |
4344 KB |
Output is correct |
15 |
Correct |
26 ms |
4856 KB |
Output is correct |
16 |
Correct |
6 ms |
3704 KB |
Output is correct |
17 |
Correct |
5 ms |
3448 KB |
Output is correct |
18 |
Correct |
112 ms |
6368 KB |
Output is correct |
19 |
Correct |
100 ms |
6348 KB |
Output is correct |
20 |
Correct |
56 ms |
5436 KB |
Output is correct |
21 |
Correct |
27 ms |
4728 KB |
Output is correct |
22 |
Correct |
6 ms |
3576 KB |
Output is correct |
23 |
Correct |
39 ms |
3468 KB |
Output is correct |
24 |
Correct |
68 ms |
5428 KB |
Output is correct |
25 |
Correct |
40 ms |
6620 KB |
Output is correct |
26 |
Correct |
50 ms |
6776 KB |
Output is correct |
27 |
Correct |
109 ms |
7164 KB |
Output is correct |
28 |
Correct |
50 ms |
6080 KB |
Output is correct |
29 |
Correct |
49 ms |
6048 KB |
Output is correct |
30 |
Correct |
110 ms |
6776 KB |
Output is correct |
31 |
Correct |
53 ms |
5468 KB |
Output is correct |
32 |
Correct |
42 ms |
6636 KB |
Output is correct |
33 |
Correct |
41 ms |
6648 KB |
Output is correct |
34 |
Correct |
52 ms |
5880 KB |
Output is correct |
35 |
Correct |
40 ms |
6648 KB |
Output is correct |
36 |
Correct |
48 ms |
6608 KB |
Output is correct |
37 |
Correct |
49 ms |
6120 KB |
Output is correct |