QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#106683#3556. Making Friends on Joitter is Funbashkort#0 1ms3584kbC++202.5kb2023-05-18 19:08:062024-05-31 13:40:14

Judging History

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

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

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 = [&](auto self, int x, int y) -> void {
        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);
        }

        fa[y] = x;

        for (auto [p, v] : in[y]) {
            out[p].extract({y, v});
            ans -= sz[y];
            assert(p == leader(p));
            if (p != x && !in[x].count({p, v})) {
                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 (p != x && !out[x].count({p, v})) {
                in[p].insert({x, v});
                ans += sz[p];
                out[x].insert({p, v});
            }
        }

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

        for (auto [p, v] : in[y]) {
            auto it = out[x].lower_bound({p, -1});
            if (p != x && it != out[x].end() && it->first == p) {
                self(self, p, y);
            }
        }

//        for (auto [p, xt] : in[x]) {
//            assert(leader(p) == p);
//            assert(p != x);
//        }
    };

    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(unite, la, lb);
            } 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;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 1ms
memory: 3584kb

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
16
20
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #2:

score: 1
Accepted
time: 1ms
memory: 3580kb

input:

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

output:

1
2
3
4
6
8
13
13
16
16
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #3:

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

input:

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

output:

1
2
3
4
5
6
7
8
10
15
20
20
20
20
20
20
20
20
20
20

result:

wrong answer 9th lines differ - expected: '11', found: '10'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%