Triangle LOVE


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2683    Accepted Submission(s): 1084

Problem 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

题目大意:给你一个图,图中随意两点之间要么有正向边,要么有反向边。

推断是否含有a->b->c->a的三角形环。

思路:事实上仅仅要有环,就能构成三角形环。

由于随意两点之间要么有正向边,

要么有反向边。假设如今有一个四元素环 a->b->c->d->a,若a不指向c,则

c必然指向a,所以必然存在三角形环。直接拓扑排序,假设不能排序。则有

三角环,输出“Yes”,能拓扑排序。则不含有三角环,输出"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【拓扑排序】的更多相关文章

  1. Triangle LOVE(拓扑排序)

    Triangle LOVE Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total ...

  2. HDU 4324 Triangle LOVE 拓扑排序

    Problem Description Recently, scientists find that there is love between any of two people. For exam ...

  3. hdu4324 Triangle LOVE (拓扑排序)

    这是一道最简单的拓扑排序题,好久没看这个算法了! 有点生疏了! 后附上百度的资料; #include<stdio.h> #include<string.h> int in[50 ...

  4. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. HDU - 4324 Triangle LOVE(拓扑排序)

    https://vjudge.net/problem/HDU-4324 题意 每组数据一个n表示n个人,接下n*n的矩阵表示这些人之间的关系,输入一定满足若A不喜欢B则B一定喜欢A,且不会出现A和B相 ...

  6. HDU 4324 Triangle LOVE (拓扑排序)

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. HDU 4324 (拓扑排序) Triangle LOVE

    因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...

  8. hdu 4324 Triangle LOVE(拓扑排序,基础)

    题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...

  9. hdu4324 拓扑排序

    #include<cstdio> #include<string.h> #define maxn 2013 char M[maxn][maxn]; int du[maxn]={ ...

随机推荐

  1. vue 过滤器使用的传参说明

    在table中,需要对obj的数据类型进行文字转换,例如后台接口返回的姓别值:1,2.其中需要页面根据字典需要把1=>男,2=>女进行转换. 以前的习惯是每一个过滤方法都写一个方法进行转换 ...

  2. array_combine php一个比较偏门的数组函数

    这函数  返回数组1的值 当做key,把数组2的值当做value,   当查询数据库用了 group +GROUP_CONCAT 两个组合时,(例如查询某个班级的,用户名,用户id,返回的是字符串,打 ...

  3. 使用 Xshell 连接 linux 系统

    一.下载 Xshell 链接:https://pan.baidu.com/s/1htwqpzm 密码:zau7 二.安装 Xshell 无脑下一步就可以了 三.连接 linux 四.安装 Xftp h ...

  4. java整型byte,short,int,long取值范围大小

     byte 1个字节 short 2个字节 int 4个字节long 8 个字节 varchar 可变长度的非Unicode数据,最长为8000个字符nvarchar 可变长度Unicode数据,最长 ...

  5. ASP.NET-Active Direcotry编程示例

    查找指定的AD帐号 using (DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE")) { string DCName ...

  6. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  7. 晋IT分享成长沙龙集锦

    第一期"晋IT"分享成长沙龙于2014年7月19日圆满结束.下面是相关内容整理和第二期预告. 各位伙伴认真的介绍自己,介绍自己的业务,分析自己眼下存在的问题,大家一起探讨,真诚出谋 ...

  8. MyEclipse打包可运行的jar包

    详细步骤: Export... -> java -> Runnable JAR file Launch configuration:选择main方法所在的文件/类 Export desti ...

  9. Android上方便地开发的C程序

    假设你基于没有一个专门的开发板练手.那你的Android手机也能够开发大多数C应用程序.安装好后编译C的编译器. 本文仅仅写一个Hello World的执行过程. 长处是:不须要eclipse,不须要 ...

  10. SSH Key的生成和使用(for git)

    SSH Key的生成和使用 一.总结 1.用git base生成ssh,会生成id_rsa.pub文件,还有一个私钥文件.     $ ssh-keygen -t rsa -C “youremailn ...