#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define N 100005
using namespace std;
typedef long long ll;
ll n, m, ans, bu[N], v[N], m1[N*2], m2[N*2], s[N];
vector<int> a[N];
queue<int> qu;
ll f(ll p) {
if (bu[p] == p) return p;
else return bu[p] = f(bu[p]);;
}
int main()
{
ll i, j, t, t1, t2, x1, x2;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> t1 >> t2;
m1[i] = t1;
m2[i] = t2;
a[t1].push_back(t2);
}
for (i = 1; i <= n; i++) bu[i] = i;
for (i = 1; i <= n; i++) {
if (a[i].size() >= 2) {
for (j = 1; j < a[i].size(); j++) {
x1 = f(a[i][0]);
x2 = f(a[i][j]);
if (x1 != x2) bu[x2] = x1;
}
}
}
for (i =1; i <= n; i++) {
if (bu[i] != i) {v[i] =1; qu.push(i);}
}
while (!qu.empty()) {
t = qu.front(); qu.pop();
for (i = 0; i < a[t].size(); i++) {
t2 = a[t][i];
x1 = f(t);
x2 = f(t2);
if (x1 != x2) bu[x2] = x1;
if (v[t2]) continue;
qu.push(t2); v[t2] = 1;
}
}
for (i = 1; i <= n; i++) f(i);
for (i = 1; i <= n; i++) {
s[bu[i]]++;
}
for (i = 1; i <= n; i++) {
if (s[i] > 1) ans += s[i] * (s[i] - 1);
}
for (i = 0; i < m; i++) {
if (bu[m1[i]] != bu[m2[i]]) ans++;
}
cout << ans;
return 0;
}
Compilation message
friends.cpp: In function 'int main()':
friends.cpp:31:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (j = 1; j < a[i].size(); j++) {
| ~~^~~~~~~~~~~~~
friends.cpp:43:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (i = 0; i < a[t].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
8652 KB |
Output is correct |
2 |
Incorrect |
85 ms |
13512 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |