您的位置:首页 > 其它

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

2017-09-29 21:46 197 查看
题目: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: