zoukankan      html  css  js  c++  java
  • LightOJ

    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 havingb. 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


    PROBLEM SETTER: JANE ALAM JAN:
    思路:直接拓扑排序判断有无环,复杂度O(N+E)
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<vector>
     7 #include<string.h>
     8 #include<math.h>
     9 #include<map>
    10 #include<vector>
    11 char str1[20];
    12 char str2[20];
    13 int cnt[200005];
    14 int top(int ans,int nn,int k);
    15 using namespace std;
    16 vector<int>vec[20005];
    17 int main(void)
    18 {
    19     int i,j,k,p,q;
    20     int s;
    21     scanf("%d",&k);
    22     for(s=1; s<=k; s++)
    23     {
    24         map<string,int>my;
    25         memset(cnt,0,sizeof(cnt));
    26         scanf("%d",&p);
    27         int ans=1;
    28         for(i=0; i<20004; i++)
    29             vec[i].clear();
    30         for(i=0; i<p; i++)
    31         {
    32             scanf("%s %s",str1,str2);
    33             if(my[str1]==0)
    34             {
    35                 my[str1]=ans++;
    36             }
    37             if(my[str2]==0)
    38             {
    39                 my[str2]=ans++;
    40             }
    41             vec[my[str1]].push_back(my[str2]);
    42             cnt[my[str2]]++;
    43         }
    44         int ss=top(ans-1,0,ans-1);
    45         printf("Case %d: ",s);
    46         if(ss)printf("Yes
    ");
    47         else printf("No
    ");
    48     }
    49     return 0;
    50 }
    51 int top(int ans,int nn,int k)
    52 {
    53     int i,j;
    54     queue<int>que;
    55     for(i=1; i<=ans; i++)
    56         if(cnt[i]==0)
    57         {
    58             nn++;
    59             que.push(i);
    60         }
    61     while(!que.empty())
    62     {
    63         int y=que.front();
    64         que.pop();
    65         for(i=0; i<vec[y].size(); i++)
    66         {
    67             cnt[vec[y][i]]--;
    68             if(cnt[vec[y][i]]==0)
    69             {
    70                 nn++;
    71                 que.push(vec[y][i]);
    72             }
    73         }
    74     }
    75     if(nn==k)return 1;
    76     else return 0;
    77 }
    油!油!you@
  • 相关阅读:
    移动端网页头部meta
    fastclick使用方法
    淘宝店铺
    Yii框架下使用redis做缓存,读写分离
    计算一个页面中的数据库查询次数和用时
    数据库优化设计
    工作中使用频率比较高的常规验证器
    框架结构
    smarty
    PDO
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5289539.html
Copyright © 2011-2022 走看看