#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline void fastio() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
ll n, m, ans;
ll parent[101010], cnt[101010];
bool vst[101010];
vector<ll> gr[101010];
queue<ll> q;
ll Find(ll x) {
if (x == parent[x]) {
return x;
} else {
return parent[x] = Find(parent[x]);
}
}
void Union(ll x, ll y) {
parent[Find(y)] = Find(x);
}
int main() {
fastio();
cin >> n >> m;
for (ll i = 0, a, b; i < m; i++) {
cin >> a >> b;
gr[a].emplace_back(b);
}
for (ll i = 1; i <= n; i++) {
parent[i] = i;
}
for (ll i = 1; i <= n; i++) {
if (vst[i] or gr[i].size() < 2) continue;
vector<ll> p;
q.emplace(i);
while (!q.empty()) {
ll loc = q.front();
q.pop();
if (vst[loc]) continue;
vst[loc] = true;
for (auto& x : gr[loc]) {
p.emplace_back(x);
q.emplace(x);
}
}
for (ll j = 0; j < p.size() - 1; j++) {
Union(p[j], p[j + 1]);
}
vst[i] = false;
}
for (ll i = 1; i <= n; i++) {
cnt[Find(i)]++;
}
for (ll i = 1; i <= n; i++) {
ans += cnt[i] * (cnt[i] - 1);
}
for (ll i = 1; i <= n; i++) {
for (auto& x : gr[i]) {
ans += Find(x) != Find(i);
}
}
cout << ans;
return 0;
}
Compilation message
friends.cpp: In function 'int main()':
friends.cpp:65:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (ll j = 0; j < p.size() - 1; j++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
5 ms |
3620 KB |
Output is correct |
4 |
Correct |
21 ms |
8936 KB |
Output is correct |
5 |
Correct |
15 ms |
6996 KB |
Output is correct |
6 |
Correct |
31 ms |
10388 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Correct |
5 ms |
3832 KB |
Output is correct |
10 |
Correct |
11 ms |
4824 KB |
Output is correct |
11 |
Correct |
27 ms |
10620 KB |
Output is correct |
12 |
Correct |
4 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3164 KB |
Output is correct |
16 |
Correct |
5 ms |
3912 KB |
Output is correct |
17 |
Correct |
30 ms |
10244 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
3164 KB |
Output is correct |
20 |
Correct |
29 ms |
10604 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
3108 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7900 KB |
Output is correct |
2 |
Correct |
42 ms |
9424 KB |
Output is correct |
3 |
Correct |
41 ms |
11720 KB |
Output is correct |
4 |
Correct |
25 ms |
7684 KB |
Output is correct |
5 |
Correct |
14 ms |
6624 KB |
Output is correct |
6 |
Correct |
37 ms |
11088 KB |
Output is correct |
7 |
Correct |
38 ms |
10404 KB |
Output is correct |
8 |
Correct |
41 ms |
10320 KB |
Output is correct |
9 |
Correct |
33 ms |
8020 KB |
Output is correct |
10 |
Correct |
33 ms |
9132 KB |
Output is correct |
11 |
Correct |
79 ms |
11792 KB |
Output is correct |
12 |
Correct |
71 ms |
11464 KB |
Output is correct |
13 |
Correct |
34 ms |
8528 KB |
Output is correct |
14 |
Correct |
31 ms |
10540 KB |
Output is correct |
15 |
Correct |
12 ms |
6236 KB |
Output is correct |
16 |
Correct |
4 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4188 KB |
Output is correct |
18 |
Correct |
44 ms |
11544 KB |
Output is correct |
19 |
Correct |
47 ms |
11504 KB |
Output is correct |
20 |
Correct |
26 ms |
7344 KB |
Output is correct |
21 |
Correct |
15 ms |
6200 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
15 ms |
6612 KB |
Output is correct |
24 |
Correct |
30 ms |
7652 KB |
Output is correct |
25 |
Correct |
20 ms |
8452 KB |
Output is correct |
26 |
Correct |
22 ms |
9092 KB |
Output is correct |
27 |
Correct |
42 ms |
11932 KB |
Output is correct |
28 |
Correct |
24 ms |
8168 KB |
Output is correct |
29 |
Correct |
24 ms |
8020 KB |
Output is correct |
30 |
Correct |
51 ms |
11768 KB |
Output is correct |
31 |
Correct |
24 ms |
7748 KB |
Output is correct |
32 |
Correct |
18 ms |
8540 KB |
Output is correct |
33 |
Correct |
18 ms |
8692 KB |
Output is correct |
34 |
Correct |
27 ms |
7764 KB |
Output is correct |
35 |
Correct |
17 ms |
8536 KB |
Output is correct |
36 |
Correct |
31 ms |
8912 KB |
Output is correct |
37 |
Correct |
28 ms |
8020 KB |
Output is correct |