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-objectForKey与valueForKey在NSDictionary中的差异

    从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...

  2. #define和#undefine的用法

    #undef将保持已定义状态且在 作用域内,直到程序结束或者使用#undef 指令取消定义. 预处理器 在此程序中,我们将取消在先前程序中对预处理器的定义. 1 2 3 4 5 6 7 8 9 10 ...

  3. pl/sql编程

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. 回退符\b

    回退符\b #include <stdio.h> int main(){ printf("hello\b"); getchar(); getchar(); ; } 实验 ...

  5. php 解决大流量网站访问量问题

    当一个网站发展为知名网站的时候(如新浪,腾讯,网易,雅虎),网站的访问量通常都会非常大,如果使用虚拟主机的话,网站就会因为访问量过大而引起 服务器性能问题,这是很多人的烦恼,有人使用取消RSS等错误的 ...

  6. Model Thinking1

    Why Model Reason # 1: Intelligent Citizen of the World Reason # 2: Clearer Thinker Reason # 3: Under ...

  7. PR曲线平滑

    两天写论文中,本来设计的是要画这个Precision-Recall Curve的,因为PRC是从信息检索中来的,而且我又做的类似一个检索,所以要画这个图,但是我靠,竟然发现不好画,找了很多资料等.最后 ...

  8. BZOJ 3672 购票

    Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国\(n\)个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与 ...

  9. 【UVA1371】Period (二分+DP)

    题意: 给出两个字符串A,B将B分解成若干个子字符串,然后每个子字符串都要经过编辑变成字符串A,所有子串中编辑最多的次数即为当前状态下的最大编辑次数,要求求最小的最大编辑次数. 编辑操作包括修改.删除 ...

  10. 孤陋寡闻又一遭:ReportEvent API函数(有微软Service官方例子为例)

    API 详解: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363679(v=vs.85).aspx 使用例子: https: ...