Triangle Love
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): 2683 Accepted S Ubmission (s): 1084
Problem Description
Recently, 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 ".
Input
The 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).
Output
For each case, output the case number as shown and then print "Yes", if there are a "Triangle love" among these N people, O Therwise 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
The main idea: to give you a picture, the figure randomly between two points have either a positive edge, or have a reverse edge.
Infer whether the triangle ring with A->b->c->a is included.
Idea: In fact, only have the ring, it can form a triangular ring.
Since the random two points have either a positive edge,
There is either a reverse edge. Assuming that there is now a four element ring A->b->c->d->a, if a does not point to C, then
C must point to a, so there must be a triangular ring. Direct topological ordering, assuming it cannot be sorted. Then there are
Triangle ring, Output "Yes", can be topological sort. It does not contain a triangular ring and outputs "no".
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdio > #include <cstring>using namespace std;const int maxn = 2010;int N,m,t;int TOPO[MAXN],G[MAXN][MAXN],VIS[MAXN]; Char map[maxn][maxn];bool dfs (int u) {Vis[u] =-1; for (int v = 0; v < N; v++) {if (G[u][v]) {if (Vis[v] < 0) return false; else if (!vis[v] &&!dfs (v)) return false; }} Vis[u] = 1; TOPO[--T] = u; return true;} BOOL Toposort () {t = N; memset (vis,0,sizeof (VIS)); for (int u = 0; u < N; u++) {if (!vis[u]) if (!dfs (U)) return false; } return true; int main () {int t,kase = 0; Cin >> T; while (t--) {memset (g,0,sizeof (G)); memset (topo,0,sizeof (topo)); GetChar (); Cin >> N; for (int i = 0; i < N; i++) cin >> Map[i]; for (int i = 0; i < N; i++) {for (int j = 0; J < N; J + +) if (map[i][j] = = ' 1 ') g[i][j] = 1; } cout << "Case #" << ++kase << ":"; if (Toposort ()) cout << "No" << Endl; else cout << "Yes" << Endl; } return 0;}
HDU4324 Triangle Love "topological sort"