hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3566 Accepted Submission(s):
1395
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”.
<= 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).
print “Yes”, if there is a “Triangle Love” among these N people, otherwise print
“No”.
Take the sample output for more details.
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#define MAX 2010
using namespace std;
int n,k;
vector<int>map[MAX];
char s[MAX];
int vis[MAX];
void getmap()
{
int i,j;
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=n;i++)
{
scanf("%s",s);
for(j=0;j<n;j++)
{
if(s[j]=='1')
{
map[i].push_back(j+1);
vis[j+1]++;
}
}
}
} void tuopu()
{
int i,j;
queue<int>q;
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
q.push(i);
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
ans++;
q.pop();
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
q.push(v);
}
}
printf("Case #%d: ",k++);
if(ans!=n)
printf("Yes\n");
else
printf("No\n");
}
int main()
{
int t,i,j;
k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
getmap();
tuopu();
}
return 0;
}
hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】的更多相关文章
- UVA-1572 Self-Assembly(拓扑排序判断有向环)
题目: 给出几种正方形,每种正方形有无穷多个.在连接的时候正方形可以旋转.翻转. 正方形的每条边上都有一个大写英文字母加‘+’或‘-’.00,当字母相同符号不同时,这两条边可以相连接,00不能和任何边 ...
- Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- hihocoder 1174 [BFS /拓扑排序判断是否有环]
hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...
- 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393
//拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- hdoj 4324 Triangle LOVE 【拓扑】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
随机推荐
- Unix时间戳 和 NSDate 的转换
一个时间戳字符串:NSString *timeStampStr = @"1441602721"; 转换成时间 double unixTimeStamp ...
- OC - 14.NSOperation与NSOperationQueue
简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...
- C++学习笔记-1-自增和自减运算符
1. post-increment and pre-increment 的区别 来源:http://www.c4learn.com/c-programming/c-increment-operator ...
- Introduction to object
1 Declarations VS definitions (Page 81) declarations: This function or variable exists somew ...
- JS中的 this
JS中的 this 变化多端,似乎难以捉摸,但实际上对 this 的解读,还是有一定规律的. 分析this,该如何下手呢?下面有一个函数 function show(){ alert(this); } ...
- easy ui tree 取父节点的值和取蓝色框的值
var nodes = $('#basetree').tree('getChecked'); var cnode = ''; var fnode = ''; for ( var i = 0; i &l ...
- windows 基础及基本软件测试环境搭建
- 批处理协同blat自动发邮件
Blat - A Windows (32 & 64 bit) command line SMTP mailer. Use it to automatically eMail logs, the ...
- [译]36 Days of Web Testing(三)
Day 14: Automate the tedious Why ? 有些时候,web测试还是蛮单调乏味的,在开始测试前,你可能要必须跳转到一个特定的表单页面,或则为了得到一个特定的页面(或配置),你 ...
- 树莓派连接GPS模块
一月份的时候觉得好玩买了树莓派,但是太懒没怎么研究,但最近当初买树莓派时的那个梦想又萦绕心头,决定抽空完成一下当年的计划~ GPS模块是其中很重要的一环,于是在某宝上搜索,找了一家相对便宜也很轻巧的G ...