HRBUST 2310 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

题意

给你一颗树,求需要用最少几笔才能画出整棵树

题解

由于是一颗树,任意两点都连通,所以最后只需要判断一幅图需要几笔画出

判断是否无向图欧拉路径,只需要判断度数为奇数的节点数量g(0和2为1笔,其余为数量/2笔)

一颗树的话奇点数g=0不存在

代码

#include<cstdio>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,u,v,D[100005]={0};
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            D[u]++;D[v]++;
        }
        int g=0;
        for(int i=1;i<=n;i++)
            if(D[i]%2==1)
                g++;
        printf("%d\n",g/2);
    }
    return 0;
}

 

posted on 2018-02-27 22:49  大桃桃  阅读(180)  评论(0编辑  收藏  举报

导航