hdu-2647 Reward && hdu-2049产生冠军 &&hdu-3342Legal or Not(拓扑排序)

题目链接:

hdu-2647

/*Problem : 2647 ( Reward )     Judge Status : Accepted
RunId : 16919085    Language : G++    Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+6;
typedef long long ll;
const ll mod=1e9+7;
int n,m,ind[N],u,v,vis[N];
vector<int>ve[N];
struct node
{
    int num,pri;
};
node temp;
queue<node>qu;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ve[i].clear();
            vis[i]=ind[i]=0;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            ve[v].push_back(u);
            ind[u]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(!ind[i])
            {
                temp.num=i;
                temp.pri=888;
                qu.push(temp);
            }
        }
        int cnt=n;
        while(!qu.empty())
        {
            node fr=qu.front();
            ans+=(ll)fr.pri;
            qu.pop();
            cnt--;
            int len=ve[fr.num].size();
            for(int i=0;i<len;i++)
            {
                int y=ve[fr.num][i];
                ind[y]--;
                if(ind[y]==0)
                {
                    temp.num=y;
                    temp.pri=fr.pri+1;
                    qu.push(temp);
                }
            }
        }
        if(cnt==0)printf("%lld\n",ans);
        else printf("-1\n");
    }
    return 0;
}

hdu-2049

/*Problem : 2094 ( 产生冠军 )     Judge Status : Accepted
RunId : 16919272    Language : G++    Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+6;
typedef long long ll;
const ll mod=1e9+7;
int n,ind[N];
map<string,int>mp;
vector<int>ve[N];
string str1[N],str2[N];
int main()
{
    while(1)
    {
        scanf("%d",&n);
        if(n==0)break;
        for(int i=1;i<=n;i++)
        {
            ve[i].clear();
            ind[i]=0;
        }
        int cnt=1;
        for(int i=0;i<n;i++)
        {
            cin>>str1[i]>>str2[i];
            if(mp[str1[i]]==0)mp[str1[i]]=cnt++;
            if(mp[str2[i]]==0)mp[str2[i]]=cnt++;
            int u=mp[str1[i]],v=mp[str2[i]];
            ve[u].push_back(v);
            ind[v]++;
        }
        int num=0;
        for(int i=1;i<cnt;i++)
        {
            if(!ind[i])
            {
                num++;
            }
        }
        if(num!=1)printf("No\n");
        else printf("Yes\n");
        for(int i=0;i<n;i++)
        {
            mp[str1[i]]=0;
            mp[str2[i]]=0;
        }

    }
    return 0;
}

hdu-3342

/*Problem : 2647 ( Reward )     Judge Status : Accepted
RunId : 16919085    Language : G++    Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+6;
typedef long long ll;
const ll mod=1e9+7;
int n,m,ind[N],u,v,vis[N];
vector<int>ve[N];
struct node
{
    int num,pri;
};
node temp;
queue<node>qu;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ve[i].clear();
            vis[i]=ind[i]=0;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            ve[v].push_back(u);
            ind[u]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(!ind[i])
            {
                temp.num=i;
                temp.pri=888;
                qu.push(temp);
            }
        }
        int cnt=n;
        while(!qu.empty())
        {
            node fr=qu.front();
            ans+=(ll)fr.pri;
            qu.pop();
            cnt--;
            int len=ve[fr.num].size();
            for(int i=0;i<len;i++)
            {
                int y=ve[fr.num][i];
                ind[y]--;
                if(ind[y]==0)
                {
                    temp.num=y;
                    temp.pri=fr.pri+1;
                    qu.push(temp);
                }
            }
        }
        if(cnt==0)printf("%lld\n",ans);
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-10-09 05:14:59

hdu-2647 Reward && hdu-2049产生冠军 &&hdu-3342Legal or Not(拓扑排序)的相关文章

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

HDU 2647 Reward(图论-拓扑排序)

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,a

ACM: hdu 2647 Reward -拓扑排序

hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble

HDU 2647 Reward(拓扑排序)

Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,and some

HDU 2647 Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9918    Accepted Submission(s): 3165 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

hdu 2647 Reward 拓扑排序。

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4599    Accepted Submission(s): 1400 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

HDU 2647 Reward 拓扑排序

Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how

hdu 2647 Reward(拓扑排序,反着来)

Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 51   Accepted Submission(s) : 21 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Dandelion's uncle is a boss of

HDU 2647 Reward(toposort)

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,a

hdu 2647 Reward(拓扑排序+反图)

题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡: 但是老板很人道, 想满足所有人的要求, 并且很吝啬,想画的钱最少 输入若干个关系 a b a c c b 意味着a 的工资必须比b的工资高 同时a 的工资比c高: c的工资比b高 当出现环的时候输出-1 #include<iostream> #include<cstring> #