# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
994386 | gmroh06 | 우호 조약 체결 (JOI14_friends) | C++14 | 79 ms | 11932 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |