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的工资,最低工 ...
随机推荐
- vSCode打开文件老覆盖原窗口
https://segmentfault.com/q/1010000006131199?_ea=1023522 设置中搜preview,改为false
- Git直接拉取远程分支
用Git,一直有个疑惑,可不可以不拉取远程Origin主干,我直接pull一个分支下来 今天想了一下,找到了一个办法 本地分支关联 // 0.新建一个文件夹,然后初始化git git init // ...
- mysql zerofill 的使用
转自:http://www.jquerycn.cn/blog/mysql/ 那这个int[M]中M是什么意义喃,在定义数值型数据类型的时候,可以在关键字括号内指定整数值(如:int(M),M的最大值为 ...
- Springmvc 上传文件MultipartFile 转File
转自:http://blog.csdn.net/boneix/article/details/51303207 业务场景:ssm框架 上传文件到应用服务器过程中要传到专有的文件服务器并返回url进行其 ...
- Python引用(import)文件夹下的py文件的方法
Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...
- 五毛党可能要失业了,因为AI水军来了
当AI已经开始写稿.唱歌.翻译文章.把语音转录为文字的时候,我们其实应该清醒的认识到,五毛党要消亡了. 相信大部分人和小编一样,现在只要出门吃饭,就会打开大众点评搜好吃的,看评分,看网友的评论.一般来 ...
- ubuntu12.04 lts 安装gcc 4.8
gcc 4.8.1 是第一个完全支持C++11 的编译器,Windows上可以安装mingw版的,在sourceforge 上有下载,安装也比较方便.在Linux上安装的话需要首先安装一些依赖库.在U ...
- Xcode missing file or .png is missing from working copy
当不小心在工程文件中删掉文件时.有可能会提示 .xxx is missing from working copy 有可能是SVN引起的.删掉这个文件就好了 如果是单个文件.进入Terminal 相 ...
- 对IIC总线时序的一点理解以及ACK和NACK(NAK)
参考自:http://blog.chinaunix.net/uid-16100003-id-3059814.html 关于IIC的响应问题:对于每一个接收设备(从设备,slaver),当它被寻址后,都 ...
- QT之设计部件背景色
一.使用QT样式表设计部件外观 样式表使用文本描写叙述,能够使用QApplication::setStyleSheet()函数将其设置到整个应用程序上.也能够使用QWidget::setStyleSh ...