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\n", 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;
}
hdu 4324 Triangle LOVE的更多相关文章
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- HDU 4324 Triangle LOVE (拓扑排序)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 4324 Triangle LOVE(拓扑判环)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 4324 Triangle LOVE(拓扑排序,基础)
题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...
- HDU - 4324 Triangle LOVE(拓扑排序)
https://vjudge.net/problem/HDU-4324 题意 每组数据一个n表示n个人,接下n*n的矩阵表示这些人之间的关系,输入一定满足若A不喜欢B则B一定喜欢A,且不会出现A和B相 ...
- HDU 4324 Triangle LOVE【拓扑排序】
题意:给出n个人,如果a喜欢b,那么b一定不喜欢a,如果b不喜欢a,那么a一定喜欢b 就是这n个点里面的任意两点都存在一条单向的边, 所以如果这n个点不能构成拓扑序列的话,就一定成环了,成环的话就一定 ...
- HDU 4324 (拓扑排序) Triangle LOVE
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- HDU 5914 Triangle 数学找规律
Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...
- HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
随机推荐
- 25_android下文件访问的权限
写文件:FileOutputStream fos = 上下文.openFileOutput("private.txt",Context.MODE_PRIVATR);参数1 文件名, ...
- Jsp,EL表达式的入门
Jsp,EL表达式的入门 *Servlet/JSP 是两种动态的WEB资源的两种技术 使用Servlet生成HTML的页面是可以的 response.getWriter("<form ...
- sqlserver on linux
学习SQL Server从在Linux上安装开始 原创 2016-11-18 衡子 衡子hengzi 微软已经发布了SQL Server on Linux,目前支持Redhat和Ubuntu两种发行版 ...
- ibatis 改下数据库连接
<!--<dataSource name="iBatisNet" connectionString="data source= DESKTO ...
- sorcketlog
正在运行的API有bug,不能var_dump进行调试,因为会影响client的调用. 将日志写到文件,查看也不方便,特别是带调用栈或大数据结构的文件日志,查看日志十分困难. 这时候用SocketLo ...
- IE样式兼容写法
1.第一种写法 利用<!--[if lt IE 6/7/8/9/10/11]><![endif]-->,给每个html写一个class <!DOCTYPE html> ...
- git gc
git gc 有时候当你运行一些git命令(比如git pull)的时候,会有如下提示: 1 2 Auto packing the repository for optimum performance ...
- 一步一图:从SQLSERVER2005中导出insert语句
1.为什么要导出insert语句,我电脑装的是SQL Server Express免费版的,服务器上装的是正式版,在服务器上备份的数据库文件在本机上还原的时候 因为版本不一样,总是不成功.如果能直接使 ...
- 502 bad gateway 错误
在安装好使用过程中出现502问题,一般是因为默认php-cgi进程是5个,可能因为phpcgi进程不够用而造成502,需要修改/usr/local/php/etc/php-fpm.conf 将其中的m ...
- CentOS6.4安装Smokeping节点监控软件
Smokeping:它是rrdtool的作者制作的,在图形显示方面很漂亮,可以用来很好的检测网络状态和稳定性,下面简单说一下Smokeping的安装以及配置方法. 0.首先关闭selinux和防火墙 ...