HDU4324 Triangle LOVE【拓扑排序】
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【拓扑排序】的更多相关文章
- Triangle LOVE(拓扑排序)
Triangle LOVE Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total ...
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- hdu4324 Triangle LOVE (拓扑排序)
这是一道最简单的拓扑排序题,好久没看这个算法了! 有点生疏了! 后附上百度的资料; #include<stdio.h> #include<string.h> int in[50 ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU - 4324 Triangle LOVE(拓扑排序)
https://vjudge.net/problem/HDU-4324 题意 每组数据一个n表示n个人,接下n*n的矩阵表示这些人之间的关系,输入一定满足若A不喜欢B则B一定喜欢A,且不会出现A和B相 ...
- 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
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- hdu 4324 Triangle LOVE(拓扑排序,基础)
题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...
- hdu4324 拓扑排序
#include<cstdio> #include<string.h> #define maxn 2013 char M[maxn][maxn]; int du[maxn]={ ...
随机推荐
- 洛谷 P1582 倒水 (二进制)
这道题实际上是考二进制 很容易看出杯子水量一定是2的i次方 所以n杯水最后剩下的水一定是n用二进制表示中1的个数 所以就枚举n来求什么时候1的个数小于k 那么这里有个优化,不然会超时 因为每次加的目的 ...
- js实现导航固定定位
js实现导航固定定位 <!DOCTY ...
- 基本配置及安全级别security-level
interface GigabitEthernet0/0 nameif outside //指定接口名称 security-level 0 //安全级别设置 ip address 1.1.1.2 ...
- HDU 3073 Saving Beans
Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
- Mysql优化之优化工具profiling
程序员的成长之路 2016-11-23 22:42 Mysql优化之优化工具profiling 前言 mysql优化技术: mysql优化不是做一个操作就可以的优化,它包含很多的细节,需要一点一点的优 ...
- HDU 4035
dp求期望的题. 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望.E[1]即为所求. 叶子结点: E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i] ...
- ida动态调试so,在init_array和JNI_ONLOAD处下断点
本文涉及到的apk.请在github下载https://github.com/jltxgcy/AliCrack/AliCrackme_2.apk. 0x00 怎样在JNI_ONLOAD下断点.參考安卓 ...
- Light OJ 1317 Throwing Balls into the Baskets 概率DP
n个人 m个篮子 每一轮每一个人能够选m个篮子中一个扔球 扔中的概率都是p 求k轮后全部篮子里面球数量的期望值 依据全期望公式 进行一轮球数量的期望值为dp[1]*1+dp[2]*2+...+dp[ ...
- Math类概述及其成员方法
Math 类包括用于运行基本数学运算的方法,如初等指数.对数.平方根和三角函数,这个类寻常开发中用的不多,可是在某些需求上会用到,比方求二个用户年龄的相差多少岁,这会用到Math类中的方法!如今把Ma ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...