QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#197151#6503. DFS Order 3maoxuyiWA 149ms4140kbC++141.4kb2023-10-02 12:26:182023-10-02 12:26:19

Judging History

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

  • [2023-10-02 12:26:19]
  • 评测
  • 测评结果:WA
  • 用时:149ms
  • 内存:4140kb
  • [2023-10-02 12:26:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
set <pair<int, int> > mp;
set<pair<int,int> >::iterator it;
bool connect (int x, int y) {
	if (x > y)
		swap(x, y);
	pair<int, int> pii = make_pair(x, y);
	if (mp.count(pii))
		return false;
	mp.insert(pii);
	return true;
}
int tr[1005][1005], vis[1005], h[1005], t[1005], leaf[1005], d[1005], n;
void solve () {
	queue <int> q;
	do {
		if (!q.empty()) {
			int x = q.front();
			q.pop();
			if (t[x] < 1 || vis[x])
				continue;
			while (vis[tr[x][t[x]]] && t[x] > 1) {
				t[x]--;
			}
			if (t[x] >= 1) {
				vis[tr[x][t[x]]] = 1;
			}
		}
		for (int i = 1; i <= n; i++) {
			if (h[i] > n || vis[i])
				continue;
			while (vis[tr[i][h[i]]] && h[i] <= n) {
				h[i]++;
			}
			if (h[i] <= n) {
				connect(i, tr[i][h[i]]);
			}
		}
		for (int i = 1; i <= n; i++) {
			while (vis[t[i]])
				t[i]--;
			if (t[i] >= 1)
				q.push(i);
		}
	}while (!q.empty());
	return;
}
void print () {
	for (it = mp.begin(); it != mp.end(); it++) {
		printf("%d %d\n", it->first, it->second);
	}
	mp.clear();
	return;
}
int main () {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			vis[i] = 0;
			h[i] = 2;
			t[i] = n;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				scanf("%d", &tr[i][j]);
			}
		}
		solve();
		print();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4140kb

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
2 3
1 2
2 3
2 4
1 2
1 3
2 4
3 5

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Wrong Answer
time: 149ms
memory: 3936kb

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

result:

wrong answer your output is not a tree (test case 1)