Triangle Love
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 3566 Accepted Submission (s): 1395
Problem descriptionrecently, scientists find that there was love between any of the people. For example, between A and B, if a don ' t love B, then B must love A, vice versa. And there is the no possibility that the people love each other, what a crazy world!
Now, scientists want to know whether or not there are a "Triangle love" among N people. "Triangle Love" means this among any three people (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 "T Riangle Love ".
Inputthe first line contains a single integer t (1 <= t <=), 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 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, which 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 PEO PLE, otherwise print "No".
Take the sample output for more details.
Sample Input25001001000001001111011100050111100000010000110001110
Sample outputcase #1: yescase #2:no Test instructions: Gives a matrix s[][] equals 1 for I like J, (but the title guarantees that there cannot be two people in love with each other) now ask whether there is a triangle in the relationship between these people Love (a love b,b love C,c a) if there is output yes otherwise output no problem: Because the topic has been guaranteed that there will not be two people love each other, so that the ring is the least three ring, Direct topological sequencing to determine if a loop is possible (to do with the adjacency table, the matrix will be super )
#include <stdio.h> #include <string.h> #include <vector> #include <queue> #define MAX 2010using namespace Std;int N,k;vector<int>map[max];char s[max];int vis[max];void getmap () {int i,j;for (i=1;i<=n;i++) map[i].clear (); for (i=1;i<=n;i++) {scanf ("%s", s); for (j=0;j<n;j++) { if (s[j]== ' 1 ') { map[i].push_back (j+1); vis[j+1]++;}}}} void Tuopu () {int i,j;queue<int>q;while (!q.empty ()) Q.pop (); for (i=1;i<=n;i++) if (vis[i]==0) Q.push (i); int u,v;int ans=0;while (!q.empty ()) {U=q.front (); Ans++;q.pop (); for (I=0;i<map[u].size (); i++) { v= Map[u][i]; vis[v]--; if (vis[v]==0) Q.push (v);}} printf ("Case #%d:", k++), if (ans!=n) printf ("yes\n"); elseprintf ("no\n");} int main () {int t,i,j;k=1;scanf ("%d", &t), while (t--) {scanf ("%d", &n), memset (vis,0,sizeof (VIS) ); Getmap (); Tuopu ();} return 0;}
Hdoj 4324 Triangle Love "topological sequencing to determine if there is a ring"