• hust 1032 Jim


    题目描述

    Jim is boss of a big company . There are so many workers in his company that it will take a long time for his command send to all of his workers . One day he want to know how long it will take at least . So he ask you , one of his best programer , to solve the problem . All the workers in Jim's conpany will receive command from only one people . So there is a tree (the root is Jim) identify the order Jim's command will send by . One send command to another will cost 1 minute and anyone can't send command to more than one people at the same time .

    输入

    There will be multiple cases in the input . For each case there will be just one number N (N<=10000) in the first line , then N-1 fellowed , each line with two names: worker1 worker2 , that is to say worker2 send command to worker1 . The name of the workers will only contain characters and the length is at most 5 .

    输出

    Just one number to tell how many minutes it will cost at least .

    样例输入

    5
    Tom Jim
    Bob Jim
    John Tom
    Mily Tom
    

    样例输出

    3
    唉!这个题一直做错,最后是在厚着脸皮问了学长,结果被学长狂虐了,树形dp,膜拜大神他们啊
    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<vector>
    using namespace std;
    
    vector<int>tree[10001];
    map<string,int>people;
    typedef map<string,int>::iterator Itr;
    Itr itr;
    int nowmax;
    bool vis[10001];
    bool cmp(int x,int y){return x>y;}
    
    int dfs(int u)
    {
        int a[10001];
        int sum=tree[u].size();
        if (sum==0) return 0;
        int ans=0,k=0;
        for (int i=0;i<tree[u].size();i++)
        {
            int v=tree[u][i];
            if (!vis[v])
            {
                vis[v]=true;
                a[++k]=dfs(v);
            }
        }
        sort(a+1,a+sum+1,cmp);
        for (int i=1;i<=sum;i++)
        ans=max(ans,a[i]+i);
        return ans;
    }
    
    int main()
    {
        int n,m;
        string str1,str2;
        m=0;
        while (scanf("%d",&n)!=EOF)
        {
            if (n==1)
            {
                cout<<"0"<<endl;
                continue;
            }
            for (int i=0;i<=n;i++) tree[i].clear();
            m=0;
            people.clear();
            for (int i=1;i<=n-1;i++)
            {
                cin>>str1>>str2;
                if(people.count(str1)==0)
                {
                    people[str1]=++m;
                    if (people.count(str2)==0)
                    {
                        people[str2]=++m;
                        tree[m].push_back(m-1);
                    }
                    else tree[people[str2]].push_back(m);
                }
                else
                {
                    if (people.count(str2)==0)
                    {
                        people[str2]=++m;
                        tree[m].push_back(people[str1]);
                    }
                    else tree[people[str2]].push_back(people[str1]);
                }
            }
            int u=people["Jim"];
            memset(vis,0,sizeof(vis));
            vis[u]=true;
            cout<<dfs(u)<<endl;
        }
        return 0;
    }

    毕竟是自己写的程序,加油

    至少做到我努力了
  • 相关阅读:
    tyvj1061Mobile Service
    POJ3666序列最小差值
    POJ2279杨氏矩阵+钩子定理
    POJ2127 LICS模板
    codevs2189数字三角形(%100)
    qhfl-7 结算中心
    qhfl-6 购物车
    qhfl-5 redis 简单操作
    qhfl-4 注册-登录-认证
    qhfl-3 Course模块
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3721207.html
Copyright © 2020-2023  润新知