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. Action、Category、Data、Extras知识具体解释

    开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...

  2. 【转】Android 避免APP启动闪黑屏(Theme和Style)

    前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受.没办法,只能想想怎么解决,最后找到了下面的方法.闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和on ...

  3. NUnit3 Test Adapter vs2015

    NUnit的安装 前言:NUnit是什么? NUnit 是一个单元测试框架,专门针对于.NET来写的.NUnit是xUnit家族种的第4个主打产品,完全由C#语言来编写,并且编写时充分利用了许多.NE ...

  4. 我的第一次windows规划

    #include <windows.h>   LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; //WinMain功能被分配一 ...

  5. 鸟哥Linux私房菜 基础学习篇读书笔记(10):Linux磁盘和文件系统管理(3)

    本文总结了Linux操作系统来管理我们的硬盘和文件系统需要使用命令.当我们在系统中增加一个硬盘驱动器.什么是我们需要去通过这个硬盘就可以真正使用步骤?下面步骤: (1)对磁盘进行分区,新建能够使用的分 ...

  6. Redis系列之(一):10分钟玩转Redis(转)

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  7. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  8. iOS 执行ScrollView在空白空间-解

    iOS7 中间UIViewControllers配置automaticallyAdjustsScrollViewInsets.假设YES.scrollview它会根据status bar, navig ...

  9. [python]自问自答:python -m参数? (转)

    python -m xxx.py 作用是:把xxx.py文件当做模块启动但是我一直不明白当做模块启动到底有什么用.python xxx.py和python -m xxx.py有什么区别! 自问自答: ...

  10. cocos2dx 制作单机麻将(二)

    cocos2dx 制作单机麻将(二) 打乱麻将顺序2 前面解说了怎样打乱初始给定的麻将牌堆, 另一种是打乱随意给定的麻将牌堆 //混乱扑克2 void RandAppointCardData(BYTE ...