Tree Painting(欧拉路径性质的使用)

Give you a tree, can you draw the tree with minimum strokes without overlapping? Noted that it is ok if two strokes intersect at one point. Here we define a tree as a connected undirected graph with N points and N-1 edges.

Input

The input data has several test cases. The first line contains a positive integer T (T<=20), indicates the number of test cases.

For each case:

The first line contains an integers N (2<=N<=10^5), indicates the number of points on the tree numbered from 1 to N.

Then follows N-1 lines, each line contains two integers Xi, Yi means an edge connected Xi and Yi (1<=Xi, Yi<=N).

Output

For each test case, you should output one line with a number K means the minimum strokes to draw the tree.

Sample Input

2

2

1 2

5

1 2

1 5

2 3

2 4

Sample Output

1

2

题意:

已知一颗n个结点,n-1条边的树,树上边为无向边,求最少需要花几次才能把这棵树画出来,走过的

路径不能重复

思路:

从一个奇度顶点出发,到另一个奇度顶点,为一条路径

需要画的次数ans = 奇度顶点个数/2

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5+5;
int Deg[N],n;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		memset(Deg,0,sizeof(Deg));
		scanf("%d",&n);
		for(int i=1;i<n;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			Deg[u]++;
			Deg[v]++;
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		   ans+=(Deg[i]&1);
		printf("%d\n",ans/2);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81260510