QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207937#6560. Broken Minimum Spanning TreeFreeuni1#RE 0ms3788kbC++142.0kb2023-10-08 23:29:262023-10-08 23:29:26

Judging History

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

  • [2023-10-08 23:29:26]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3788kb
  • [2023-10-08 23:29:26]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
    int n, m, k;
vector<array<int, 4> > v, v1;
vector<array<int, 2> > w[2222];
int d[2222], ok[3333];
vector<array<int, 2> > ans;

int gpar(int a) {
    if (d[a] == a) return a;
    return d[a] = gpar(d[a]);
}

bool dsu(int a, int b) {
    a = gpar(a);
    b = gpar(b);
    if (a == b) {
        return false;
    }
    d[a] = b;
    return true;
}

int dfs(int idx, int par, int tar, int ans) {
    //cout << idx << "< >" << tar << endl;

    if (idx == tar) {
        return ans;
    }
    for (auto it: w[idx]) {
        if (it[0] == par) {
            continue;
        }
        //cout << it[1] << "!!";
        if (ans == -1 && ok[it[1]] && it[1] >= n-1) {

            int pas = dfs(it[0], idx, tar, it[1]);
            if (pas != -1) {
                return pas;
            }
        } else if (ok[it[1]]) {
            int pas = dfs(it[0], idx, tar, ans);
            if (pas != -1) {
                return pas;
            }
        }
    }
    return -1;
}
main() {
    cin >> n >> m;
    for (int i = 0; i < m; i ++) {
        int a, s, h;
        cin >> a >> s >> h;
        v.push_back({h, i, a, s});
        v1.push_back({h, i, a, s});
        w[a].push_back({s, i});
        w[s].push_back({a, i});
    }

    sort(v.begin(), v.end());
    for (int i = 1; i <= n; i ++) {
        d[i] = i;
    }

    for (int i = 0; i < m; i ++) {
        if (dsu(v[i][2], v[i][3])) {
            ok[v[i][1]] = 1;
        }
    }
    int j = 0;
    for (int i = 0; i < n - 1; i ++) {
        if (!ok[i]) {
            ans.push_back({i, 0});
        }
    }

    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i ++) {
        ans[i][1] = dfs(v1[ans[i][0]][2], -1, v1[ans[i][0]][3], -1);
        cout << ans[i][0]+1 << " " << ans[i][1] + 1 << endl;
        assert(ans[i][1] >= 0);
        assert(v1[ans[i][1]][0] > v1[ans[i][1]][1]);
        ok[ans[i][0]] = 1;
        ok[ans[i][1]] = 0;
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3788kb

input:

4 4
1 2 10
2 3 3
3 4 1
1 4 4

output:

1
1 4

result:

ok correct!

Test #2:

score: -100
Runtime Error

input:

6 8
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
1 3 1
4 6 1

output:

2
2 7

result: