QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187197 | #3846. Link-Cut Tree | bzw | WA | 3ms | 33992kb | C++14 | 1.1kb | 2023-09-24 15:19:34 | 2023-09-24 15:19:35 |
Judging History
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'