HDU Multi-School League 5326 work

Source: Internet
Author: User

HDU Multi-School League 5326 work


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


This should be the simplest topic of the two-school game of the week.

Problem-solving idea: It is back to root. Using an array of root[i] = J means that the ancestor of I is J, and this is handled for each input relationship. The result of traversing each number until Root[i] is then 0 (because the root does not have a superior, so the root is 0), and in the process of going back to the root, an array of cnt[i] is used to indicate the number of times I have passed this point. The end is to traverse Cnt[i], add cnt[i] = k results can be AC


#include <bits/stdc++.h>using namespace Std;const int MAX = 100+2;int Root[max] = {};int Cnt[max] = {};//back to root int getr oot (int n, int k) {for    (int i=1; i<=n; ++i)    {        int t = i;        while (Root[t])        {            cnt[root[t]]++;   Each pass the superior, then the superior accumulates, somewhat like the superior reports the same            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;        }        Back        to the root printf ("%d\n", Getroot (n, k));    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Http://blog.csdn.net/core__code

HDU Multi-school league 5326 work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.