QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#50658#1265. Total Eclipseckiseki#AC ✓1107ms22940kbC++1.8kb2022-09-28 14:19:582022-09-28 14:19:59

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-28 14:19:59]
  • 评测
  • 测评结果:AC
  • 用时:1107ms
  • 内存:22940kb
  • [2022-09-28 14:19:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

struct Dsu {
    vector<int> pa, rk;
    int CC;
    Dsu(int n) : pa(n), rk(n), CC(0) {
        iota(pa.begin(), pa.end(), 0);
    }
    int anc(int x) {
        return x == pa[x] ? x : pa[x] = anc(pa[x]);
    }
    void join(int x, int y) {
        x = anc(x);
        y = anc(y);
        if (x == y)
            return;
        --CC;
        if (rk[x] < rk[y]) swap(x, y);
        pa[y] = x;
        if (rk[x] == rk[y]) ++rk[x];
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t; cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        Dsu dsu(n);
        map<int,vector<int>,greater<>> mp;
        for (int i = 0; i < n; i++) {
            int b;
            cin >> b;
            mp[b].emplace_back(i);
        }
        vector<vector<int>> g(n);
        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }
        vector<int> mark(n);
        int lastb = 1e9;
        int64_t ans = 0;
        for (const auto &[b, v]: mp) {
            if (lastb != -1) {
                // cerr << "--- ";
                // cerr << lastb << ' ' << b << ' ' << dsu.CC << endl;
                ans += 1LL * (lastb - b) * dsu.CC;
            }
            lastb = b;
            for (int i: v) {
                dsu.CC += 1;
                mark[i] = true;
            }
            for (int i: v)
                for (int j: g[i])
                    if (mark[j])
                        dsu.join(i, j);
        }
        if (lastb != -1) {
            int b = 0;
            // cerr << "--- ";
            // cerr << lastb << ' ' << b << ' ' << dsu.CC << endl;
            ans += 1LL * (lastb - b) * dsu.CC;
        }
        cout << ans << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3552kb

input:

1
3 2
3 2 3
1 2
2 3

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 1107ms
memory: 22940kb

input:

10
90000 89999
29695126 280198164 879119046 430007344 949559367 73085127 889966040 364820814 284458985 931692411 799753826 296370022 333668874 420472873 151595337 343496667 39363958 831424981 863897406 650573429 217091529 533448388 789707546 749864138 599409565 909987504 320920370 476338054 11898738...

output:

15024049952546
16659615874110
400000005
400000003
5601933778666
6160080698685
5988219861596
6982527535131
6237837736440
6956893227831

result:

ok 10 lines