HDU - 4324 Triangle LOVE(拓扑排序 + 判断环路)
2015-08-19 20:01
441 查看
题意:人类间的凄美爱情,若a爱b,则b一定不爱a, 求是否存在三角恋,判断是否存在环路即可。
解题思路:
直接链式向前星存边,手工队列拓扑排序,判断环路即可。
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). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,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
Sample Output
解题思路:
直接链式向前星存边,手工队列拓扑排序,判断环路即可。
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). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,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<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<list> #include<iostream> #include<map> #include<queue> #include<set> #include<stack> #include<vector> using namespace std; int buf[10]; //整型变量快速输入输出函数 inline int readint() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x; } inline void writeint(int i) { int p = 0; if(i == 0) p++; else while(i) { buf[p++] = i % 10; i /= 10; } for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]); } ////////////////////////////////////////////////////////////////////// #define MAX_N 2005 const int INF = 0x3f3f3f3f; int n; char m[MAX_N][MAX_N]; int indegree[MAX_N]; int head[MAX_N]; int top; struct node { int to, next; }edge[MAX_N * MAX_N]; void init() { memset(head, -1, sizeof(head)); memset(indegree, 0, sizeof(indegree)); top = 0; } void add_edge(int u, int v) { edge[top].to = v; edge[top].next = head[u]; head[u] = top++; } int topo_sort() { int iq = 0; int que[MAX_N]; for(int i = 0 ; i < n ; i++) { if(indegree[i] == 0) { que[iq++] = i; } } for(int i = 0 ; i < iq ; i++) { for(int k = head[que[i]] ; k != -1; k = edge[k].next) { indegree[edge[k].to]--; if(indegree[edge[k].to] == 0) { que[iq++] = edge[k].to; } } } if(iq < n - 1) return 1; return 0; } int main() { int t; t = readint(); int cas = 1; while(t--) { init(); n = readint(); for(int i = 0 ; i < n ; i++) { scanf("%s", &m[i]); for(int j = 0 ; j < n ; j++) { if(m[i][j] == '1') { add_edge(i, j); indegree[j]++; } } int ans = topo_sort(); if(ans) printf("Case #%d: Yes\n", cas++); else printf("Case #%d: No\n", cas++); } return 0; }
相关文章推荐
- Android 将.sql格式文件转换为.db格式数据库文件
- jquery的几个重要的对象关系逻辑
- 多线程并发问题(二)
- php unset函数执行
- Android基础布局+Activity启动+Intent
- JVM的相关知识整理和学习
- poj1159(dp)
- Best Time to Buy and Sell Stock II
- JAVA 多线程编程 --线程的调度策略
- HDU 5304(Eastest Magical Day Seep Group's Summer-环加外向树生成树计数)[Template:Kirchhoff矩阵]
- 循环队列查找问题
- 刀哥之指纹识别biometrics
- unity中的[xxxxxx]特性(Attributes)
- unity中的[xxxxxx]特性(Attributes)
- 路漫漫其修远兮,吾要上下左右前后而求索
- 2015暑假训练赛个人赛(8.19)
- mysqlconn-ocilib
- cocos2d lua绑定感悟---像cc.Sprite,cc.Director这些是如何识别的
- poj 1985 树状数组+并查集(第k大组数)
- Hello,World!