HDU 4324 Triangle LOVE (拓扑排序)
Triangle LOVE
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1707 Accepted Submission(s): 729
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”.
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).
Take the sample output for more details.
5
00100
10000
01001
11101
11000
5
01111
00000
01000
01100
01110
Case #2: No
题意分析(转载):
此题可以一遍拓扑排序判环求解 即只需要找到一个环,
就必定存在三元环 证明如下: 假设存在一个n元环,
因为a->b有边,b->a必定没边,反之也成立
所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,
就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,
这样就形成了一个n-1元环。。。。
所以只需证明n大于3时一定有三元环即可,显然成立。
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N=; int n,indeg[N]; //存储的是节点的入度
char str[N][N]; int main(){ //freopen("input.txt","r",stdin); int t,cases=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int flag=;
memset(indeg,,sizeof(indeg)); //将所有的节点入度初始化为0
int i,j;
for(i=;i<n;i++){
scanf("%s",str[i]);
for(j=;j<n;j++)
if(str[i][j]=='') //如果i喜欢j,则把j的入度加1
indeg[j]++;
}
for(i=;i<n;i++){
for(j=;j<n;j++)
if(indeg[j]==) //找出入度为0的节点
break;
if(j==n){ //任何一个节点的入度都不为0,说明存在环了,则必有三角恋
flag=;
break;
}else{
indeg[j]--; //除去当前结点
for(int k=;k<n;k++) //把从这个节点出发的引起的节点的入度都减去1
if(str[j][k]=='')
indeg[k]--;
}
}
if(flag)
printf("Case #%d: Yes\n",++cases);
else
printf("Case #%d: No\n",++cases);
}
return ;
}
HDU 4324 Triangle LOVE (拓扑排序)的更多相关文章
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- hdu 4324 Triangle LOVE(拓扑排序,基础)
题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...
- 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) To ...
- hdu 4324 Triangle LOVE
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
随机推荐
- google test框架与eclipse插件
1. https://github.com/google/googletest (google的测试框架) 2. eclipse测试框架插件 https://github.com/xgsa/cd ...
- (转)径向模糊效果shader
转自:http://blog.csdn.net/xoyojank/article/details/5146297 最先在这里看到:http://www.gamerendering.com/2008/1 ...
- SharpDX之Direct2D教程I——简单示例和Color(颜色)
研究Direct2D已经有一段时间了,也写了一个系列的文章 Direct2D ,是基于Windows API Code Pack 1.1.在前文 Direct2D教程VIII——几何(Geometry ...
- jQuery实现倒计时效果-杨秀徐
本实例效果:剩余368天22小时39分57秒结束 代码简单易懂,适用各种倒计时: <!DOCTYPE html> <head> <title>jQuery实现倒计时 ...
- ZH奶酪:PHP抓取网页方法总结
From:http://www.jb51.net/article/24343.htm 在做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过ht ...
- IIS 之 IIS 7及以上多域名或端口绑定同一物理目录并设置不同默认文档
今天在 IIS 7 多端口或域名绑定同一物理目录,设置不同的默认文档遇到问题:同一物理目录的多个站点修改任意一个站点默认文档都会一起更改. 原因:在同一个物理目录下只有一个 web.config,并且 ...
- Linux下安装配置SNMP服务
一.安装snmp服务 1.检查系统是否已经安装snmp的rpm包 以下是安装snmp服务需要的rpm包: libsensors3-2.10.6-55.el5.i386.rpm lm_sensors-2 ...
- 微信小程序字符串如何转数字?
[微信小程序]字符串如何转数字 字符串可以采用 parseInt()方法来转换为数字. input_number_sim = parseInt(input_number_sim) 也可采用的用 ...
- C# Chart控件的使用总结1
(1)来自SQL server 此时就可以用查询语句,来获取你要用到的数据 (要引用如下命名空间: using System.Data.SqlClient; usingSystem.Windows.F ...
- SSH小问题:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HA ...