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. 玩转Web之Jsp(三)-----Jsp+SQLServer 用sql语句实现分页

    在BBS的实现里,jsp与sqlserver 结合的操作中,怎样实现分页,使每页显示根帖的名字,并按发表时间降序排列? 在这里举例说明,page_size为每页显示的条数,pageNo为当前页数,st ...

  2. Python科学计算库演示

    号码值计算基础 NumPy至Python提供了高速的多维数组处理的能力.而SciPy则在NumPy基础上加入了众多的科学计算所需的各种工具包,有了这两个库,Python就有差点儿和Matlab一样的处 ...

  3. Windows Phone 独立存储资源管理器工具

    如何使用独立存储资源管理器工具 http://msdn.microsoft.com/zh-CN/library/hh286408(v=vs.92)C:\Program Files (x86)\Micr ...

  4. 软测试综述——PV操作

     在操作系统中,进程之间常常会存在相互排斥(都须要共享独占性资源时)和同步(完毕异步的两个进程的协作)两种关系.而信号量和PV操作完美有效的处理了这两种情况.     相互排斥:就好比过独木桥,一 ...

  5. jquery的智能提示控件

    福利到~分享一个基于jquery的智能提示控件intellSeach.js   一.需求 我们经常会遇到[站内搜索]的需求,为了提高用户体验,我们希望能做到像百度那样的即时智能提示.例如:某公司人事管 ...

  6. hdu4758 Walk Through Squares 自动机+DP

    题意:给n*m的地图,在地图的点上走,(n+1)*(m+1)个点,两种操作:往下走D和往右走R.现在要从左上角走到右下角,给定两个操作串,问包含这两个串的走法总共有多少种. 做法:用这两个串构建自动机 ...

  7. 每天一个JavaScript实例-canvas绘图

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Javascript标准类型的方法集

    1 array.concat(item...) concat方法会产生一个新数组,将一个或多个item附加在数组之后 var a = ['a', 'b', 'c'] var b = ['x', 'y' ...

  9. Https 客户端与服务器交互过程梳理(转)

    本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...

  10. HDU4893:Wow! Such Sequence!(段树lazy)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...