Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4324
Topic:
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 problem: The topic gives a does not love B, Then B must love a. Judging there is no triangular relationship, that is, a love b,b love C,c love A. In fact, as long as the judgment has no ring on it (because as long as there is a ring, and A and B must have a relationship, then you will be able to find the triangle ring). Note This problem with scanf one character input will time out ...
1#include <stack>2#include <queue>3#include <cstdio>4#include <cstring>5#include <iostream>6 #defineFast_io Ios::sync_with_stdio (False)7 using namespacestd;8 9 Const intn= -+Ten;Ten intt,n,cnt; One int inch[N]; A CharS[n][n]; -Vector <int>E[n]; -Priority_queue <int>Q; the - voidToposort () { - for(intI=1; i<=n;i++)if(!inch[i]) q.push (i); - while(!Q.empty ()) { + intu=Q.top (); Q.pop (); -cnt++; + for(intj=0; J<e[u].size (); j + +){ A intv=E[u][j]; at inch[v]--; - if(inch[v]==0) Q.push (v); - } - } - } - in intMain () { - Fast_io; toCin>>T; + for(intk=1; k<=t;k++){ -Cnt=0; theMemsetinch,0,sizeof(inch)); * for(intI=0; i<n;i++) e[i].clear (); $Cin>>N;Panax Notoginseng for(intI=1; i<=n;i++){ - e[i].clear (); the for(intj=1; j<=n;j++){ +Cin>>S[i][j]; A if(s[i][j]=='1'){ the E[i].push_back (j); + inch[j]++; - } $ } $ } - Toposort (); - the if(cnt==n) cout<<"Case #"<<k<<": No"<<Endl; - Elsecout<<"Case #"<<k<<": Yes"<<Endl;Wuyi } the return 0; -}
HDU 4324 Triangle Love (topological sort ring)