HDU - 4324 Triangle LOVE(拓扑排序)
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(拓扑排序)的更多相关文章
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 4324 Triangle LOVE (拓扑排序)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 4324 Triangle LOVE(拓扑排序,基础)
题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...
- hdu 4324 Triangle LOVE(拓扑判环)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 4324 Triangle LOVE
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
随机推荐
- 常用的查询DOM的方法
查询body的方法=========== document.body // document.getElementsTagname("body")[0]; 查询html的方法=== ...
- ThreadPoolExecutor使用详解
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线 ...
- [转帖] Win10 多桌面转换的快捷键
http://down.52pk.com/zhishi/52202.shtml win10系统中用户除了可以切换任务之外,还可以进行多桌面切换,就像是切换了电脑屏幕一样,可能网友们还都不知道win10 ...
- [转载] 虚拟机下面安装windows+oracle ASM的过程
转帖:https://www.2cto.com/database/201303/195261.html 最开始的时候 我找了一个挺好的教程 安装过 但是已经找不到了,先转载一下这个内容,后续再测试完善 ...
- BZOJ1916[USACO 2010 Open Gold 2.Water Slides]——DP+记忆化搜索
题目描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪.超级轨道包含 E (1 <= E <=15 ...
- Luogu4782 【模板】2-SAT 问题(2-SAT)
模板.注意若x=y不一定是废话,x=0或x=0表示x必须为0.以及数组开2n. #include<iostream> #include<cstdio> #include< ...
- Spring的 AOP底层用到两种代理机制
JDK 的动态代理:针对实现了接口的类产生代理.CGlib 的动态代理:针对没有实现接口的类产生代理,应用的是底层的字节码增强的技术 生成当前类的子类对象 JDK动态代理实现1. 创建接口和对应实现类 ...
- poj1753 【枚举】
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- 小强学Python+OpenCV之-1.3绘图
目标 今天的课程比较轻松,我们来学习一下OpenCV中几个绘图函数: cv2.line cv2.rectangle cv2.circle 画直线 直接经过前面两节的内容.我想直接上代码应该是可以接受的 ...