hdu 4324 Triangle LOVE

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4324  

Triangle LOVE

Description

Recently, 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 A.
  Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a “Triangle Love”.

Input

The 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).

Output

For 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

2
5
00100
10000
01001
11101
11000
5
01111
00000
01000
01100
01110

Sample Output

Case #1: Yes
Case #2: No

拓扑排序。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 2010;
const int INF = 0x3f3f3f3f;
struct TopSort {
    struct edge { int to, next; }G[N * N];
    int tot, inq[N], head[N];
    inline void init() {
        tot = 0, cls(inq, 0), cls(head, -1);
    }
    inline void add_edge(int u, int v) {
        G[tot].to = v; G[tot].next = head[u]; head[u] = tot++;
    }
    void built(int n) {
        char buf[N];
        rep(i, n) {
            scanf("%s", buf);
            rep(j, n) {
                int f = buf[j] - '0';
                if(!f) continue;
                inq[j + 1]++;
                add_edge(i + 1, j + 1);
            }
        }
    }
    inline bool bfs(int n) {
        int topNum = 0;
        queue<int> q;
        rep(i, n) {
            if(!inq[i + 1]) {
                q.push(i + 1);
            }
        }
        while(!q.empty()) {
            int u = q.front(); q.pop();
            topNum++;
            for(int i = head[u]; ~i; i = G[i].next) {
                if(--inq[G[i].to] == 0) {
                    q.push(G[i].to);
                }
            }
        }
        return topNum == n;
    }
    inline void solve(int n) {
        static int k = 1;
        init(), built(n);
        printf("Case #%d: %s
", k++, !bfs(n) ? "Yes" : "No");
    }
}go;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int t, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        go.solve(n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GadyPu/p/4792954.html