답안 #770911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770911 2023-07-02T06:21:06 Z PurpleCrayon 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
4 ms 7252 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;

int n, m, par[N];
vector<int> st[N];
set<ar<int, 2>> ed;
set<int> in[N];

void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        st[i].push_back(i);
        par[i] = i;
    }

    ll ans = 0;

    auto f = [&](int x) {
        assert(x == par[x]);
        return (long long) sz(st[x]) * sz(in[x]);
    };

    auto g = [&](int x) {
        assert(x == par[x]);
        return (long long) sz(st[x]) * (sz(st[x]) - 1);
    };

    auto merge = [&](int a, int b) {
        a = par[a], b = par[b];
        if (a == b) return;
        if (sz(st[a]) < sz(st[b])) swap(a, b);

        ans -= g(a), ans -= g(b);
        ans -= f(a), ans -= f(b);
        for (int x : st[b]) {
            par[x] = a;
            st[a].push_back(x);
            in[a].erase(x);
        }

        st[b].clear();

        for (int x : in[b]) {
            if (par[x] != a) {
                in[a].insert(x);
            }
        }
        in[b].clear();

        ans += f(a);
        ans += g(a);
    };

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b, --a, --b;

        ans -= f(par[b]);
        if (par[a] != par[b]) in[par[b]].insert(a);
        ans += f(par[b]);

        ed.insert({a, b});
        if (ed.count({b, a})) {
            merge(a, b);
        }

        cout << ans << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -