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
题意: 有n个人 然后以下有n行(i) 每行有n个数字 假设第j个数字为1,表示i对j有好感。推断这些关系中是否有三角恋
代码:
#include <stdio.h>
#include <string.h>
int t,n;
//存储的是节点的入度
int in_degree[2010];
//存储的是i,j两个节点的关系,1:i love j,0:j love i
char adj_mat[2010][2010]; int main()
{
bool flag;//true表示为有三角恋。false表示为没有三角恋
scanf("%d",&t);
for(int i = 1; i <= t;i++)
{ scanf("%d",&n);
flag = false;
//将全部的节点入度初始化为0
memset(in_degree,0,sizeof(in_degree));
for(int j = 0; j < n; j++)
{
scanf("%s",adj_mat[j]);
for(int k=0;k<n;k++)
if(adj_mat[j][k]=='1')//假设j喜欢k,则把k的入度加1
in_degree[k]++;
} for(int j=0;j<n;j++)
{
int k;
for(k=0;k<n;k++)
if(in_degree[k]==0)break;//找出入度为0的节点
if(k==n)//不论什么一个节点的入度都不为0。说明存在环了,则必有三角恋
{
flag = true;
break;
}else{
//将这个点的入度设为-1,避免再次循环时有查到了这个节点,
//此时说明这个点已经从集合中除掉了
in_degree[k]--;
for(int p=0;p<n;p++)
{
//把从这个节点出发的引起的节点的入度都减去1
if(adj_mat[k][p]=='1'&&in_degree[p]!=0)
in_degree[p]--;
}
}
}
if(flag)
printf("Case #%d: Yes\n",i);
else printf("Case #%d: No\n",i);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 4324 Triangle LOVE 拓扑排序的更多相关文章

  1. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

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

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

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

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

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

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

  5. hdu 4324 Triangle LOVE(拓扑判环)

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

  6. hdu 4324 Triangle LOVE

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...

  7. hdu 5098 双队列拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...

  8. HDU 5811 Colosseo(拓扑排序+单调DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...

  9. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

随机推荐

  1. 从XML文件乱码问题,探寻其背后的原理(转)

    由于网友反应本文图片不能显示,由于时间关系未能及时修正.请访问原文地址: 本文出自http://blog.csdn.net/dinglang_2009/article/details/6895355, ...

  2. Jedis连接

    Jedis连接 到场api中的jedis.我们能够发现,jedis类提供了4个构造方法.都可用于连接: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29 ...

  3. unity3d c# 产生真正的随机数

    虽然能够使用Random类来生成随机数.但它是系统时钟种子,因此,有大量的反复产生伪随机数的. 您可以使用RNGCryptoServiceProvider();相对真随机数生成. 由加密服务提供程序( ...

  4. D其他项目打电话AL工程EF Model

     Based on your description and the error code, you will just need to ensure that your DataContext ...

  5. JDBC在getConnection之前为什么要调用Class.forName(转)

    获取一个数据库连接的通用模板如下: String driver = "oracle.jdbc.OracleDriver"; String url = "jdbc:orac ...

  6. ReferenceTypeDemo

    对象a作为参数argument在方法中使用时,如果argument在方法中赋予另一个对象的地址,则之后方法中对参数argument的操作,都不会影响到对象a. 方法中参数argument如果是对象,a ...

  7. .ARM.exidx

    简介: `.ARM.exidx` is the section containing information for unwinding the stack. If your C program ha ...

  8. C++11的一些功能

    .断言是将一个须要为真的表达式放在语句中,在debug模式下检查一些逻辑错误的參数.C++中使用assert须要使用<assert.h>或者<cassert>头文件.有函数定义 ...

  9. MEF初体验之七:Using Catalogs

    MEF特性化编程模型的价值主张之一是通过catalogs动态发现部件的能力.Catalogs允许应用程序很容易地消费那些通过[Export]已经自我注册的exports. Assembly Catal ...

  10. 跳跃Java一些周期,双跳FOR周期

    今天写的代码写在一个双层for周期,目前仍在使用Iterator,大致意思是假定在第二个周期在排位赛中给了整个双回路跳. 刚開始,直接使用break.巴拉巴拉的敲了一堆代码,信心满满的就直接执行.等到 ...