Hdu 2094 产生冠军

STL map + 拓扑排序。

 

 

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

const  int SIZE =  5000;

int ind[SIZE];
int cnt;
map < stringint> Map;

void init()
{
    Map.clear();
    memset(ind,  0sizeof(ind));
    cnt =  0;
}

int main()
{
     int n;
     char sz1[ 31], sz2[ 31];
     while(~scanf( " %d ", &n), n)
    {
        init();
         for( int i =  1; i <= n; i++)
        {
            scanf( " %s %s ", sz1, sz2);
             if(!Map[sz1])
            {
                Map[sz1] = ++cnt;
            }
             if(!Map[sz2])
            {
                Map[sz2] = ++cnt;
            }
            ind[Map[sz2]]++;
        }
         int ans =  0;
         for( int i =  1; i <= cnt; i++)    //入度为0的顶点个数
             if(!ind[i]) ans++;
         if(ans ==  1)
        {
            printf( " Yes\n ");
        }
         else
        {
            printf( " No\n ");
        }
    }
     return  0;
}

 

你可能感兴趣的:(HDU)