hdu 4324 Triangle LOVE(拓扑判环)

Triangle LOVE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 3603    Accepted Submission(s): 1416

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, what a crazy world!

Now, scientists want to know whether or not there is a “Triangle Love” among N people. “Triangle Love” means that among any three people (A,B and C) , A loves B, B loves C and C loves A.

Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a “Triangle Love”.

Input

The first line contains a single integer t (1 <= t <= 15), the number of test cases.

For each case, the first line contains one integer N (0 < N <= 2000).

In the next N lines contain the adjacency matrix A of the relationship (without spaces). Ai,j = 1 means i-th people loves j-th people, otherwise Ai,j = 0.

It is guaranteed that the given relationship is a tournament, that is, Ai,i= 0, Ai,j ≠ Aj,i(1<=i, j<=n,i≠j).

Output

For each case, output the case number as shown and then print “Yes”, if there is a “Triangle Love” among these N people, otherwise print “No”.

Take the sample output for more details.

Sample Input

2
5
00100
10000
01001
11101
11000
5
01111
00000
01000
01100
01110

Sample Output

Case #1: Yes

Case #2: No

输入一个矩阵,如果a[i][j]==1,意思是i喜欢j,问存不存在一个三角恋关系,
	既拓扑排序判环,,要深入理解拓扑排序的原理
	2015,8,14
#include<stdio.h>
#include<string.h>
#define M 2100
char mp[M][M];
int du[M];
int n,cas;
void topo(){
	int ok=0,i,k;
	for(int i=1;i<=n;i++){
		for(k=1;k<=n;k++)
			if(du[k]==0) //z找到入度为0的点
				break;
		if(k==n+1){//如果不存在入度为0的点,那么就是一定存在环 ,就有三角恋
			ok=1;
			break;
		}else{
			du[k]--;//删除这个点
			for(int j=1;j<=n;j++){
				if(mp[k][j]=='1'&&du[j]!=0)
					du[j]--;
			}
		}
	}
	if(ok) printf("Case #%d: Yes\n",cas++);
	else printf("Case #%d: No\n",cas++);
}
int main(){
	int i,j,t;
	cas=1;
	scanf("%d",&t);
	while(t--){
		memset(du,0,sizeof(du));
		scanf("%d",&n);
		for(i=1;i<=n;i++){//按照注释的输入会超时
			//getchar();
			scanf("%s",mp[i]+1);;
			for(j=1;j<=n;j++){
			//	scanf("%c",&mp[i][j]);
				if(mp[i][j]=='1')
					du[j]++;
			}
		}
		topo();
	}
	return 0;
} 

时间: 2024-12-21 00:39:18

hdu 4324 Triangle LOVE(拓扑判环)的相关文章

HDU 4324:Triangle LOVE( 拓扑排序 )

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

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 4324 Triangle LOVE(拓扑排序)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3858    Accepted Submission(s): 1516 Problem Description Recently, scientists f

hdu 4324 Triangle LOVE

本题链接:点击打开链接 本题大意: 题意分析(转载):此题可以一遍拓扑排序判环求解 即只需要找到一个环,就必定存在三元环 证明如下: 假设存在一个n元环,因为a->b有边,b->a必定没边,反之也成立所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,这样就形成了一个n-1元环....所以只需证明n大于3时一定有三元环即可,显然成立. 具体请参见代码: #include<std

SDNU 1089.拓扑排序(拓扑判环小顶堆)

Description 给定一个有向图,若图无环,则将其进行拓扑排序并输出,否则输出IMPOSABLE. Input 第一行为两个整数n(1<=n<=1000).m(1<=m<=100000): 之后m行,每行两个整数a.b(0 < a, b <= n)表示一条从a到b的有向边. Output 若存在环,输出IMPOSABLE,否则输出一行用一个空格隔开的拓扑排序的结果,若存在多个结果,输出字典序最小的. Sample Input 5 4 1 2 2 3 3 4 4 5

HDU 4324 Triangle LOVE(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 题目: 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 possi

HDU 4324 Triangle LOVE【拓扑排序】

题意:给出n个人,如果a喜欢b,那么b一定不喜欢a,如果b不喜欢a,那么a一定喜欢b 就是这n个点里面的任意两点都存在一条单向的边, 所以如果这n个点不能构成拓扑序列的话,就一定成环了,成环的话就一定能够找到一个三元环 所以只需要判断能不能构成拓扑序列 另外,tle了一晚上是因为用了cin------55555555555555 以后少用cin了----- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring&g

牛客练习赛11 B trie树+拓扑判环 E 分治求平面最近点对

牛客练习赛11 B  假的字符串题意:给定n个字符串,互不相等,你可以任意指定字符之间的大小关系(即重定义字典序),求有多少个串可能成为字典序最小的串,并输出它们. tags:好题 对于一个字符串, 1]如有其它字符串是它的前缀,那肯定不可能.这个直接用字典树处理就可以. 2]但如果以这个字符串为最小,怎么判定其它字符串不会矛盾呢? 其实矛盾的情况详细一点说是: 比如要以  abcd 为最小, 但又有另一个字符串 aba ,这就矛盾了. 对这种情况,在跑字典树的时候,我们对有相同父亲结点的多个儿

【建图+拓扑判环】BZOJ3953: [WF2013]Self-Assembly

Description 自动化学制造(Automatic Chemical Manufacturing,简称ACM)正在对一个叫自组装(self-assembly)的过程进行实验.在这个过程中,有着天然相互吸引力的分子被混合在溶液中,任由它们聚集组合成更大的结构.但是有一个问题随之出现:有时候,分子们会把自身组合成一个无限大的结构体,以至于把容器撑爆. 你需要写一个程序来判断一个给定的分子集合是否可能组合成一个无限大的结构体.为了使问题简化,你可以作以下两个假设: 1. 问题被限制在二维平面上.