现在的位置: 首页 > 综合 > 正文

hdu 2094 产生冠军(水题,拓扑排序)

2017年10月18日 ⁄ 综合 ⁄ 共 885字 ⁄ 字号 评论关闭

小记:只要是只有一个0入度的点,就能产生冠军,否则不可以

思路:如上

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 510;
const int N = 100010;
const int INF = 0x7fffffff;

int in[MAX_], out[MAX_];
int g[MAX_][MAX_];

int n,m,cnt;

map<string ,int>mp;

int topsort() {
    int tmp;
    bool flag = false, ok = true;
   REP(i, 1, cnt){
    if(in[i] == 0){
        if(flag)return 0;
        flag = true;
    }
   }
   if(flag)return 1;
   else
    return 0;
}


int main() {
    int T, s, t;
    string s1, s2;

    while(~scanf("%d", &n), n) {
        mst(g, 0);
        mst(out, 0);
        mst(in, 0);
        mp.clear();
        cnt = 1;

        REP(i, 0, n) {
            cin>>s1>>s2;
            if(mp[s1] == 0){
                mp[s1] = cnt++;
            }
            s = mp[s1];
            if(mp[s2] == 0){
                mp[s2] = cnt++;
            }
            t = mp[s2];

            g[s][t] ++;
            out[s] ++;
            in[t] ++;
        }

        int ans = topsort();

        if(ans){
            printf("Yes\n");
        }else printf("No\n");
    }
    return 0;
}

抱歉!评论已关闭.