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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值