Triangle LOVE
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1387 Accepted Submission(s): 584
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”.
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). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,i(1<=i, j<=n,i≠j).
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). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,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.
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
第一次做拓扑排序的题目,这是一道典型的拓扑排序的题,刚开始时没有理解,上网查了好多关于拓扑排序的知识才明白,就是针对一个
顶点活动网(Activity On Vertex network),简称
AOV网,从中去除入度为0的顶点,同时更新从改点出发引起的入度,让这些点的入度减1,直到最后如果AOV网为空时,说明那么去除的这些点就组成了一个拓扑排序,如果AOV网不为空,这种情况若在程序中出现,则称为死锁或死循环,是应该必须避免的,说明这些活动是永远执行不到的。(活动的前驱又是在活动之后执行)
/** 题意分析(转载): 此题可以一遍拓扑排序判环求解 即只需要找到一个环, 就必定存在三元环 证明如下: 假设存在一个n元环, 因为a->b有边,b->a必定没边,反之也成立 所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边, 就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边, 这样就形成了一个n-1元环。。。。 所以只需证明n大于3时一定有三元环即可,显然成立。 */ #include <stdio.h> #include <string.h> int t,n; //存储的是节点的入度 int in_degree[2010]; //存储的是i,j两个节点的关系,1:i love j,0:j love i char adj_mat[2010][2010]; int main() { bool flag;//true表示为有三角恋,false表示为没有三角恋 scanf("%d",&t); for(int i = 1; i <= t;i++) { scanf("%d",&n); flag = false; //将所有的节点入度初始化为0 memset(in_degree,0,sizeof(in_degree)); for(int j = 0; j < n; j++) { scanf("%s",adj_mat[j]); for(int k=0;k<n;k++) if(adj_mat[j][k]=='1')//如果j喜欢k,则把k的入度加1 in_degree[k]++; } for(int j=0;j<n;j++) { int k; for(k=0;k<n;k++) if(in_degree[k]==0)break;//找出入度为0的节点 if(k==n)//任何一个节点的入度都不为0,说明存在环了,则必有三角恋 { flag = true; break; }else{ //将这个点的入度设为-1,避免再次循环时有查到了这个节点, //此时说明这个点已经从集合中除掉了 in_degree[k]--; for(int p=0;p<n;p++) { //把从这个节点出发的引起的节点的入度都减去1 if(adj_mat[k][p]=='1'&&in_degree[p]!=0) in_degree[p]--; } } } if(flag) printf("Case #%d: Yes\n",i); else printf("Case #%d: No\n",i); } return 0; }
本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/archive/2012/10/16/2726757.html,如需转载请自行联系原作者