Submission #770934

# Submission time Handle Problem Language Result Execution time Memory
770934 2023-07-02T07:12:23 Z PurpleCrayon Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
6 ms 9824 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];
vector<ar<int, 2>> use[N];

ll ans = 0;
ll f(int x) {
    assert(x == par[x]);
    return (long long) sz(st[x]) * sz(in[x]);
};

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


void 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);

    vector<ar<int, 2>> change;

    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);

        for (auto e : use[x]) {
            change.push_back({e[0], e[1]});
        }
        use[x].clear();
    }

    st[b].clear();

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

    vector<ar<int, 2>> to_merge;
    for (auto e : change) {
        ar<int, 2> ne{par[e[0]], par[e[1]]};
        ed.insert(ne);
        if (par[ne[0]] != par[ne[1]] && ed.count({ne[1], ne[0]})) {
            to_merge.push_back(ne);
        }

        use[ne[1]].push_back(ne);
    }

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

    for (auto e : to_merge) {
        if (par[e[0]] != par[e[1]]) {
            merge(e[0], e[1]);
        }
    }
}

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

    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({par[a], par[b]});
        use[par[a]].push_back({par[a], par[b]});
        use[par[b]].push_back({par[a], par[b]});
        if (ed.count({par[b], par[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();
}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 9620 KB Output is correct
2 Correct 5 ms 9716 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9720 KB Output is correct
6 Correct 5 ms 9720 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Incorrect 6 ms 9824 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9620 KB Output is correct
2 Correct 5 ms 9716 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9720 KB Output is correct
6 Correct 5 ms 9720 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Incorrect 6 ms 9824 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9620 KB Output is correct
2 Correct 5 ms 9716 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9720 KB Output is correct
6 Correct 5 ms 9720 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Incorrect 6 ms 9824 KB Output isn't correct
9 Halted 0 ms 0 KB -