#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i<= b; ++i)
#define FORD(i, a, b) for (int i = a; i>=b; --i)
#define REP(i, a) for (int i = 0; i<a; ++i)
#define DEBUG(x) { cerr << #x << " = " << x << endl; }
#define Arr(A, l, r) { cerr << #A << " = "; FOR(_, l, r) cerr << A[_] << ' '; cerr << endl; }
#define N 1001000
#define pp pair<int, int>
#define next __next
#define prev __prev
#define __builtin_popcount __builtin_popcountll
#define bit(S, i) (((S) >> i) & 1)
#define IO ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
using namespace std;
int n, m, f[N];
vector<int> e[N];
void Union(int a, int b) {
int t = f[a] + f[b];
if (f[a] > f[b]) swap(a, b);
f[a] = t;
f[b] = a;
}
int root(int u) { return f[u] < 0 ? u : (f[u] = root(f[u])); }
int main() {
IO;
cin >> n >> m;
REP(i, m) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
}
FOR(i, 1, n) f[i] = -1;
FOR(u, 1, n) {
int r = -1;
for (int v : e[u]) {
if (r == -1) r = v;
else {
int a = root(r);
int b = root(v);
if (a != b) Union(a, b);
}
}
}
long long ans = 0;
FOR(u, 1, n) {
for (int v : e[u]) if (root(u) != root(v)) ans++;
if (f[u] < 0) ans += 1ll * f[u] * (f[u] + 1);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23904 KB |
Output is correct |
3 |
Correct |
20 ms |
23904 KB |
Output is correct |
4 |
Incorrect |
20 ms |
23956 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
24092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
25028 KB |
Output is correct |
2 |
Incorrect |
100 ms |
27188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |