답안 #346614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346614 2021-01-10T12:06:03 Z dolphingarlic 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
0 / 100
10 ms 14444 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int cmp[100001];
ll sz[100001], ans = 0;
set<int> followers[100001], graph[100001], rgraph[100001];
queue<pair<int, int>> to_merge;

void insert_weak_connection(int A, int B) {
    graph[A].insert(B);
    rgraph[B].insert(A);
    // If there's A new strong connection between A's and B's components, merge them
    if (graph[B].count(A)) to_merge.push({A, B});
}

int find(int A) { return (A == cmp[A] ? A : cmp[A] = find(cmp[A])); }

void onion(int A, int B) {
    if (A == B) return;
    // Merge the smaller component into the larger
    if (sz[A] < sz[B]) swap(A, B);

    // Add new contribution
    ans += sz[B] * followers[A].size() + sz[A] * followers[B].size();

    // DSU stuff
    cmp[B] = A;
    sz[A] += sz[B];

    // Merge followers of B into A
    for (int i : followers[B]) {
        if (followers[A].count(i)) ans -= sz[A];
        else followers[A].insert(i);
    }
    // Erase the connections
    graph[A].erase(B), rgraph[A].erase(B);
    graph[B].erase(A), rgraph[B].erase(A);
    // Merge the weak connections to other components
    for (int i : graph[B]) {
        rgraph[i].erase(B);
        insert_weak_connection(A, i);
    }
    for (int i : rgraph[B]) {
        graph[i].erase(B);
        insert_weak_connection(i, A);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cmp[i] = i;
        sz[i] = 1;
        followers[i].insert(i);
    }
    while (m--) {
        int A, B;
        cin >> A >> B;

        B = find(B);
        // If A isn't in B's component and doesn't already follow someone in B's component...
        if (find(A) == B || followers[B].count(A)) continue;
        // We insert A as A follower of B and add sz[find(B)] to the answer
        followers[B].insert(A);
        ans += sz[B];

        A = find(A);
        // Add connections between components
        insert_weak_connection(A, B);
        // We may have to merge multiple components for each new event
        while (to_merge.size()) {
            tie(A, B) = to_merge.front();
            to_merge.pop();
            onion(find(A), find(B));
        }
        cout << ans << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14444 KB Output isn't correct
2 Halted 0 ms 0 KB -