답안 #994322

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994322 2024-06-07T11:13:05 Z gmroh06 우호 조약 체결 (JOI14_friends) C++14
0 / 100
60 ms 16352 KB
#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], rgr[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);
        rgr[b].emplace_back(a);
    }

    for (ll i = 1; i <= n; i++) {
        parent[i] = i;
    }

    for (ll i = 1; i <= n; i++) {
        if (vst[i]) continue;

        q.emplace(i);

        while (!q.empty()) {
            ll loc = q.front();
            q.pop();

            if (vst[loc]) continue;

            vst[loc] = true;
            pos[Find(loc)] |= gr[loc].size() >= 2;

            for (auto& x : gr[loc]) {
                Union(loc, x);
                if (!vst[x]) q.emplace(x);
            }
        }
    }

    for (ll i = 1; i <= n; i++) {
        if (pos[Find(i)] and !rgr[i].empty()) {
            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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Incorrect 1 ms 6748 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 7004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 13456 KB Output is correct
2 Incorrect 60 ms 16352 KB Output isn't correct
3 Halted 0 ms 0 KB -