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. 《你又怎么了我错了行了吧》第九次团队作业:Beta冲刺与验收准备

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十三 团队作业9 团队名称 你又怎么了我错了行了吧 作业学习目标 (1)掌握软件黑盒测试技术: (2)学会编制软件项目总结PPT. ...

  2. 推荐几款常用的Eclipse插件

    Eclipse 应该说是老牌也是最常用的Java开发工具,尽管这几年 InstelliJ IDEA 的发展势头很强劲,身边使用和推崇的人也大有人在,但个人而言还是觉有些不太习惯.这里也介绍几款自己常用 ...

  3. keil5下载程序时出现“internal command error”解决方法

    今天下载的时候,插入下载器,查看Debug可用看到“internal command error”,一直找不到原因,后来查看上一个工程才发现,上一个程序禁用了Seral Wire 引脚,导致的. 解决 ...

  4. Bootstrap组件之页头、缩略图

    .page-header--指定div元素包裹页头组件. <div class="page-header"> <h1>小镇菇凉<small> 2 ...

  5. Oracle字符乱码、数据越界訪问典型Bug分析

    Oracle字符乱码.数据越界訪问典型Bug分析 前言:           作为乙方,在甲方客户那里验收阶段发现两个诡异Bug. 下面就问题来源.问题根因.解决方式.怎样避免做具体描写叙述. .且两 ...

  6. 微软版UnityVs横空出世,究竟是谁成就了谁?

    在移动互联网浪潮持续发力下,手游行业也异常火热.在现在的手游行业,Unity3d无疑是最耀眼的哪颗星.一直觉得Unity面向设计师是友好的,对程序猿这边并非非常友好. 2012年用Unity时开发工具 ...

  7. Apicloud自定义模块

    各种坑,折腾了两天才有点头绪.我用的是Android Studio编辑器,官网是Eclipse的视频.文档也比较蛋疼. 自定义模块的目录结构要按照下面来处理 其中res_模块名,存放res和Andro ...

  8. iframe 高度宽度自适应

    <iframe id="iframeHome" name="iframeHome" src="/Page/NewHome/GongZuoTai. ...

  9. sicily 1137 河床 (二分分治)

    <计算机算法设计与分析>啃书中... 有点看不进书,就来刷个水题吧,刚开始看错题了还. 注意:是所有测量点相差均不大于di而不是相邻两点... //1137.河床 #include < ...

  10. SqlDependency C#代码监听数据库表的变化

    SqlDependency提供了这样一种能力:当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的. ...