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 ...
随机推荐
- Java OOP考试错题分析
解析: A.ArrayList 可以存储NULL值,也可以存储重复的值,对集合没有任何影响. B.一旦实例化不可改变自身大小,这是数组的特性.集合的容量是自身扩容的. C.ArrayList可以存 ...
- Codevs 4768 跳石头 NOIP2015 DAY2 T1
4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...
- Command 模式
Command 模式通过将请求封装到一个对象(Command)中,并将请求的接受者存放具体的 ConcreteCommand 类中(Receiver)中,从而实现调用操作的对象和操作的具体实现 者之间 ...
- 使用Node.js作为后台进行爬虫
看了一遍又一遍Node.js但是没过多久就又忘了,总想找点东西来练练手,就发现B站首页搜索框旁边的GIF图特别有意思,想着是不是可以写一个小Node.js项目把这些图全部扒下来,于是带着复习.预习与探 ...
- [Linux]命令root与other切换
切换至root:sudo -i切换至other:su 用户名
- cx_Oracle使用方法一
cx_Oracle使用方法 正确安装好cx_oracle之后,要使用它来连接到oracle数据库进行操作,具体应该分3步走: 第一步:导入cx_Oracle ,建立连接 >>> im ...
- C# this.Invoke()的作用和用法(摘)
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); 在多线程编程中,我们经常要在工作线程 ...
- 如何查看Windows下端口占用
查看端口占用的PID进程号 C:\Users\yan>netstat -ano | findstr "8888" 查看是哪个进程或者程序占用了17840端口 C:\Users ...
- Hibernate 注解多对一 要求在多那边产生一个外键而不会另外产生一个表
在使用hibernate注解的时候,我们映射一对多时,有时候莫名其妙的产生了两张表,其中一张表是A_B,这并不符合数据库中多的一方放置一个外键的原则,那么如何控制只产生一个表呢,请看下面的例子: 多的 ...
- JSP页面间传递参数的5种方法
JSP页面间传递参数是经常需要使用到的功能,有时还需要多个JSP页面间传递参数.下面介绍一下实现的方法. (1)直接在URL请求后添加 如:< a href="thexuan.jsp? ...