QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#302846#6307. Chase Game 2Itisa#WA 1ms6332kbC++141.9kb2024-01-11 14:08:302024-01-11 14:08:30

Judging History

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

  • [2024-01-11 14:08:30]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6332kb
  • [2024-01-11 14:08:30]
  • 提交

answer

#include <iostream>
#include <cstring>
using namespace std;

int read() {
	int r=0,w=1;char ch=getchar();
	while(ch < '0' || ch > '9'){if (ch=='-') {w=-1;}ch=getchar();}
	while(ch >= '0' && ch <='9') {r = r*10+(ch-'0');ch=getchar();}
	return r*w;
}

const int N = 200005;


int head[N];
int nxt[N];
int to[N];
int deg[N];
int cnt;
int lastfa;

int sons;
int ans;
bool ava;
int mm;
void add(int u,int v) {
	cnt++;
	nxt[cnt] = head[u];
	to[cnt] = v;
	head[u] = cnt;
}
void dfs1(int now,int f){
	int c = 0;
	for (int w = head[now]; w != -1; w = nxt[w]) {
		c++;
		int v = to[w];
		
		if (v == f) continue;
		
		dfs1(v,now);
	}
	deg[now] = c;
	
}


int dfs2(int now,int f){
	int c = 0;
	int leaves = 0;
	for (int w = head[now]; w != -1; w = nxt[w]) {

		c++;
		int v = to[w];
		
		if (v == f) continue;
		
		int k = dfs2(v,now);
		if (k == 1) {
			leaves++;
		}
		
	}
	if (leaves > 0){
		if (sons == 0) {
			sons += leaves;
		} else {
			if (sons >= leaves){
				sons -= leaves;
				ans += leaves;
			} else {
				ans += leaves - sons;
				sons = leaves - sons;
			}
			ava = true;
		}
		lastfa = now;
	}
	
	return c;
}


int main(){
	int T = read();
	int n = 0;
	int u,v;
	cnt = 0;
	memset(head,-1,sizeof(head));
	while(T--) {
//	
		for (int i = 0; i <= n; ++i) {
			head[i] = -1;
		}
//		for (int i = 0; i < cnt+1; ++i) {
//			nxt[i] = -1;
//			to[i] = -1;
//		}
//		memset(head,-1,sizeof(head));
		cnt = 0;
		n = read();
		for (int i = 0; i < n-1; ++i) {
			u = read();
			v = read();
			add(u,v);
			add(v,u);
		}
		
		ans = 0;
		sons = 0;
		ava = false;
		dfs1(1,-1);
		
//		return 0;
		
		for (int i = 1; i <= n; ++i) {
			if (deg[i] != 1) {
				int tmp = dfs2(i,-1);
				break;
			}
		}
		
		if (ava == false) {
			printf("-1\n");
		} else {
			printf("%d\n", ans+sons);
		}
		
		
	}	

	
	return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 5956kb

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: 0ms
memory: 6332kb

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

result:

wrong answer 7th numbers differ - expected: '3', found: '4'