#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> u(m);
vector<int> v(m);
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i];
--u[i]; --v[i];
g[u[i]].push_back(v[i]);
}
vector<int> par(n);
vector<int> sz(n, 1);
iota(par.begin(), par.end(), 0);
function<int(int)> GetPar = [&](int x) {
return par[x] == x ? x : par[x] = GetPar(par[x]);
};
auto Merge = [&](int x, int y) {
x = GetPar(x);
y = GetPar(y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
};
for (int foo = 0; foo < 20; foo++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int) g[i].size(); j++) {
Merge(g[i][0], g[i][j]);
}
if (sz[GetPar(i)] > 1) {
for (int j : g[i]) {
Merge(i, j);
}
}
}
}
long long ans = 0;
for (int i = 0; i < n; i++) {
if (GetPar(i) == i) {
ans += sz[i] * 1LL * (sz[i] - 1);
}
}
for (int i = 0; i < m; i++) {
if (GetPar(u[i]) != GetPar(v[i])) {
ans += 1;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
19 ms |
1056 KB |
Output is correct |
4 |
Correct |
104 ms |
3928 KB |
Output is correct |
5 |
Correct |
75 ms |
3164 KB |
Output is correct |
6 |
Correct |
144 ms |
5460 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
9 ms |
776 KB |
Output is correct |
9 |
Correct |
21 ms |
1492 KB |
Output is correct |
10 |
Correct |
37 ms |
1628 KB |
Output is correct |
11 |
Correct |
130 ms |
5476 KB |
Output is correct |
12 |
Correct |
12 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
604 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
23 ms |
1432 KB |
Output is correct |
17 |
Correct |
155 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
144 ms |
5456 KB |
Output is correct |
21 |
Correct |
5 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
600 KB |
Output is correct |
23 |
Correct |
9 ms |
928 KB |
Output is correct |
24 |
Correct |
6 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
5 ms |
600 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
4980 KB |
Output is correct |
2 |
Correct |
208 ms |
10072 KB |
Output is correct |
3 |
Correct |
196 ms |
10064 KB |
Output is correct |
4 |
Correct |
116 ms |
7404 KB |
Output is correct |
5 |
Correct |
71 ms |
5992 KB |
Output is correct |
6 |
Correct |
170 ms |
7684 KB |
Output is correct |
7 |
Correct |
188 ms |
10064 KB |
Output is correct |
8 |
Correct |
195 ms |
10244 KB |
Output is correct |
9 |
Correct |
113 ms |
7772 KB |
Output is correct |
10 |
Correct |
171 ms |
8836 KB |
Output is correct |
11 |
Correct |
220 ms |
10256 KB |
Output is correct |
12 |
Correct |
187 ms |
10584 KB |
Output is correct |
13 |
Correct |
62 ms |
8536 KB |
Output is correct |
14 |
Correct |
136 ms |
5720 KB |
Output is correct |
15 |
Correct |
73 ms |
5808 KB |
Output is correct |
16 |
Correct |
13 ms |
3676 KB |
Output is correct |
17 |
Correct |
10 ms |
3420 KB |
Output is correct |
18 |
Correct |
193 ms |
9840 KB |
Output is correct |
19 |
Correct |
198 ms |
9812 KB |
Output is correct |
20 |
Correct |
136 ms |
7144 KB |
Output is correct |
21 |
Correct |
68 ms |
5468 KB |
Output is correct |
22 |
Correct |
11 ms |
3416 KB |
Output is correct |
23 |
Correct |
70 ms |
2908 KB |
Output is correct |
24 |
Correct |
125 ms |
7096 KB |
Output is correct |
25 |
Correct |
56 ms |
8788 KB |
Output is correct |
26 |
Correct |
138 ms |
8640 KB |
Output is correct |
27 |
Correct |
212 ms |
10580 KB |
Output is correct |
28 |
Correct |
94 ms |
7764 KB |
Output is correct |
29 |
Correct |
98 ms |
7720 KB |
Output is correct |
30 |
Correct |
194 ms |
10180 KB |
Output is correct |
31 |
Correct |
111 ms |
7112 KB |
Output is correct |
32 |
Correct |
76 ms |
8616 KB |
Output is correct |
33 |
Correct |
56 ms |
8540 KB |
Output is correct |
34 |
Correct |
110 ms |
7596 KB |
Output is correct |
35 |
Correct |
63 ms |
8536 KB |
Output is correct |
36 |
Correct |
93 ms |
8460 KB |
Output is correct |
37 |
Correct |
106 ms |
7840 KB |
Output is correct |