QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187197#3846. Link-Cut TreebzwWA 3ms33992kbC++141.1kb2023-09-24 15:19:342023-09-24 15:19:35

Judging History

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

  • [2023-09-24 15:19:35]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:33992kb
  • [2023-09-24 15:19:34]
  • 提交

answer

// qwq
#include <bits/stdc++.h>
using namespace std;
constexpr int N=1e6+1;
int T,a[N],b[N],fa[N],n,m,pr[N];
vector<int> e[N];
int gf(int x){return fa[x]==x?x:fa[x]=gf(fa[x]);}
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            e[i].clear(),fa[i]=i,pr[i]=0;
        for(int i=1;i<=m;i++)
            cin>>a[i]>>b[i];
        bool ok=false;
        for(int i=1;i<=m;i++){
            if(gf(a[i])==gf(b[i])){
                queue<int>q;q.push(a[i]);pr[a[i]]=-1;
                while(!q.empty()){
                    int u=q.front();q.pop();
                    for(int v:e[u])if(!pr[v])
                        pr[v]=u,q.push(v);
                }
                int x=b[i];
                while(x!=a[i])cout<<x<<' ',x=pr[x];
                cout<<a[i]<<'\n';
                ok=1;
                break;
            }
            else
                e[a[i]].push_back(b[i]),
                e[b[i]].push_back(a[i]),
                fa[gf(a[i])]=gf(b[i]);
        }
        if(!ok)puts("-1");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 33992kb

input:

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

output:

4 3 2 5
-1

result:

wrong answer 1st lines differ - expected: '2 4 5 6', found: '4 3 2 5'