QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#81632#5520. Distance ParitiesSmallbasic#TL 2ms3408kbC++141.7kb2023-02-25 17:23:442023-02-25 17:23:44

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-25 17:23:44]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3408kb
  • [2023-02-25 17:23:44]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 505;

inline int read() {
	register int s = 0, f = 1; register char ch = getchar();
	while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
	while (isdigit(ch)) s = (s * 10) + (ch & 15), ch = getchar();
	return s * f;
}

inline int readdig() {
	register int s = 0; register char ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	return (ch & 15);
}

vector<int> g[N]; 

int e[N][N], dis[N][N], n;
bool vis[N];

inline void dfs(int now) {
	if (vis[now]) return ;
	vis[now] = 1;
	for (int i : g[now])
		if (!vis[i]) dfs(i);
}

inline void bfs(int now) {
	dis[now][now] = 0;
	queue<int> q; q.push(now);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (int i : g[x])
			if (!dis[now][i] && i != now)
				dis[now][i] = dis[now][x] + 1, q.push(i);
	}
}

int main() {
	int T; cin >> T;
	while (T--) {
		cin >> n; int cnt = 0;
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j) {
				cnt += e[i][j] = readdig();
				if (e[i][j]) {
					g[i].push_back(j);
					g[j].push_back(i);
				}
			}
		dfs(1);
		for (int i = 1; i <= n; ++i) {
			if (!vis[i]) {
				puts("NO");
				goto fail;
			}
		}
		for (int i = 1; i <= n; ++i) bfs(i);
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j)
				if ((dis[i][j] & 1) != e[i][j]) {
					puts("NO");
					goto fail;
				}
		}
		puts("YES");
		cout << cnt / 2 << endl;
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j < i; ++j)
				if (e[i][j]) cout << i << ' ' << j << endl;
		fail:;
		for (int i = 1; i <= n; ++i) {
			g[i].clear(); vis[i] = 0;
			for (int j = 1; j <= n; ++j)
			 e[i][j] = dis[i][j] = 0;
		}
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3408kb

input:

3
3
011
101
110
4
0100
1000
0001
0010
5
01010
10101
01010
10101
01010

output:

YES
3
2 1
3 1
3 2
NO
YES
6
2 1
3 2
4 1
4 3
5 2
5 4

result:

ok Correct (3 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

1
500
001001010000101001100000100011101011010001100110010000011000001100000011010001001111001010010101110100000100011000110111100010001000010111111000000101101010011111000010110010111100111110111000010000100100010010001110000100111000001111101011111101111110111110001000111110001011111100110011100100...

output:

YES
62433
3 1
3 2
5 3
5 4
6 1
6 4
6 5
7 2
8 1
8 2
8 3
8 5
8 6
8 7
9 2
9 6
9 8
10 3
10 5
10 6
10 7
10 8
11 4
11 5
11 6
11 9
11 10
12 3
12 8
12 9
12 10
12 11
13 1
13 4
13 6
13 7
13 8
13 12
14 4
14 7
14 9
14 13
15 1
15 2
15 3
15 6
15 7
15 9
15 10
15 12
15 13
15 14
16 2
16 3
16 5
16 6
16 8
16 10
16 11
1...

result: