QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#106680#3556. Making Friends on Joitter is Funbashkort#0 0ms3576kbC++202.2kb2023-05-18 18:47:592024-05-31 13:40:12

Judging History

你现在查看的是最新测评结果

  • [2024-05-31 13:40:12]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:3576kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-18 18:47:59]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<set<pair<int, int>>> in(n), out(n);
    vector<int> fa(n), sz(n, 1);
    iota(fa.begin(), fa.end(), 0);

    auto leader = [&](int x) {
        while (fa[x] != x) {
            x = fa[x] = fa[fa[x]];
        }
        return x;
    };

    ll ans = 0;

    auto unite = [&](int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) {
            return;
        }

        if (in[x].size() + out[x].size() < in[y].size() + out[y].size()) {
            swap(x, y);
        }

        ans -= 1LL * sz[x] * (sz[x] - 1) + 1LL * sz[y] * (sz[y] - 1);
//        ans -= in[x].size() * 1LL * sz[x];
        sz[x] += sz[y];
        ans += 1LL * sz[x] * (sz[x] - 1);
        fa[y] = x;

        for (auto [p, v] : in[y]) {
            out[p].extract({y, v});
            ans -= sz[y];
            assert(p == leader(p));
            if (leader(p) != x) {
                out[p].insert({x, v});
                ans += sz[x];
                in[x].insert({p, v});
            }
        }

        for (auto [p, v] : out[y]) {
            in[p].extract({y, v});
            ans -= sz[p];
            assert(p == leader(p));
            if (leader(p) != x) {
                in[p].insert({x, v});
                ans += sz[p];
                out[x].insert({p, v});
            }
        }

        ans += in[x].size() * 1LL * sz[y];
    };

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

        if (la != lb) {
            auto it = in[la].lower_bound({lb, 0});
            if (it != in[la].end() && it->first == lb) {
                unite(la, lb);
//                cout << "unite: " << a << " " << b << endl;
            } else {
                if (!in[lb].count({la, a})) {
                    ans += sz[lb];
                    in[lb].insert({la, a});
                    out[la].insert({lb, a});
                }
            }
        }

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

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3576kb

input:

5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2
5 2
1 4
4 1
3 4
5 1
2 4
2 1
4 3
1 3
5 4
3 5
5 3

output:

1
2
3
4
6
7
8
12
14
17
17
17
17
17
17
17
17
17
17
17

result:

wrong answer 9th lines differ - expected: '16', found: '14'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%