QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#197614#6503. DFS Order 3vanthoci#WA 85ms7520kbC++171.5kb2023-10-02 17:42:332023-10-02 17:42:33

Judging History

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

  • [2023-10-02 17:42:33]
  • 评测
  • 测评结果:WA
  • 用时:85ms
  • 内存:7520kb
  • [2023-10-02 17:42:33]
  • 提交

answer

#include<bits/stdc++.h>
#define fer(i,a,b) for(int i = a ; i <= b ; i ++)
const int N = 2e3 + 10 ;
using namespace std;

int f[N] ;
int a[N][N] ;

int find(int x)
{
    return f[x] == x ? f[x] : f[x] = find(f[x]) ;
}

vector<pair<int,int>> res ;
int id[N] , xia[N][N] ;


void de(vector<int> v)
{
    for(auto i : v)
        cout << i << " " ;
    cout << '\n' ;
}

void merge(int a , int b)
{
    int pa = find(a) , pb = find(b) ;
    if(pa != pb)
    {
        f[pa] = pb ;
        res.push_back({a , b}) ;
    }
}
void solution(){
    int n;
    cin>>n;
    //cout << n << '\n' ;
    res.clear() ;
    fer(i,1,n) f[i] = i , id[i] = 1 ;

    vector<int> v ;
    for(int i = 1 ; i <= n ; i ++)
    {
        fer(j,1,n)
            cin >> a[i][j] ,  xia[i][a[i][j]] = j ;
        if(n >= 2)
            merge(a[i][1] , a[i][2]) ;
        //cout << a[i][n] << '\n' ;
        v.push_back(a[i][n]) ;
    }

    
    for(auto i : v)
    {
        //cout << i << '\n' ;
        for(int j = 1 ; j <= n ; j ++)
        {
            int t = xia[j][i] ;
            if(t + 1 <= n)
            {
                //cout << a[j][1] << " " << a[j][t + 1] << '\n' ;
                merge(a[j][1] , a[j][t + 1]) ;
            }
        }
    }

    for(auto i : res)
        cout << i.first << " " << i.second << '\n' ;

    return ;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T; cin >> T;
    while (T--) solution();
    return 0;
}


/*

1 2 
1 2
3 2
1 2
3 2
4 2
1 2
1 3
2 4
3 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7520kb

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
Wrong Answer
time: 85ms
memory: 5536kb

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
3 1
4 10
6 10
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
3 9
8 10
5 9
1 6
2 4
3 8
5 7
9 6
10 2
10 6
3 10
5 10
1 9
2 10
3 6
4 7
5 1
8 9
10 3
4 3
5 7
1 10
2 6
3 8
4 8
5 10
7 9
3 10
9 8
6 10
1 10
2 3
3 7
4 8
5 8
6 7
9 2
2 10
4 10
1 4
2 3
4 2
5 10
6 7
8 4
9 1...

result:

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