Triangle LOVE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3566    Accepted Submission(s):
1395

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). 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).
 
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
 
题意:给出一个矩阵s[][]如果其中s[i][j]等于1代表i喜欢j,(但是题目保证不可能有两个人互相相爱)现在问这些人的关系中是否存在三角           恋(A爱B,B爱C,C爱A)如果有则输出yes否则输出no
题解:因为题目已经保证不会有两个人互相相爱,所以说成环最少也是三人环,直接拓扑排序判断是否成环即可(要用邻接表做,矩阵会超           时)
#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【拓扑排序判断是否存在环】的更多相关文章

  1. UVA-1572 Self-Assembly(拓扑排序判断有向环)

    题目: 给出几种正方形,每种正方形有无穷多个.在连接的时候正方形可以旋转.翻转. 正方形的每条边上都有一个大写英文字母加‘+’或‘-’.00,当字母相同符号不同时,这两条边可以相连接,00不能和任何边 ...

  2. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  3. HDU 4324 Triangle LOVE 拓扑排序

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

  4. hihocoder 1174 [BFS /拓扑排序判断是否有环]

    hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...

  5. 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393

    //拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...

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

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

  7. hdoj 4324 Triangle LOVE 【拓扑】

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

  8. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  9. POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28399   Accepted: 9684 De ...

随机推荐

  1. (转)iOS被开发者遗忘在角落的NSException-其实它很强大

    转载自 http://www.jianshu.com/p/05aad21e319e iOS被开发者遗忘在角落的NSException-其实它很强大 字数597 阅读968 评论4 喜欢28 NSExc ...

  2. 设置appicon和启动图

    设置appicon General – App Icon Source 设置AppIcon 图片大小 iphone 6P(@3x) 180x180 iphone 6(@2x) iphone5/5s ( ...

  3. android入门到熟练(二)----活动

    1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...

  4. 『重构--改善既有代码的设计』读书笔记----Change Reference to Value

    如果你有一个引用对象,很小且不可改变,而且不易管理,你就需要考虑将他改为一个值对象.在Change Value to Reference我们说过,要在引用对象和值对象之间做选择,有时候并不容易,有了重 ...

  5. js 之 json

    /*JSON语法数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组 JSON 数据的书写格式是:名称/值对名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值;如"myw ...

  6. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  7. 读书笔记-《Training Products of Experts by Minimizing Contrastive Divergence》

    Training Products of Experts by Minimizing Contrastive Divergence(以下简称 PoE)是 DBN 和深度学习理论的 肇始之篇,最近在爬梳 ...

  8. Bag of Words/Bag of Features的Matlab源码发布

    2010年11月19日 ⁄ 技术, 科研 ⁄ 共 1296字 ⁄ 评论数 26 ⁄ 被围观 4,150 阅读+ 由于自己以前发过一篇文章讲bow特征的matlab代码的优化的<Bag-Of-Wo ...

  9. APCS

    arm汇编程序中,R0,R1,R2,R3,R12都是作为中间寄存器,而R4-R11是不能随便使用的,暂时我还不知它们的用途.所以,中间寄存器,在程序运行的开始处与结束的时候值是可以不一样的,也就是说中 ...

  10. activity学习(1) 生命周期理解

    可以忽略onWindowFocusChanged.onSaveInstanceState.onRestoreInstanceState几个事件,这几个事件官网中的生命周期里面没有提到.忽略掉这几个方法 ...