light oj 1003 - Drunk

這題看似簡單,但蒟蒻我做起來確實花了不少功夫~
大致題意:
給出T組數據,每一組輸入兩個字符串,意爲要想喝後面那種飲料就必須先喝前面的,看能不能喝完所有飲料。
若能輸出“Yes”
else printf(“No\n”);
思路:
就是一個裸的搜環,若搜到了就直接退出,一開始的字符串就用map處理好成數字。

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<map>
    #include<algorithm>
    using namespace std;

    map<string,int>q;

    struct node{
        char s[60];
    };
    const int maxn=100010;
    int to[maxn],Begin[maxn],e,Next[maxn];
    void add(int x,int y){
        to[++e]=y;
        Next[e]=Begin[x];
        Begin[x]=e;
    } 
    node b[20010];
    char chuan1[20010];
    char chuan2[20010];
    bool flag1,flag2;
    int cnt;
    bool p[20010];
    bool flag;
    void dfs(int now)
    {
        if(flag)return;
        for(int i=Begin[now];i;i=Next[i]){
            if(p[to[i]]){
                flag=1;
                return;
            }
                if(!p[to[i]]){
                p[to[i]]=1;
                dfs(to[i]);
                p[to[i]]=0;
            }
        }
    }
    int main(){
        int i,j,k,m,n,_,end1,end2,Case=0;
        scanf("%d",&_);
        while(_--){
            memset(Begin,0,sizeof(Begin));
            Case ++ ;
            memset(p,0,sizeof(p));
            q.clear();
            scanf("%d",&m);
            for(i=1;i<=m;i++){
                scanf("%s%s",chuan1,chuan2);
                if(!q[chuan1]) q[chuan1]=++cnt;
                if(!q[chuan2]) q[chuan2]=++cnt;
                add(q[chuan1],q[chuan2]);
                flag=0;
            for(i=1;i<=cnt;i++){
                p[i]=1;
                dfs(i);
                memset(p,0,sizeof(p));
                if(flag){
                    printf("Case %d: No\n",Case);
                    break;
                }
            }
            if(!flag)
            printf("Case %d: Yes\n",Case);
        }
        return 0;
    }
發佈了55 篇原創文章 · 獲贊 52 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章