HDU 4324--Triangle Love "topology sort && adjacency Table implementation"

Source: Internet
Author: User

Triangle LoveTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 3496 Accepted Submission (s): 1357


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 Input
25001001000001001111011100050111100000010000110001110

Sample Output
Case #1: yescase #2: No

Main topic:
T Group test data, each group of data a n represents n individuals, and then the matrix of N*n to represent the relationship between these people, input must meet if a does not like B, B must like a, and will not appear a and b mutually like the situation, ask you whether there is a triangle love.

Problem Solving Ideas:
After topological sorting, it is necessary to determine whether there is a ring, and there is a triangle love.


#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 2010using namespace Std;char map[maxn];int indu[maxn];int HEAD[MAXN], cnt;int N, k;struct node {int u, V, next;};    Node EDGE[MAXN * maxn];void init () {cnt = 0;    Memset (Head,-1, sizeof (head)); memset (indu, 0, sizeof (INDU));}    void Add (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;}    void input () {scanf ("%d", &n);        for (int i = 0; i < n; ++i) {scanf ("%s", map);                for (int j = 0; J < N; ++j) if (map[j] = = ' 1 ') {Add (I, j);            indu[j]++;    }}}void Topsort () {printf ("Case #%d:", ++k);    Queue<int >q;    int ans = 0;            for (int i = 0; i < n; ++i) {if (!indu[i]) {Q.push (i);        ans++;        }} while (!q.empty ()) {int u = q.front ();        Q.pop ();     for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;       indu[v]--;                if (!indu[v]) {Q.push (v);            ans++;    }}} if (n = = ans) printf ("no\n"); else printf ("yes\n");}    int main () {int T;    scanf ("%d", &t);    k = 0;        while (t--) {init ();        Input ();    Topsort (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4324--Triangle Love "topology sort && adjacency Table implementation"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.