• Lightoj 1003


    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

    拓扑排序,判断是否有环。

    嗯...只要剩下点就是有环 好久没看自己以前 写的博客了http://fengweiding.blog.163.com/blog/static/2300541212014112831155847/

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/6/7 20:22:01
    File Name     :1003.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    vector<int>edge[maxn];
    int m,num;
    map<string,int>mp;
    int in[maxn];
    bool topsort(){
        int cnt=0;
        queue<int>q;
        for(int i=1;i<=num;i++)if(in[i]==0)q.push(i);
        while(!q.empty()){
            int u=q.front();q.pop();
            cnt++;
            for(int i=0;i<edge[u].size();i++){
                int v=edge[u][i];
                in[v]--;
                if(in[v]==0){
                    q.push(v);    
                }
            }
        }
        if(cnt<num)return false;
        return true;
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int T;
        string s1,s2;
        cin>>T;
        for(int t=1;t<=T;t++){
            cin>>m;
            num=0;mp.clear();cle(in);
            for(int i=0;i<maxn;i++)edge[i].clear();
            for(int i=1;i<=m;i++){
                cin>>s1>>s2;
                if(!mp[s1])mp[s1]=++num;
                if(!mp[s2])mp[s2]=++num;
                edge[mp[s1]].push_back(mp[s2]);
                in[mp[s2]]++;
            }
            printf("Case %d: %s
    ",t,topsort()?"Yes":"No");
        }
        return 0;
    }
  • 相关阅读:
    JSP笔记05——生命周期(转)
    JSP笔记04——架构(转)
    JSP笔记03——环境搭建(转)
    JSP笔记02——概述(转)
    JSP笔记01——尝试
    【Head First Servlets and JSP】笔记14:session再探 & Listener示例
    【stl学习笔记】list
    【stl学习笔记】deques
    【stl学习笔记】vector
    【APUE】用户态与内核态的区别
  • 原文地址:https://www.cnblogs.com/pk28/p/5568263.html
Copyright © 2020-2023  润新知