QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#643583#6560. Broken Minimum Spanning Treeenze114514WA 0ms3632kbC++203.4kb2024-10-15 22:09:352024-10-15 22:09:36

Judging History

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

  • [2024-10-15 22:09:36]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3632kb
  • [2024-10-15 22:09:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define pb push_back

const ld pi = 3.14159265358979323846;
// const int mod = 998244353;
const ll INF = 1e18;

template<typename T>
T chmax(T a, T b) {
    return a > b ? a : b;
}

template<typename T>
T chmin(T a, T b) {
    return a > b ? b : a;
}

const int N = (int)2e5 + 1, M = N * 2;

struct DSU{
    vector<int> node, size;
 
    DSU(){}
    DSU(int n){
        init(n);
    }
 
    void init(int n){
        node.resize(n);
        size.resize(n);
 
        for(int i = 0; i < n; i++){
            node[i] = i;
            size[i] = 1;
        }
    }
 
    int find(int x){
        return x == node[x] ? x : node[x] = find(node[x]);
    }
 
    void erase(int x){
        size[find(x)] -= 1;
        node[x] = x;
    }
 
    void relocate(int x, int y){ //can't be root
        int dx = find(x), dy = find(y);
        if(dx == dy){
            return;
        }
        node[x] = dy;
        size[dx] -= 1;
        size[dy] += 1;
    }
 
    int set(int x, int y){
        return find(x) == find(y);
    }
 
    int merge(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y){
            return 0;
        }
 
        size[x] += size[y];
        node[y] = x;
        return 1;
    }
 
    int heuristic_merge(int x, int y){ 
        x = find(x);
        y = find(y);
        if(size[x] < size[y]){
            swap(x, y);
        }
        return merge(x, y);
    }
 
    int get(int x){
        return size[find(x)];
    }
};

ll get_span(vector<vector<int>> a, int n){
    sort(a.begin(), a.end());
    DSU dsu(n + 1);

    ll qwq = 0, cnt = 0;
    for(int i = 0; i < a.size(); i++){
        int u = a[i][1], v = a[i][2];
        if(dsu.set(u, v)) continue;
        else{
            dsu.heuristic_merge(u, v);
            qwq += a[i][0];
            cnt++;
        }
    }
    if(cnt != n - 1){
        return INF;
    }
    return qwq;
}

void solve(){
    int n, m;
    cin >> n >> m;

    vector<vector<int>> a, b, c;

    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;

        if(i < n - 1){
            a.pb({w, u, v, i + 1});
        }
        else{
            b.pb({w, u, v, i + 1});
        }
    }

    ll owo = get_span(a, n);

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    map<vector<int>, int> mp;

    for(int i = a.size() - 1, j = 0; i >= 0 && j < b.size(); i--){
        if(a[i][0] > b[j][0]){
            vector<vector<int>> d;
            for(int k = 0; k < a.size(); k++){
                if(k == i || mp[a[k]]) continue;
                d.pb(a[k]);
            }
            for(int k = 0; k < c.size(); k++){
                d.pb(b[c[k][2]]);
            }
            d.pb(b[j]);

            ll span = get_span(d, n);
            if(span < owo){
                c.pb({a[i][3], b[j][3], j});
                mp[a[i]] = 1;
                owo = span;
                j++;
            }
        }
    }

    cout << c.size() << endl;
    for(auto p : c){
        cout << p[0] << " " << p[1] << endl;
    }
}

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

    int t = 1;
//    cin >> t;

    while(t--){
        solve();
    }

    return 0;
}

詳細信息

Test #1:

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

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
Wrong Answer
time: 0ms
memory: 3588kb

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:

1
2 7

result:

FAIL participant's MST is better than jury!