lightoj 1003 拓扑裸题

本文介绍了一个有趣的编程问题,通过拓扑排序来判断是否能够按照特定顺序饮酒以达到醉酒状态。问题中定义了不同饮品之间的前置条件,并提供了一种使用C++实现的解决方案。

One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let me share his ideas a bit. I am expressing in my words.

There are many kinds of drinks, which he used to take. But there are some rules; there are some drinks that have some pre requisites. Suppose if you want to take wine, you should have taken soda, water before it. That’s why to get real drunk is not that easy.

Now given the name of some drinks! And the prerequisites of the drinks, you have to say that whether it’s possible to get drunk or not. To get drunk, a person should take all the drinks.

Input
Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with an integer m (1 ≤ m ≤ 10000). Each of the next m lines will contain two names each in the format a b, denoting that you must have a before having b. The names will contain at most 10 characters with no blanks.

Output
For each case, print the case number and ‘Yes’ or ‘No’, depending on whether it’s possible to get drunk or not.

Sample Input
Output for Sample Input
2
2
soda wine
water wine
3
soda wine
water wine
wine water
Case 1: Yes
Case 2: No

这题拓扑排序裸题
大概的意思就是如果有环,那么就一定有剩下的排不出来
一开始把入度为0的都找到然后输出,输出完毕进栈或者队列这个无所谓
把出去的边对应的点的入度减一…
结束就行

#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
#include<memory.h>
#include<stack>
using namespace std;
int du[30000];
vector<int> v[30000];
int main()
{
    int T;
    int u=0;
    cin>>T;
    while(T--)
    {
        memset(du,0,sizeof(du));
        map<string,int> qw;
        qw.clear();
        string q,w;
        int m;
        cin>>m;
        for(int a=0;a<=2*m;a++)v[a].clear();
        for(int a=1;a<=m;a++)
        {
            cin>>q>>w;
            if(!qw[q])qw[q]=qw.size();
            if(!qw[w])qw[w]=qw.size();
            v[qw[q]].push_back(qw[w]);
            du[qw[w]]++;
        }
        int quan=qw.size();
        //for(int a=1;a<=quan;a++)cout<<du[a]<<endl;
        //cout<<quan<<endl;
    //  stack<int> we;

        stack<int> we;
        for(int a=1;a<=qw.size();a++)
            if(du[a]==0)
            {
                quan--;
            //  cout<<quan<<endl;
                we.push(a);
            }
        while(!we.empty())
        {
            int qq=we.top();
            we.pop();
            for(int a=0;a<v[qq].size();a++)
            {
                du[v[qq][a]]--;
                if(du[v[qq][a]]==0)quan--,we.push(v[qq][a]);
            }
        }
        //cout<<quan<<endl;
        if(quan<=0)printf("Case %d: Yes\n",++u);
        else printf("Case %d: No\n",++u);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值