Triangle lovetime limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 2586 accepted submission (s): 1051
Problem descriptionrecently, 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.
Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a "triangle love ".
Inputthe 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 ).
Outputfor 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
25001001000001001111011100050111100000010000110001110
Sample output
Case #1: YesCase #2: No
Question: Give the relationship between all of you, and then let you determine whether there is a love triangle or a love affair.
Analysis: we can create a Directed Graph Based on the relationship. If a prefers B, it will point A to B. If there is a love triangle or a love affair, it will definitely form a ring. I choose to sort it by topology, if the ring is formed, all the numbers will not be sorted.
Note: If you use a chained forward star, the edge array must be above 2000*1999, And you have re several times.
Experience: I did not think about the topology during the competition, but I just thought about and checked the set. I still didn't make it in the left.
The query set can determine whether a tree is a ring, and the topology can determine whether a graph is a ring.
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define M 2005struct node{int to, next;}s[20000005];int in[M], head[M], n, tot, queue[M];char map[M][M];void getmap(int a, int b){s[tot].to = b;in[b]++;s[tot].next = head[a];head[a] = tot++;}int toposort(){bool vis[M];memset(vis, false, sizeof(vis));int i, j, iq = 0;for(i = 0; i < n; i ++){if(!in[i]){queue[iq++] = i;vis[i] = 1;}}for(i = 0; i < iq; i++){int temp = head[queue[i]];for(j = temp; j != -1; j = s[j].next){ if(!(--in[s[j].to])){queue[iq++] = s[j].to;}}}if(iq < n) return 1;else return 0;}int main(){int t, v = 1;scanf("%d", &t);while(t --){memset(in, 0, sizeof(in));memset(head, -1, sizeof(head));tot = 0;scanf("%d", &n);int i, j, flag;for(i = 0; i < n; i ++){scanf("%s", map[i]);for(j = 0; j < n; j ++){if(map[i][j] == '1'){getmap(i, j);}}}printf("Case #%d: ", v++);flag = toposort();if(flag) printf("Yes\n");else printf("No\n");}return 0;}
Hdoj 4324 triangle love [topology]