hdu 2094 谁是冠军(STL,拓扑排序)

来源:互联网 发布:淘宝双十一c店报名条件 编辑:程序博客网 时间:2024/05/16 07:31

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2094

这题简直太水了,开始觉得拓扑,后来写着写着发现大部分代码没用,就删成这样了。只要入度唯一即可输出yes

stl是真好用,特别是auto定义迭代器遍历map,set等。

但是这题确实写错了,光写flag没判断入度均不为0时,应该是No,而之前写的忘判断flag==0时size是不是0;

#include<cstdio>#include<iostream>#include<queue>#include<string>#include<cstring>#include<map>using namespace std;const int maxn=2005;int n;string x[maxn],y[maxn];map<string,int> in;void toposort(){    queue<string> s;    int flag=0;    for(auto it:in){        //cout<<it.second<<endl;        if(it.second==0)      //收入入度为0的点        s.push(it.first);        if(s.size()>1)            flag=1;    }    if(flag==1||s.size()==0) printf("No\n");    else  printf("Yes\n");}int main(){    while(scanf("%d",&n)&&n){        in.clear();        for(int i=0;i<n;i++){            cin>>x[i]>>y[i];            in[y[i]]=0;            in[x[i]]=0;        }        for(int i=0;i<n;i++){            in[y[i]]++;        }        toposort();        for(int i=0;i<n;i++){            x[i].clear();            y[i].clear();        }    }    return 0;}


原创粉丝点击