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”.
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).
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
针对一个
顶点活动网(Activity On Vertex network),简称
AOV网,从中去除入度为0的顶点,同时更新从改点出发引起的入度,让这些点的入度减1,直到最后如果AOV网为空时,说明那么去除的这些点就组成了一个拓扑排序,如果AOV网不为空,这种情况若在程序中出现,则称为死锁或死循环,是应该必须避免的,说明这些活动是永远执行不到的。(活动的前驱又是在活动之后执行)
AC代码:


#include <stdio.h> #include <string.h> int t,n; //存储的是节点的入度 int in_degree[2013]; //存储的是i,j两个结点的关系,1:i love j,0:j love i char adj_mat[2013][2013]; 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(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; }