QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#358346#6307. Chase Game 2Djangle162857#WA 5ms9984kbC++201.4kb2024-03-19 19:18:492024-03-19 19:18:50

Judging History

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

  • [2024-03-19 19:18:50]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:9984kb
  • [2024-03-19 19:18:49]
  • 提交

answer

#include <bits/stdc++.h>
// M
using namespace std;
const int N = 200010;
int n, cnt, c[N];


struct Graph {
	int tot, root, ind[N], head[N], suiv[N << 1], ver[N << 1], dis[N << 1],
		fa[N << 1];

	inline void init()
	{
		for (int i = 0; i <= tot; i++)
			head[i] = dis[i] = ind[i] = fa[i] = 0;
		root = tot = 0;
	}

	inline void lnk(int x, int y)
	{
		ver[++tot] = y;
		suiv[tot] = head[x];
		head[x] = tot;
		ind[y]++;
	}

	inline void dfs(int x)
	{
		//	cout << "dfs " << x << endl;
		dis[x] = dis[fa[x]] + 1;
		if (dis[x] > dis[root])
			root = x;
		for (int i = head[x]; i; i = suiv[i]) {
			int y = ver[i];
			if (y == fa[x])
				continue;
			fa[y] = x;
			dfs(y);
		}
	}

} e;


inline void sol()
{
	cnt = 0;
	e.init();
	scanf("%d", &n);
	for (int i = 1, x, y; i < n; i++)
		scanf("%d%d", &x, &y), e.lnk(x, y), e.lnk(y, x);

	int ind_cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (e.ind[i] > 1)
			ind_cnt++;
	}

	if (ind_cnt <= 1) {
		puts("-1");
		return;
	}

	e.dfs(1);
	e.dis[e.root] = 1;
	e.fa[e.root] = 0;
	e.dfs(e.root);
	for (int i = e.root; i; i = e.fa[i]) {
		c[++cnt] = i;
	}
	// for (int i = 1; i <= cnt; i++)
	//	cout << c[i] << endl;
	// cout << endl;

	int ans = 1;


	for (int i = 2; i < cnt; i++) {
		if (e.ind[i] > 2)
			ans++;
	}
	printf("%d\n", ans);
}


signed main()
{
	int T;
	scanf("%d", &T);
	while (T--)
		sol();
	return 0;
}

详细

Test #1:

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

input:

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

output:

-1
1
-1
2

result:

ok 4 number(s): "-1 1 -1 2"

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 9936kb

input:

10000
4
1 2
1 3
3 4
4
1 2
1 3
1 4
4
1 2
2 3
1 4
5
1 2
2 3
1 4
4 5
5
1 2
2 3
3 4
4 5
4
1 2
2 3
2 4
5
1 2
1 3
2 4
2 5
4
1 2
2 3
1 4
5
1 2
1 3
2 4
1 5
5
1 2
2 3
3 4
2 5
5
1 2
1 3
2 4
2 5
4
1 2
1 3
3 4
5
1 2
1 3
3 4
1 5
4
1 2
1 3
1 4
5
1 2
1 3
3 4
3 5
5
1 2
2 3
3 4
3 5
4
1 2
1 3
2 4
5
1 2
2 3
2 4
3 5
5
...

output:

1
-1
1
1
1
-1
2
1
1
2
2
1
1
-1
2
2
1
2
1
1
1
1
-1
1
2
2
1
-1
1
1
2
1
1
-1
1
1
1
1
1
-1
1
1
2
1
1
1
1
1
-1
1
2
1
1
2
1
2
1
1
1
-1
-1
-1
2
2
2
1
1
1
1
2
1
-1
1
1
-1
1
1
-1
2
-1
-1
1
1
1
2
1
1
1
1
1
1
1
1
1
2
-1
1
1
1
-1
1
1
1
1
-1
1
-1
1
-1
-1
2
-1
1
1
2
1
1
1
1
1
2
1
1
1
1
-1
2
1
1
1
1
1
1
1
1
1
1
-1...

result:

wrong answer 9th numbers differ - expected: '2', found: '1'