QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358374#6307. Chase Game 2Djangle162857#WA 10ms10044kbC++201.4kb2024-03-19 19:29:502024-03-19 19:29:51

Judging History

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

  • [2024-03-19 19:29:51]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:10044kb
  • [2024-03-19 19:29:50]
  • 提交

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[c[i]] > 2)
			ans++;
	}
	printf("%d\n", ans);
}


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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 5ms
memory: 9964kb

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
2
2
2
1
2
-1
2
2
1
2
2
1
1
1
-1
2
2
2
1
-1
1
1
2
1
1
-1
1
2
1
1
1
-1
1
1
2
2
2
1
1
1
-1
1
2
1
1
2
1
2
1
1
2
-1
-1
-1
2
2
2
1
1
1
2
2
2
-1
1
2
-1
1
1
-1
2
-1
-1
1
2
2
2
1
1
1
1
1
1
1
1
1
2
-1
1
1
2
-1
2
1
1
1
-1
2
-1
1
-1
-1
2
-1
2
1
2
2
1
1
1
1
2
1
1
1
1
-1
2
1
2
1
1
1
1
1
1
1
2
-1...

result:

ok 10000 numbers

Test #3:

score: -100
Wrong Answer
time: 10ms
memory: 10044kb

input:

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

output:

1
2
4
4
3
2
3
3
3
3
3
2
2
3
3
2
3
2
3
3
3
3
3
3
2
3
3
4
3
3
4
3
2
3
3
4
3
3
3
4
3
3
3
3
4
3
2
3
3
3
3
3
4
2
4
2
3
3
2
3
4
4
3
3
4
2
2
3
4
2
3
2
4
4
3
4
3
3
3
4
4
4
2
4
2
2
2
3
3
2
3
2
3
3
3
4
3
3
3
2
4
3
3
3
2
3
4
2
3
2
3
2
3
3
3
3
2
3
3
3
3
3
2
2
3
3
3
2
3
4
4
3
2
4
4
4
4
4
3
3
3
3
3
4
3
3
3
3
3
3
...

result:

wrong answer 2nd numbers differ - expected: '3', found: '2'