杭电ACM2094——产生冠军~~拓扑排序
来源:互联网 发布:linux 重启命令卡住 编辑:程序博客网 时间:2024/06/15 09:47
题目的意思,如题。很容易明白。
解决的方法就是拓扑排序,就可以很容易的解决了。
每输入一对选手,判断两个选手是否出现过,没有出现过,新建一个头结点,加入到邻接表中,更新结点的入度。
最后判断是否存在一个结点的入度为0,有,则Yes,否则No。
我用的是STL中的list容器来创建的邻接表。
下面的是 AC的代码:
#include <iostream>#include <list>#include <cstring>using namespace std;class data //结点的结构体{public:int indegree;int point;char str[100];};list <data> List[2000]; //链表数组,存在多个人int main(){int n, i, j, k;char s1[100], s2[100];data temp;while(cin >> n && n){for(i = 0; i < 2000; i++) //清空各个链表List[i].clear();k = 0;for(i = 0; i < n; i++){cin >> s1 >> s2;int flag = 0, tag = 0, a, b;a = b = -1;for(j = 0; j < k; j++){if(strcmp(List[j].front().str, s1) == 0) //判断第一个人是否出现过{a = j; //标记在链表数组的下标flag = 1;}if(strcmp(List[j].front().str, s2) == 0) //判断第二个人是否出现过{b = j; //同上tag = 1;;}}if(!flag) //没存在,新建一个,扩大数组的大小{strcpy(temp.str, s1);temp.indegree = 0;temp.point = -1;List[k].insert(List[k].end(), temp);k++;}if(!tag) //同上{strcpy(temp.str, s2);temp.indegree = 0;temp.point = -1;List[k].insert(List[k].end(), temp);k++;}if(flag && !tag) //第一个出现过,第二个没有{List[k - 1].front().indegree++; //更新入度strcpy(temp.str, s2);temp.indegree = -1;temp.point = k - 1;List[a].insert(List[a].end(), temp);}else if(!flag && tag) //第一个没出现,第二个出现过{List[b].front().indegree++;strcpy(temp.str, s2);temp.indegree = -1;temp.point = b;List[k - 1].insert(List[k - 1].end(), temp);}else if(flag && tag) //都出现过{List[b].front().indegree++;strcpy(temp.str, s2);temp.indegree = -1;temp.point = b;List[a].insert(List[a].end(), temp);}else if(!flag && !tag) //都没出现过{List[k - 1].front().indegree++;strcpy(temp.str, s2);temp.indegree = -1;temp.point = k - 1;List[k - 2].insert(List[k - 2].end(), temp);}}int ans = 0;for(i = 0; i < k; i++){if(List[i].front().indegree == 0 && !List[i].empty())ans++;}if(ans == 1)cout << "Yes" << endl;elsecout << "No" << endl;}return 0;}
1 0
- 杭电ACM2094——产生冠军~~拓扑排序
- 产生冠军(杭电ACM2094)
- 杭电acm2094产生冠军(2)
- 杭电 2094 产生冠军 【拓扑排序】
- 杭电 2094 产生冠军(拓扑排序)
- 产生冠军——拓扑排序
- 产生冠军 (拓扑排序)
- 拓扑排序-产生冠军
- 【拓扑排序】hdu2094 产生冠军
- hdu2094 产生冠军 拓扑排序
- 产生冠军(hdu2094拓扑排序)
- hdu2094产生冠军--拓扑排序
- HDU 产生冠军(拓扑排序)
- 产生冠军(拓扑排序)
- HDU2094 产生冠军 拓扑排序
- HDOj 2094 产生冠军(拓扑排序)
- hdu 2049 产生冠军(拓扑排序)
- HDU-2094产生冠军-拓扑排序
- freemarker layout 模板使用
- 2015-07-23日总结
- 数位DP小结
- 欢迎使用CSDN-markdown编辑器
- html学习
- 杭电ACM2094——产生冠军~~拓扑排序
- Axure入门设计——倒计时设计
- mac 上安装IDEA14 注意事项
- java 实现IP访问量控制
- 今天很高兴!找到了组织,找到了师父!正式加入beetlsql开发,发个博客庆祝下。
- ch2 一切都是对象
- 单机配置tomcat 8 集群
- LeetCode - Best Time to Buy and Sell Stock IV
- 目标检测程序开发(四)——目标检测