QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121823#6503. DFS Order 3P3KOTL 1ms4052kbC++14999b2023-07-08 21:44:392023-07-08 21:44:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-08 21:44:42]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4052kb
  • [2023-07-08 21:44:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int MAXN=1005;

int n;
deque<int> order[MAXN];
bool vis[MAXN][MAXN];

void init(int x){
	for(int i=1;i<=x;i++)
		order[i].clear();
}

int main(){
	int t;cin>>t;
	while(t--){
		vector<pair<int,int>>ans;
		cin>>n;
		for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
			int x;cin>>x;
			order[i].push_back(x);
		}
		int cnt=0;
		while(cnt<n-1){
			int tag=0;
			for(int i=1;i<=n;i++){
				if(!order[i].empty()&&!tag)tag=order[i].back();
				if(!order[i].empty()&&order[i].back()==tag)order[i].pop_back();
			}
			int nxt=0;
			for(int i=1;i<=n;i++)
				if(!order[i].empty()&&order[i].front()==tag){
					order[i].pop_front();
					nxt=order[i].front();
				}
			if(!vis[tag][nxt]&&nxt){
				ans.push_back({tag,nxt});
				cnt++;
				vis[tag][nxt]=vis[nxt][tag]=1;
			}
		}
		for(auto x:ans){
			cout<<x.first<<" "<<x.second<<"\n";
			vis[x.first][x.second]=0;
			vis[x.second][x.first]=0;
		}
		init(n);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:

2 1
3 2
2 1
4 2
3 2
2 1
5 3
3 1
4 2
2 1

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Time Limit Exceeded

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:

9 10
10 1
8 3
3 1
7 6
6 4
5 4
2 1
7 4

result: