#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], pos[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) {
x = Find(x), y = Find(y);
if (x != y) {
parent[y] = x;
pos[y] |= pos[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]) {
pos[x] = true;
p.emplace_back(x);
q.emplace(x);
}
}
for (ll j = 0; j < p.size() - 1; j++) {
Union(p[j], p[j + 1]);
}
}
for (ll i = 1; i <= n; i++) {
if (pos[i]) {
cnt[Find(i)]++;
} else {
ans += gr[i].size();
}
}
for (ll i = 1; i <= n; i++) {
ans += cnt[i] * (cnt[i] - 1);
}
cout << ans;
return 0;
}
Compilation message
friends.cpp: In function 'int main()':
friends.cpp:71: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]
71 | for (ll j = 0; j < p.size() - 1; j++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
2648 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 |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2660 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 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
7900 KB |
Output is correct |
2 |
Incorrect |
40 ms |
9436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |