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. 【solr这四个主题】大约VelocityResponseWriter

    一个.大约Velocity基本配置 在Solr在,可以以多种方式返回搜索结果,作为一个简单的文字回复(XML.JSON.CSV等待),能够返回velocity.js等格式.而VelocityRespo ...

  2. Android - 直线(line)画法

    Android - 直线(line)画法 本文地址: http://blog.csdn.net/caroline_wendy 横线(horizontal line) <View android: ...

  3. 第十二章——SQLServer统计信息(1)——创建和更新统计信息

    原文:第十二章--SQLServer统计信息(1)--创建和更新统计信息 简介: 查询的统计信息: 目前为止,已经介绍了选择索引.维护索引.如果有合适的索引并实时更新统计信息,那么优化器会选择有用的索 ...

  4. Android开发之合并文件的几种方式

    以下介绍合并文件的几种方式,并通过合并amr文件来举例介绍合并文件的详细流程.amr格式的文件头是6字节,所以在进行文件合并的时候要减去除第一个文件以外的其它文件的文件头. 注意:不同文件的文件头是不 ...

  5. 学习英语每日一 On the house. 赠品

    tp=webp" alt=""> On the house. 免费赠送.我们之前学过请客能够说I'll buy you something. 事实上还有一种说法是I ...

  6. Kafka项目实践

    用户日志上报实时统计之编码实践 1.概述 本课程的视频教程地址:<Kafka实战项目之编码实践>  该课程我以用户实时上报日志案例为基础,带着大家去完成各个KPI的编码工作,实现生产模块. ...

  7. linux+win7双系统重装win7修复grub的办法(转)

    本人是debian+win7的双系统, 下面介绍下重装win7的整个过程以及遇到的一些小问题,在查阅相关博客和朋友的帮助下成功修复, 记录下以便以后有不时之需, 也希望能帮助到遇到同样问题的朋友! 首 ...

  8. Android的相关的源代码的方法

    这里给大家介绍一个非常方便的相关法源代码. 1.打开Android SDK Manager.把你所使用的版本号的API给下载下来,例如以下图所看到的... 2.关联源代码时,将源代码关联到相应API的 ...

  9. 类(class)能不能自己继承自己(转)

    类(class)能不能自己继承自己不行,继承关系会出现环. 假设类A继承类A.那么要新建一个类A的对象,就必须先建立一个类A父类的对象.这是一个递归的过程,而且没有终止条件.会死循环的. 从编译的角度 ...

  10. Git客户端(Windows系统)的使用(Putty)(转)

    本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.14.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...