QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112429 | #6503. DFS Order 3 | crzsty# | RE | 2ms | 3596kb | C++14 | 2.5kb | 2023-06-11 17:42:48 | 2023-06-11 17:42:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=1010;
int n;
pair <int,int> G[M][M];
//pair <int,int> NG[M][M];
int mp[M][M];
pair <int,int> ans[M];
int tot,tim;
int f[M];
int findf(int a){
return f[a]==a?a:f[a]=findf(f[a]);
}
void addans(int a,int b){
if(mp[a][b]!=tim){
mp[a][b]=mp[b][a]=tim;
// printf("AD %d %d\n",a,b);
ans[++tot]=make_pair(a,b);
}
}
int had[M],idx[M];
int hadb[M][M],res[M];
int topp;
int decd;
pair <int,int> hb[M];
void solve(int hcnt,int coln){
// printf("%d %d %d\n",hcnt,coln,decd);
// for(int i=1;i<=hcnt;i++){
// for(int j=1;j<=coln;j++){
// printf("[%d %d] ",G[i][j].first,G[i][j].second);
// } printf("\n");
// }
if(hcnt==1||coln==1) return;
int next_coln=coln;
topp=0;
decd=0;
for(int i=1,a,b;i<=hcnt;i++){
if(coln==n){
addans(G[i][1].first,G[i][2].first);
a=findf(G[i][1].first);
b=findf(G[i][2].first);
if(a<b)swap(a,b);
if(a^b) f[a]=b,--next_coln;
}else{
a=G[i][1].second;
b=G[i][2].second;
if(hadb[a][b]==coln) continue;
hadb[a][b]=hadb[b][a]=coln;
hb[++topp]=make_pair(a,b);
int ii=idx[b];
// printf("%d-> H:%d BEL:%d\n",G[i][2].first,ii,a);
for(int j=2;j<=coln;j++){
if(G[ii][j].second==a){
addans(G[i][2].first,G[ii][j].first);
++decd;
break;
}
}
}
}
if(coln!=n&&5*decd<=hcnt){
exit(-1);
}
for(int i=1,a,b;i<=topp;i++){
a=findf(hb[i].first);
b=findf(hb[i].second);
if(a==b) continue;
if(b>a)swap(a,b);
f[a]=b;--next_coln;
}
int nowup=0;
for(int i=1;i<=n;i++){
if(findf(i)==i){
res[++nowup]=i;
}
}
for(int i=1;i<=n;i++)had[i]=0;
for(int i=1;i<=nowup;i++){
int h=idx[res[i]];
int nowt=0;
for(int j=1;j<=coln;j++){
int a=findf(G[h][j].first);
if(had[a]!=i){
had[a]=i;
G[i][++nowt].first=G[h][j].first;
G[i][nowt].second=a;
}
}
}
for(int i=1;i<=nowup;i++){
idx[res[i]]=i;
}
solve(nowup,next_coln);
}
int main(){
// freopen("test.in","r",stdin);
int T;
for(scanf("%d",&T);T;T--){
scanf("%d",&n);
tot=0;
++tim;
for(int i=1;i<=n;i++){
f[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&G[i][j].first);
G[i][j].second=G[i][j].first;
}
}
for(int i=1;i<=n;i++)idx[i]=i;
solve(n,n);
if(tot!=n-1) return 0;
// return 0;
for(int i=1;i<=tot;i++){
printf("%d %d\n",ans[i].first,ans[i].second);
}
}
return 0;
}
/*
1
7
1 2 7 3 5 4 6
2 7 1 3 4 6 5
3 5 4 6 1 2 7
4 6 3 1 2 7 5
5 3 1 2 7 4 6
6 4 3 5 1 2 7
7 2 1 3 5 4 6
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3596kb
input:
4 2 1 2 2 1 3 1 2 3 2 1 3 3 2 1 4 1 2 3 4 2 1 3 4 3 2 4 1 4 2 1 3 5 1 2 4 3 5 2 4 1 3 5 3 5 1 2 4 4 2 1 3 5 5 3 1 2 4
output:
1 2 1 2 3 2 1 2 3 2 4 2 1 2 2 4 3 5 3 1
result:
ok correct answer! (4 test cases)
Test #2:
score: -100
Runtime Error
input:
20000 10 1 2 4 5 6 7 3 8 10 9 2 1 4 5 6 7 3 8 10 9 3 8 1 2 4 5 6 7 10 9 4 5 6 7 1 2 3 8 10 9 5 4 6 7 1 2 3 8 10 9 6 7 4 5 1 2 3 8 10 9 7 6 4 5 1 2 3 8 10 9 8 3 1 2 4 5 6 7 10 9 9 10 1 2 4 5 6 7 3 8 10 1 2 4 5 6 7 3 8 9 10 1 4 3 8 2 9 6 5 7 10 2 8 9 6 3 4 1 5 7 10 3 8 2 9 6 4 1 5 7 10 4 1 3 8 2 9 6 5...
output:
1 2 3 8 4 5 6 7 9 10 10 1 4 1 1 3 6 4 1 4 2 8 3 8 5 4 6 9 7 5 10 7 3 4 9 8 1 9 2 4 3 10 5 6 7 5 8 2 5 9 10 8 10 9 1 6 2 4 3 8 5 7 9 6 10 2 6 10 7 8 7 6 1 9 2 10 3 6 4 7 5 1 8 9 7 3 7 5 3 10 1 10 2 6 3 8 4 8 5 10 7 9 9 8 6 10 8 10 1 10 2 3 3 7 4 8 5 8 6 7 9 2 5 7 2 10 1 4 2 3 4 2 5 10 6 7 8 4 9 1 7 4...