https://vjudge.net/problem/HDU-4324

题意

每组数据一个n表示n个人,接下n*n的矩阵表示这些人之间的关系,输入一定满足若A不喜欢B则B一定喜欢A,且不会出现A和B相互喜欢的情况,问你这些人中是否存在三角恋。

分析

就是求是否存在三元环。判断图是否存在环,可以使用拓扑排序,排序失败则有环。那么如何判断存在的环是三元环呢?

假设现在存在n元环,在环上有这样相邻的三个元素x--》y--》z。如果是z--》x,那么存在三元环;否则,必存在x--》z,那此时可以把y给忽略,就变成了n-1元环。就这样缩减下去,必定有三元环。因此,此题就是求是否存在环。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+;
//const int MAXN = 210;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case #%d: ",++T);
}
const int MAXN = ;
const int MAXM = ;
char ma[MAXN];
int in[MAXN];
int head[MAXN], cnt;
int n, k; struct node {
int u, v, next;
}; node edge[MAXN * MAXN]; void init(){
cnt = ;
memset(head, -, sizeof(head));
memset(in, , sizeof(in));
} void add(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void topu(){
testcase();
queue<int> q;
int ans=;
for(int i=;i<n;++i){
if(!in[i]){
q.push(i);
ans++;
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
in[v]--;
if(!in[v]){
q.push(v);
ans++;
}
}
}
if(n==ans)
printf("No\n");
else
printf("Yes\n");
} int main (){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
scanf("%d", &t);
T = ;
while(t--){
init();
scanf("%d ", &n);
for(int i = ; i < n; ++i){
gets(ma);
for(int j = ; j < n; ++j)
if(ma[j] == ''){
add(i, j);
in[j]++;
}
}
topu();
}
return ;
}

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

  1. HDU 4324 Triangle LOVE 拓扑排序

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

  2. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

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

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

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

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

  5. hdu 4324 Triangle LOVE(拓扑判环)

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

  6. hdu 4324 Triangle LOVE

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...

  7. hdu 5098 双队列拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...

  8. HDU 5811 Colosseo(拓扑排序+单调DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...

  9. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

随机推荐

  1. Activiti启动某个流程失败,页面报500

    现象:Activiti启动某个流程失败,页面报500,错误日志如下. 2017-06-19 10:50:09 [org.activiti.engine.impl.interceptor.Command ...

  2. What Is Apache Hadoop

    What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...

  3. Windows系统下PHP使用Redis

    参考链接:https://www.cnblogs.com/lhat/p/6402472.html 环境:windows 10  64位操作系统    php 5.4 redis 3.0 1.redis ...

  4. javaIO缓冲区

    java中IO类分类. 图来自网络 缓冲区:应用程序在内存中开辟的一个空间.用来放置需要被写入或写出的数据. 使用缓冲区的 优点:使得应用程序操作磁盘(或者说是与磁盘的通信)的次数降低,提高应用程序的 ...

  5. Delphi之Exception获得错误信息(简单好理解)

    Delphi之Exception获得错误信息 相关资料: http://www.cnblogs.com/hackpig/archive/2010/02/15/1668547.html 实例代码: 1 ...

  6. js 单项链表

    介绍链表 链表是由一组节点组成的集合.每一个节点都使用一个对象的引用指向它的后续借点.指向另外一个借点的引用叫做链. 很多编程语言中数组的长度是固定的,就是定义数组的时候需要定义数组的长度,所以当数组 ...

  7. Bootstrap学习目录

    前面的话 Bootstrap与CSS的关系,类似于javascript与jQuery的关系,原理与应用的关系.只是jQuery不再火爆,而Bootstrap依然火热,它在github有着超过100万的 ...

  8. Enea推出Linux实时加速方案专门针对Xilinx UltraScale+

    导读 Enea(纳斯达克OMX Nordic:ENEA)Linux实时加速方案的扩展版本,完全集成了Xilinx UltraScale +系列的所有处理单元.借助Xilinx开发者大会(XDF)201 ...

  9. BZOJ4870 [六省联考2017] 组合数问题 【快速幂】

    题目分析: 构造f[nk][r]表示题目中要求的东西.容易发现递推公式f[nk][r]=f[nk-1][r]+f[nk-1][(r-1)%k].矩阵快速幂可以优化,时间复杂度O(k^3logn). 代 ...

  10. hdu 5919 Sequence II (可持久化线段树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...