답안 #750607

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
750607 2023-05-29T22:37:05 Z stevancv 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
0 / 100
5 ms 9684 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int p[N], sz[N];
set<int> arr[N];
set<pair<int, int>> brr[N];
void Init(int n) {
    for (int i = 1; i <= n; i++) {
        p[i] = i;
        sz[i] = 1;
    }
}
int Find(int x) {
    if (x == p[x]) return p[x];
    return Find(p[x]);
}
ll C(ll x) {
    return x * (x - 1);
}
map<pair<int, int>, int> imaput, strelica;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    Init(n);
    ll ans = 0;
    while (m--) {
        int a, b;
        cin >> a >> b;
        imaput[{a, b}] = 1;
        if (Find(a) == Find(b)) {
            cout << ans << en;
            continue;
        }
        int x = Find(a);
        int y = Find(b);
        if (imaput[{b, a}] != 1) {
            if (strelica.find({y, a}) == strelica.end()) {
                ans += sz[y];
                arr[y].insert(a);
                brr[x].insert({a, y});
                strelica[{y, a}] = 1;
            }
            cout << ans << en;
            continue;
        }
        ans += C(sz[x] + sz[y]) - C(sz[x]) - C(sz[y]);
        if (arr[x].size() + brr[x].size() < arr[y].size() + brr[y].size()) {swap(x, y); swap(a, b);}
        int cnt = arr[x].size();
        int odz = 0;
        for (int u : arr[y]) {
            int o = Find(u);
            if (o == x) {
                ans -= sz[y];
            }
            else {
                ans += sz[x];
                arr[x].insert(u);
                brr[o].insert({u, x});
                if (strelica.find({x, u}) != strelica.end()) odz += 1;
                strelica[{x, u}] = 1;
            }
            brr[o].erase({u, y});
        }
        for (auto zz : brr[y]) {
            int uu = zz.first; int u = zz.second;
            if (u == x) {
                ans -= sz[x];
                cnt -= 1;
                arr[u].erase(uu);
            }
            else {
                brr[x].insert({uu, u});
            }
        }
        arr[y].clear(); brr[y].clear();
        ans += (ll)(cnt * sz[y]);
        sz[x] += sz[y];
        ans -= (ll)(odz * sz[x]);
        p[y] = x;
        cout << ans << en;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -