zoukankan      html  css  js  c++  java
  • Codeforces Gym 100650B Countdown DFS

    Problem B: Countdown
    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88443#problem/B

    Description

    Ann Sister owns a genealogical database service, which maintains family tree history for her clients. When clients login to the system, they are presented with a variety of services: searching, printing, querying, etc. One recent question that came up which the system was not quite prepared for was the following: “Which member of my family had the most grandchildren?” The client who posed this question eventually had to answer it by manually searching the family tree database herself. Ann decided to have software written in case this question (or ones similar to it asking for great-grandchildren, or great-great-grandchildren, etc.) is asked in the future.

    Input

    Input will consist of multiple test cases. The first line of the input will contain a single integer indicating the number of test cases. Each test case starts with a single line containing two positive integers n and d, where n indicates the number of lines to follow containing information about the family tree, and d indicates the specific question being asked about the tree: if d = 1, then we are interested in persons with the most children (1 generation away); if d = 2, then we are interested in persons with the most grandchildren (2 generations away), and so on. The next n lines are of the form name m dname1 dname2 ... dnamem where name is one of the family members’ names, m is the number of his/her children, and dname1 through dnamem are the names of the children. These lines will be given in no particular order. You may assume that all n lines describe one single, connected tree. There will be no more than 1000 people in any one tree, and all names will be at most 10 characters long.

    Output

    For each test case, output the three names with the largest number of specified descendants in order of number of descendants. If there are ties, output the names within the tie in alphabetical order. Print fewer than three names if there are fewer than three people who match the problem criteria (you should not print anyone’s name who has 0 of the specified descendants), and print more than three if there is a tie near the bottom of the list. Print each name one per line, followed by a single space and then the number of specified descendants. The output for each test case should start with the line Tree i: where i is the test case number (starting at 1). Separate the output for each problem with a blank line.

    Sample Input

    3 8 2 Barney 2 Fred Ginger Ingrid 1 Nolan Cindy 1 Hal Jeff 2 Oliva Peter Don 2 Ingrid Jeff Fred 1 Kathy Andrea 4 Barney Cindy Don Eloise Hal 2 Lionel Mary 6 1 Phillip 5 Jim Phil Jane Joe Paul Jim 1 Jimmy Phil 1 Philly Jane 1 Janey Joe 1 Joey Paul 1 Pauly 6 2 Phillip 5 Jim Phil Jane Joe Paul Jim 1 Jimmy Phil 1 Philly Jane 1 Janey Joe 1 Joey Paul 1 Pauly

    Sample Output

    Tree 1: Andrea 5 Don 3 Cindy 2 Tree 2: Phillip 5 Jane 1 Jim 1 Joe 1 Paul 1 Phil 1 Tree 3: Phillip 5

    HINT

    题意

    给你n个父子或者母子关系,然后让你求出前三多的第n代儿子的人是谁

    题解

    就是建一棵树,然后数据范围很小,想怎么弄就怎么弄

    dfs搞一搞就好了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200051
    #define mod 10007
    #define eps 1e-9
    int Num;
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    map<string,int> H;
    int n,d;
    string s1,s2;
    int tot=1;
    vector<int> E[1200];
    struct node
    {
        string s;
        int num;
        int dnum;
    };
    bool cmp(node a,node b)
    {
        if(a.num==b.num)
            return a.s<b.s;
        return a.num>b.num;
    }
    node fam[1200];
    int vis[1200];
    int get_id(string s)
    {
        if(H[s]==0)
        {
            H[s]=tot;
            fam[tot].s=s;
            fam[tot].num=0;
            tot++;
        }
        return H[s];
    }
    int solve(int x,int d)
    {
        if(d==0)
            return 1;
        int ans=0;
        for(int i=0;i<E[x].size();i++)
            ans+=solve(E[x][i],d-1);
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t=read();
        for(int cas=1;cas<=t;cas++)
        {
            int n=read(),d=read();
            tot=1;
            memset(vis,0,sizeof(vis));
            H.clear();
            for(int i=0;i<1100;i++)
                E[i].clear();
            for(int i=0;i<n;i++)
            {
                cin>>s1;
                int k=read();
                for(int j=0;j<k;j++)
                {
                    cin>>s2;
                    E[get_id(s1)].push_back(get_id(s2));
                }
            }
            for(int i=1;i<tot;i++)
            {
                fam[i].num=solve(i,d);
            }
            sort(fam+1,fam+tot,cmp);
            printf("Tree %d:
    ",cas);
            int cc=inf;
            for(int i=1;i<=min(3,tot-1);i++)
            {
                if(fam[i].num==0)
                    break;
                vis[i]=1;
                cc=fam[i].num;
                cout<<fam[i].s<<" ";
                printf("%d
    ",fam[i].num);
            }
            for(int i=1;i<tot;i++)
            {
                if(vis[i]==1)
                    continue;
                if(fam[i].num<cc)
                    break;
                if(fam[i].num==cc)
                {
                    cout<<fam[i].s<<" ";
                    printf("%d
    ",fam[i].num);
                }
            }
            printf("
    ");
        }
    }
  • 相关阅读:
    五分钟秒懂机器学习混淆矩阵、ROC和AUC
    五分钟学会Python装饰器,看完面试不再慌
    LeetCode54 螺旋矩阵,题目不重要,重要的是这个技巧
    Golang——变量的声明与定义
    LeetCode52题,别再问我N皇后问题了
    spark中的pair rdd,看这一篇就够了
    MySQL不香吗,为什么还要有noSQL?
    JBOSS安装配置详细教程
    Aspose.Words关于域的应用
    SqlServer PIVOT行转列
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4740459.html
Copyright © 2011-2022 走看看