码迷,mamicode.com
首页 > 其他好文 > 详细

2015 HDU 多校联赛 5326 Work

时间:2015-07-30 00:44:33      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

2015 HDU 多校联赛 5326 Work


题目: http://acm.hdu.edu.cn/showproblem.php?pid=5326


这题应该是本周二多校赛中,最简单的一道题目了。

解题思路: 就是回根。 用一个数组 root[i] = j 表示 i 的上级是 j , 对于每个输入的关系都做这样的处理。然后遍历每个编号直到root[i] 的结果为0 (因为根没有上级,所以根为0),在往根回退的过程中,用一个数组 cnt[i] 表示经过i这个点的次数。 最后就是遍历 cnt[i],累加 cnt[i] = k 的结果即可AC


#include <bits/stdc++.h>

using namespace std;

const int MAX = 100+2;
int root[MAX] = {};
int cnt[MAX] = {};

// 回根
int getRoot(int n, int k)
{
    for(int i=1; i<=n; ++i)
    {
        int t = i;
        while(root[t])
        {
            cnt[ root[t] ]++;   // 每经过上级,那么上级就累加,有点像上级汇报一样
            t = root[t];
        }
    }

    int ans = 0;
    for(int i=1; i<=n; ++i)
    {
        if (cnt[i] == k)
            ans++;
    }

    return ans;
}

int main(void)
{
    //freopen("in.txt", "r", stdin);

    int n, k;
    while(cin>>n>>k)
    {
        memset(root, 0, sizeof(root));
        memset(cnt, 0, sizeof(cnt));

        int u, v;
        for(int i=1; i<n; ++i)
        {
            scanf("%d%d", &u, &v);
            root[v] = u;
        }

        // 回根
        printf("%d\n", getRoot(n, k));
    }

    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/core__code

2015 HDU 多校联赛 5326 Work

标签:

原文地址:http://blog.csdn.net/core__code/article/details/47137933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!