QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#265493#6503. DFS Order 3obssWA 0ms3644kbC++171.4kb2023-11-25 18:47:072023-11-25 18:47:07

Judging History

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

  • [2023-11-25 18:47:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3644kb
  • [2023-11-25 18:47:07]
  • 提交

answer

#include <bits/stdc++.h>
#define tests
#define int long long
const int N = 1e3 + 10;
using namespace std;
using LL = long long;
int g[N][N];
int st[N],ed[N],p[N];
bool del[N];
int find(int x){
    if(p[x]!=x) p[x]=find(p[x]);
    return p[x];
}
void solve() {
    int n;  cin>>n;
    if(n==1)    return ;
    vector<pair<int,int>>ans;
    auto merge=[&](int a,int b){
        a=find(a);b=find(b);
        if(a!=b){
            p[a]=b;
            ans.push_back({a,b});
        }
    };
    for(int i=1;i<=n;i++)   p[i]=i;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)   cin>>g[i][j];
        st[i]=1;ed[i]=n; del[i]=0;
        merge(g[i][1],g[i][2]);
    }
    while(ans.size()<n-1){
        vector<int>ye;
        for(int i=1;i<=n;i++){
            if(ed[i]>=st[i]&&!del[g[i][ed[i]]]){
                del[g[i][ed[i]]]=1;   ye.push_back(g[i][ed[i]]);
            }
        }
        for(auto v:ye){
            for(int i=st[v];i<=ed[v];i++){
                if(!del[g[v][i]]){
                    st[v]=i+1;  merge(v,g[v][i]);   break;
                }
            }
        }
        for(int i=1;i<=n;i++){
            while(del[g[i][ed[i]]]) ed[i]--;
        }
    }
    for(auto [x,y]:ans) cout<<x<<' '<<y<<endl;
}

signed main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    cout.tie(0);

    int t = 1;
#ifdef tests
    cin >> t;
#endif
    for(int _ = 1; _ <= t; _ ++) solve();
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3644kb

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
5 4

result:

wrong answer ord[1] didn't pass the test (test case 4)