题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170

根据题意,需要找到度数为1的结点个数,如下图:

image

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 10002

vector<int> g[N];

int main()
{
    freopen("d:\\in.txt", "r", stdin);
    int n;
    while(~scanf("%d", &n)) {
        int a, b;
        for(int i=0; i<n+2; i++){
            g[i].clear();
        }
        for(int i=0; i<n-1; i++){
            scanf("%d%d", &a, &b);
            g[a].push_back(b);
            g[b].push_back(a); 
        }
        int tot = 0;//度为1的个数
        for(int i=1; i<=n; i++){
            if(g[i].size() == 1)
                tot++;
        } 
        printf("%d\n", (tot+1)/2); 
    }
}
 posted on 2015-04-02 08:46  平和之心  阅读(142)  评论(0编辑  收藏  举报