#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;
q.emplace(i);
ll tmp = gr[i].front();
while (!q.empty()) {
ll loc = q.front();
q.pop();
for (auto& x : gr[loc]) {
if (!vst[x]) {
vst[x] = true;
Union(tmp, x);
q.emplace(x);
}
}
}
}
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;
}
# |
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 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
39 ms |
8020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |