HDU 3342 Legal or Not【拓扑排序】

题意:给出n,m,人的编号为 0到n-1,再给出m个关系,问能不能够进行拓扑排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int indegree[1005],queue[1005],head[1005];
struct Edge
{
	int to;
	int next;
} edge[1005];
int main()
{
	int n,m,i,j,k,iq,a,b;
	while(scanf("%d %d",&n,&m)!=EOF&&n&&m)
	{
		memset(head,-1,sizeof(head));
		memset(indegree,0,sizeof(indegree));
		for(i=1;i<=m;i++)
		{
			scanf("%d %d",&a,&b);
			edge[i].to=b;
			edge[i].next=head[a];
			head[a]=i;
			indegree[b]++;
		}
		iq=0;
		for(i=0;i<n;i++)
		{
			if(indegree[i]==0)
			{
				queue[iq++]=i;
			}
		}

		for(i=0;i<iq;i++)
		{
			for(k=head[queue[i]];k!=-1;k=edge[k].next)
			{
				indegree[edge[k].to]--;
				if(indegree[edge[k].to]==0)
				{
					queue[iq++]=edge[k].to;
				}
			}
		}
		if(iq==n) printf("YES\n");
		else
		printf("NO\n");
	}
}

  

时间: 2024-10-08 11:18:25

HDU 3342 Legal or Not【拓扑排序】的相关文章

hdu 3342 Legal or Not - 拓扑排序

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has

hdu 3342 Legal or Not 拓扑排序判断环。 现在的我,除了刷水题,,还能干什么

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4960    Accepted Submission(s): 2270 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

HDU 1258 确定比赛名次 &amp;&amp;HDU 3342 Legal or Not 【临接表+拓扑排序】

HDU 1258 链接:click here HDU 3342 链接:click here 题意: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14142    Accepted Submission(s): 5667 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,

[ACM] hdu 3342 Legal or Not (拓扑排序)

Legal or Not Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to excha

HDU 3213 Box Relations(拓扑排序构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题意:有n个长方体,四种限制条件.(1)I x y x和y有相交:(2)X/Y/Z  x y x的最大X/Y/Z坐标小于y的最大X/Y/Z.构造出这样的n个长方体. 思路:首先,XYZ三个方向是可以分开考 虑的.那么我们可以一个个分别求解.将每个长方体拆成左上角右下角两个点,我们假设现在考虑X方向,也即是一个长方体对应两个X方向的点,共2*n个点, 边<i,j>表示i小于j,那么首先有边&l

HDU 2647 Reward(图论-拓扑排序)

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,a

HDU 2467 Reward(逆拓扑排序)

拓扑排序的变形,逆序建图就好了 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3951    Accepted Submission(s): 1203 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival

HDU 4324 Triangle LOVE (拓扑排序)

Triangle LOVE Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility that two people love each other, wh

HDU 3342 Legal or Not(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目: Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lc

HDU 3342 Legal or Not(拓扑排序判断成环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即可. 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int N=1e2+5; 6 7 int n,m; 8 int vis[N];