QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#41722#1265. Total Eclipseznk2373065134AC ✓639ms12592kbC++111.5kb2022-07-31 18:54:272022-07-31 18:54:30

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-31 18:54:30]
  • 评测
  • 测评结果:AC
  • 用时:639ms
  • 内存:12592kb
  • [2022-07-31 18:54:27]
  • 提交

answer

#include <stdio.h>
#include <vector>
#include <algorithm>
typedef long long ll;

const int MAXN = 100010;
int T, n, m, tot, a[MAXN], fa[MAXN], h[MAXN];//h[i]表示集合i中最小的点的亮度
ll ans;
bool vis[MAXN];
std::vector<int> e[MAXN];

void insert(int u, int v) {
    e[u].push_back(v);
    e[v].push_back(u);
}

bool cmp(int p, int q) {
    return h[p] > h[q];
}

int getfa(int x) {
    if (fa[x] == x) return x;
    return fa[x] = getfa(fa[x]);
}

void merge(int u, int v) {
    int x = getfa(u), y = getfa(v);
    if (x != y) {
        ans += abs(h[x] - h[y]);
        fa[y] = x;
        h[x] = std::min(h[x], h[y]);
    }
}

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            e[i].clear();
            fa[i] = i;
            vis[i] = false;
            scanf("%d", &h[i]);
            a[i] = i;
        }
        int u, v;
        for (int i = 1; i <= m; ++i) {
            scanf("%d%d", &u, &v);
            insert(u, v);
        }
        ans = 0;
        std::sort(a + 1, a + n + 1, cmp);
        for (int i = 1; i <= n; ++i) {
            u = a[i];
            vis[u] = true;
            for (auto v : e[u]) {
                if (vis[v]) {
                    merge(u, v);
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (getfa(i) == i) {
                ans += h[i];
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 6852kb

input:

1
3 2
3 2 3
1 2
2 3

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 639ms
memory: 12592kb

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