HDU5154 - Harry and Magical Computer 拓补排序

HDU5154 - Harry and Magical Computer : http://acm.hdu.edu.cn/showproblem.php?pid=5154

还是拓补排序,直接上代码了.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 105;
int Map[MAXN][MAXN],In[MAXN],flag;
int N,M;
int x,y;
void Initial()
{
	flag = 0;
	memset(Map,0,sizeof(Map));
	memset(In,0,sizeof(In));
}
void TopoSort()
{
	int x;
	queue<int>ans;
	for(int i = 1;i <= N;i++)
		if(!In[i])ans.push(i);//入度为零的点进入队列
	while(!ans.empty())
	{
		x = ans.front();
		ans.pop();
		for(int i = 1;i <= N;i++)
		{
			if(Map[x][i])
			{
//				In[i]--,Map[x][i] = 0;
				In[i] -= Map[x][i],Map[x][i] = 0;
				if(!In[i])ans.push(i);
			}
		}
	}
	for(int i = 1;i <= N;i++)
	{
		if(In[i])//还存在入度大于零的点
		{
			flag = 1;
			break;
		}
	}
	if(flag)printf("NO\n");
	else printf("YES\n");
}
int main()
{
	while(~scanf("%d%d",&N,&M))
	{
		Initial();
		while(M--)
		{
			scanf("%d%d",&x,&y);
			In[x]++;//注意是x的入度加一
		    Map[y][x]++;
//			Map[y][x] = 1;
		}
		TopoSort();
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 20:35:01

HDU5154 - Harry and Magical Computer 拓补排序的相关文章

HDU5154 Harry and Magical Computer【拓扑排序】

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 73    Accepted Submission(s): 34 Problem Description In reward of being yearly outstanding magic student, Harry gets a

【BZOJ3036】绿豆蛙的归宿 拓补排序+概率

[BZOJ3036]绿豆蛙的归宿 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K .现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少? Input 第一行: 两个整数 N M,代表图中有N个点.M条边第二行到第 1+M 行: 每

【BZOJ2815】[ZJOI2012]灾难 拓补排序+LCA

[BZOJ2815][ZJOI2012]灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生物的阿米巴告诉小强,草原是一个极其稳定的生态系统.如果蚂蚱灭绝了,小鸟照样可以吃别的虫子,所以一个物种的灭绝并不一定会引发重大的灾难. 我们现在从专业一点的角度来看这个问题.我们用一种叫做食物网的有向图来描述生物之间的关系: 一个食物网有N个点,代表N种生物,如果生物

HDU4324 - Triangle LOVE 拓补排序

HDU4324 - Triangle LOVE : http://acm.showproblemhdu.edu.cn/.php?pid=4324 标准的拓补排序,上代码 : #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 2010; int In[MAXN]; char Map[MAXN][MAXN]; int T,N; int

poj 3687 Labeling Balls(拓补排序)

Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled with a is lighter

poj 1094 Sorting It All Out 拓补排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

拓补排序

让领导先走 Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 完啦完啦,公司里发火灾拉,大家快跑啊,再不跑就没命啦.大家不要乱,请按顺序通过消防通道,说到顺序,那么问题来了. 按照中国特色社会主义文化,我们严格贯彻落实一件事,那就是,让领导先走. 现在又n人,从1标号到n.如果a是b的领导的话,就必须让a排在b的前面. 那么你就要安排大家的顺序.我保证一定有解. 输入 多组输入,然后对于每个测试数据,第一行有两个整数n(1 <=

hdu5154--Harry and Magical Computer(拓扑排序)

Harry and Magical Computer Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2015-01-06) Description In reward of being yearly outstanding magic student, Harry gets a mag

hdu 5154 Harry and Magical Computer 拓扑排序

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal wi