QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#792464#6560. Broken Minimum Spanning Treelukamosiashvili#WA 1ms3932kbC++172.1kb2024-11-29 10:31:592024-11-29 10:32:00

Judging History

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

  • [2024-11-29 10:32:00]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3932kb
  • [2024-11-29 10:31:59]
  • 提交

answer

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

array <int, 4> v[3009];
int par[3009], in[3009], t, bo[3009];

vector <pair <int, int> > adj[3009];

int fnd(int a){
    if(par[a] == a) return a; else return par[a] = fnd(par[a]);
}

void mrg(int a, int b){
    a = fnd(a);
    b = fnd(b);

    if(a == b) return;

    par[b] = a;
}

vector <int> st;
vector <pair <int, int> > ans;
int edge;

void dfs(int v){
    bo[v] = 1;
    for(auto to:adj[v]){
        if(to.first == t){
            //cout << "found\n";
            st.push_back(to.second);
            for(int j = 0; j < st.size(); j++){
                if(in[st[j]]){
                    in[st[j]] = 0;
                    ans.push_back({edge, st[j]});
                    t = -1;
                    return;
                }
            }
            return;
        }

        if(bo[to.first]) continue;
        st.push_back(to.second);
        dfs(to.first);
        if(t == 1) return;
    }

    st.pop_back();
}

int main(){
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> v[i][2] >> v[i][3] >> v[i][0];
        if(i < n){
            v[i][1] = -i;
        }else{
            v[i][1] = i;
        }
    }

    sort(v + 1, v + m + 1);

    for(int i = 1; i <= n; i++) par[i] = i;
    for(int i = 1; i <= m; i++){
        if(fnd(v[i][2]) != fnd(v[i][3])){
            mrg(v[i][2], v[i][3]);
            adj[v[i][2]].push_back({v[i][3], abs(v[i][1])});
            adj[v[i][3]].push_back({v[i][2], abs(v[i][1])});
            in[i] = 1;
        }
    }

    vector <int> a, b;
    for(int i = 1; i <= m; i++){
        if(v[i][1] < 0 && in[i] == 0){
            b.push_back(-v[i][1]);
        }

        if(v[i][1] > 0 && in[i] == 1){
            a.push_back(v[i][1]);
        }
    }

    for(int i = 0; i < a.size(); i++) ans.push_back({b[i], a[i]});

    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i].first << " " << ans[i].second << "\n";
    }
    return 0;
}

詳細信息

Test #1:

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

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: 1ms
memory: 3932kb

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
4 7
1 8

result:

wrong answer not a spanning tree after swap 1