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. MyBatis学习总结(18)——MyBatis与Hibernate区别

    也用了这么久的Hibernate和MyBatis了,一直打算做一个总结,就他们之间的优缺点说说我自己的理解: 首先,Hibernate是一个ORM的持久层框架,它使用对象和我们的数据库建立关系,在Hi ...

  2. Spring 7大功能模块的作用

    1.   Spring 7大功能模块的作用 1)   核心容器(Spring core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系 ...

  3. 洛谷——P2483 [SDOI2010]魔法猪学院

    https://www.luogu.org/problem/show?pid=2483 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的 ...

  4. HTML---经常使用标签总结与实践

    什么是HTML? 超文本标记语言,标准通用标记语言下的一个应用.    "超文本"就是指页面内能够包括图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包含" ...

  5. CURL库的宏定义列表

    列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...

  6. WinCE C#程序,控制启动时仅仅能启动一个程序,使用相互排斥量来实现,该实现方法測试通过

    </pre><pre code_snippet_id="430174" snippet_file_name="blog_20140718_5_46349 ...

  7. Android知识点总结

    说明 当中大部分文章都是转载自其它大神之手.在转载的过程中学到了非常多,这里主要解说的是android体系的相关知识点,本文会持续更新. 1 Android service相关知识点 Android ...

  8. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  9. ThinkPHP5.0框架开发--第10章 TP5.0验证器

    ThinkPHP5.0框架开发--第10章 TP5.0验证器 第10章 TP5.0验证器 ======================================= 今日学习 1.验证器 1) 控 ...

  10. linux下挂载ISCSI存储设备

    安装 首先要在存储设备上做好RAID,设置好iSCSI 目标方(target). 这里主要说明iSCSI initiator的安装. 不同的操作系统对应各自的iSCSI initiator,以Redh ...