HDU3342-Legal or Not
2015-11-20 21:41
127 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342
思想:n个点,所以删除n次之后,在判断是否存在入度大于0的点即可。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
bool Map
;
bool vis
;
struct node{
int in,out;
}s
;
int n,m,a,b;
int TopSort()
{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(s[j].in==0){
s[j].in=-1;
for(int k=0;k<n;k++){
if(Map[j][k])
--s[k].in;
}
break;
}
}
}
for(int i=0;i<n;i++)
if(s[i].in>0) return 0;
return 1;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
if(!n) break;
memset(Map,false,sizeof(Map));
memset(s,false,sizeof(s));
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(!Map[a][b]){
s[a].out++;
s[b].in++;
Map[a][b]=true;
}
}
if(TopSort()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
思想:n个点,所以删除n次之后,在判断是否存在入度大于0的点即可。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
bool Map
;
bool vis
;
struct node{
int in,out;
}s
;
int n,m,a,b;
int TopSort()
{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(s[j].in==0){
s[j].in=-1;
for(int k=0;k<n;k++){
if(Map[j][k])
--s[k].in;
}
break;
}
}
}
for(int i=0;i<n;i++)
if(s[i].in>0) return 0;
return 1;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
if(!n) break;
memset(Map,false,sizeof(Map));
memset(s,false,sizeof(s));
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(!Map[a][b]){
s[a].out++;
s[b].in++;
Map[a][b]=true;
}
}
if(TopSort()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- HDU 3342
- 2367:Genealogical tree
- hdu 1285 确定比赛名次 (简单的拓扑排序)
- hdu 2049 产生冠军(拓扑排序)
- [BZOJ2330][SCOI2011][拓扑排序][强连通分量][Tarjan]Candy
- Course Schedule
- LeetCode Course Schedule II
- 【Leetcode】Course Schedule II #210
- poj1094 拓扑排序
- (转)拓扑排序
- [leetcode]Course Schedule
- poj_1094
- cf_290(div2)A,B,C
- 图论算法(1)--- 如何求每一个节点在DAG中的最大深度
- hdu 3342 拓扑排序
- hdu 2647 拓扑排序
- UVa 1572 拓扑排序 判断有向图是否有环
- UVa10305拓扑排序入门题 可做模板用