Triangle LOVE

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 49   Accepted Submission(s) : 30
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). A[sub]i,j[/sub] = 1 means i-th people loves j-th people, otherwise A[sub]i,j[/sub] = 0. It is guaranteed that the given relationship is a tournament, that is, A[sub]i,i[/sub]= 0, A[sub]i,j[/sub] ≠ A[sub]j,i[/sub](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
代码:
 

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=;
int map[MAXN][MAXN];
int que[MAXN],flot,N,tot=;
char c[MAXN][MAXN];
queue<int>dl;
void topsort(){
for(int i=;i<=N;i++)
if(!que[i])dl.push(i);
//printf("%d",dl.size());
//puts("asfaf");
while(!dl.empty()){
int k=dl.front();
flot++;
dl.pop();
que[k]=-;
// printf("%d",dl.size());
for(int j=;j<=N;j++){
if(map[k][j])que[j]--;
if(!que[j])dl.push(j);
}
}
// puts("asfaf");
if(flot==N)printf("Case #%d: No\n",tot);
else printf("Case #%d: Yes\n",tot);
}
void initial(){
memset(que,,sizeof(que));
while(!dl.empty()){
dl.pop();
}
flot=;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
tot++;
initial();
scanf("%d",&N);
//puts("asfaf");
//printf("%d\n",N);
for(int i=;i<N;i++)scanf("%s",c[i]);
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
map[i][j]=c[i-][j-]-'';
if(map[i][j])que[j]++;
}
}
// puts("asfaf");
/* for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
printf("%d",map[i][j]);
}
puts("");
}*/
//puts("asfaf");
topsort();
}
return ;
}

Triangle LOVE(拓扑排序)的更多相关文章

  1. HDU 4324 Triangle LOVE 拓扑排序

    Problem Description Recently, scientists find that there is love between any of two people. For exam ...

  2. hdoj 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 (拓扑排序)

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. HDU4324 Triangle LOVE【拓扑排序】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. HDU 4324 (拓扑排序) Triangle LOVE

    因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...

  6. hdu 4324 Triangle LOVE(拓扑排序,基础)

    题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...

  7. hdu4324 Triangle LOVE (拓扑排序)

    这是一道最简单的拓扑排序题,好久没看这个算法了! 有点生疏了! 后附上百度的资料; #include<stdio.h> #include<string.h> int in[50 ...

  8. HDU - 4324 Triangle LOVE(拓扑排序)

    https://vjudge.net/problem/HDU-4324 题意 每组数据一个n表示n个人,接下n*n的矩阵表示这些人之间的关系,输入一定满足若A不喜欢B则B一定喜欢A,且不会出现A和B相 ...

  9. 拓扑排序/DFS HDOJ 4324 Triangle LOVE

    题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...

随机推荐

  1. Zigbee2007协议中文介绍

    Zigbee2007中文介绍ZigBee2007规范定义了ZigBee和ZigBee Pro两个特性集,全新的ZigBee 2007规范建立在ZigBee2006之上,不但提供了增强型的功能而且在某些 ...

  2. Windows Azure 的开源 DNA

    去年年底,第二期微软云加速器在中国正式启动,17 家创业公司入选.依靠云计算的 HTML 5 专业富媒体动画平台 Mugeda(乐享云)是其中之一.微软云加速器为创业公司提供大量支持,但 Mugeda ...

  3. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  4. 网易云课堂_C++程序设计入门(上)_第5单元:万类霜天竞自由 – 对象和类的更多内容_第5单元作业【4】 - 在线编程(难度:难)

    第5单元作业[4] - 在线编程(难度:难) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系 ...

  5. hdu 5607 graph (矩阵乘法快速幂)

    考虑一个经典的问题: 询问从某个点出发,走 k 步到达其它各点的方案数? 这个问题可以转化为矩阵相乘,所以矩阵快速幂即可解决. 本题思路: 矩阵经典问题:求从i点走k步后到达j点的方案数(mod p) ...

  6. JS属性

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. 代码: var a1;var a2 = tr ...

  7. DSP TMS320C6000基础学习(1)——介绍

    主要内容 1. Why process signals digitally? (1)模拟电路由模拟组件构成:电阻.电容及电感等,这些组件随着电压.温度或机械结构的改变会动态影响到模拟电路的效果: (2 ...

  8. centos6.2+nginx-1.2.3+php-5.3.17安装脚本

    #!/bin/bash # # vm test install script # # create by xk # # data 2013-04-25 # # SOFTPATH=/home/tools ...

  9. docker相关配置

    一.概述: 1.centos7下,默认firewalld为防火墙, systemctl status firewalld.service 2.关闭firewalld, systemctl stop f ...

  10. jQuery Tags Input Plugin 插件的使用

    一个jquery开发的标签功能加强插件,可以生成或删除标签,还能对输入重复标签进行检查,和JQuery autocomplete插件配合实现自动完成功能. 官网:http://xoxco.com/pr ...