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 x,y;
int flag,Case = 0;
void Initial()
{
	flag = 0;
	memset(In,0,sizeof(In));
}
void TopoSort()
{
	int i,j,k;
	for(i = 0;i < N;i++)
	{
	/*
		//这样写也可以过,但是时间会多一点点
		for(j = 0;j < N;j++)
		{
			if(!In[j])
			{
				In[j]--,flag++;
				for(k = 0;k < N;k++)
				{
					if(Map[j][k])
						Map[j][k] = 0,In[k]--;
				}
			}
		}
		*/
		for(j = 0;j < N;j++)
			if(!In[j])//找出第一个入度为0的节点
				break;
		if(j == N)//若没有入度为零的节点,则必成环
			{
				flag = 1;
				break;
			}else{
				In[j]--;//,,, ,,,
				for(k = 0;k < N;k++)
					if(Map[j][k] == '1')
					In[k]--;
				}
	}
	if(flag)printf("Case #%d: Yes\n",++Case);
	else printf("Case #%d: No\n",++Case);
}
int main()
{
	int i,j;
	scanf("%d",&T);
	while(T--)
	{
		Initial();
		scanf("%d",&N);
		for(i = 0;i < N;i++)
		{
			scanf("%s",Map[i]);
			for(j = 0;j < N;j++)
				if(Map[i][j] == '1')//若有字符'1',则代表当前人 i 喜欢'1'字符所代表的人 j
					In[j]++;//入度加一
		}
		TopoSort();
	}
	return 0;
}

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

时间: 2024-10-11 22:44:36

HDU4324 - Triangle LOVE 拓补排序的相关文章

【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 【拓扑排序】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2455    Accepted Submission(s): 997 Problem Description Recently, scientists find that there is love between any of two people. For

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]

HDU4324 Triangle LOVE【拓扑排序】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2683    Accepted Submission(s): 1084 Problem Description Recently, scientists find that there is love between any of two people. Fo

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 <=

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