HDU3342:判断有向图中是否存在3元环-Tarjan或拓扑排序
题目大意:
给你一个关系图,判断是否合法。每个人都有师父和徒弟,可以有很多个;
若A是B的师父,B是C的师父,则A也算C的师父。
不合法:
1) . 互为师徒;(有回路)
2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的师父。(出现回路)
思路:
判断有向图中是否存在回路或至少3元环;
此题至少有三种做法,此处更新拓扑排序的做法:
解题方法:
一:拓扑排序:
1) . 统计每个点的入度;
2) . 将入度为0的点加入队列;
3) . 出去队首元素,将此元素所连接的点入度减一,若此后入度为0则加入队列;
4) . 判断队列循环次数,若等于n则不存在3元环,则此关系图合法;
题目链接:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<ctime>
using namespace std;
const int N = ;
const int M = ;
int n,m;
int tot,flag;
int in[N],head[N];
struct lp
{
int u,v,nex;
lp(){}
lp(int a,int b,int c):
u(a),v(b),nex(c){}
}cw[N];
void add(int a,int b){
cw[++tot]=lp(a,b,head[a]);
head[a]=tot;
}
void tuopu(){
queue<int>Q;
while(!Q.empty())Q.pop();
for(int i=;i<n;++i){
if(in[i]==)Q.push(i);
}
int t=;
while(!Q.empty()){
t++;
int u=Q.front();Q.pop();
for(int i=head[u];i!=-;i=cw[i].nex){
int v=cw[i].v;
in[v]--;
if(in[v]==)Q.push(v);
}
}
if(t==n)flag=;
}
int main(int argc, char const *argv[])
{
int a,b;
while(~scanf("%d%d",&n,&m)&&(n)){
memset(in,,sizeof(in));
tot=-;
memset(head,-,sizeof(head));
for(int i=;i<m;++i){
scanf("%d%d",&a,&b);
add(a,b);
in[b]++;
}
flag=;
tuopu();
if(flag)printf("YES\n");
else printf("NO\n");
}
return ;
}
二:Tarjan:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<ctime>
using namespace std;
const int N = ;
const int M = ;
int n,tot,flag,idex;
int head[N],vis[N];
int low[N],dfn[N];
int qltNum;
int qltMap[N];
stack<int>st;
struct lp{
int to,nex;
lp(){}//构造函数
lp(int a,int b):
to(a),nex(b){}
}cw[N*N];
void add(int a,int b){
cw[++tot]=lp(b,head[a]);
head[a]=tot;
}
void dfs(int u,int fa){
dfn[u]=low[u]=++idex;
vis[u]=;
st.push(u);
int v;
for(int i=head[u];i!=-;i=cw[i].nex){
v=cw[i].to;
if(v==fa){
flag=;
break;
}
if(!vis[v]){
dfs(v,u);
if(flag)return;
low[u]=min(low[u],low[v]);
}else if(vis[v]==){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){//缩点
qltNum++;
int t=;
do{
t++;
v=st.top();st.pop();
vis[v]=;
qltMap[v]=qltNum;
if(t>=){
flag=;
return;
}
}while(v!=u);
//cout<<t<<"\n";
}
}
void tarjan(){
for(int i=;i<=n;++i){
if(!vis[i]){
dfs(i,-);
}
if(flag)return;
}
}
void init(){//初始化
while(!st.empty())st.pop();
qltNum=idex=flag=;
tot=-;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(qltMap,,sizeof(qltMap));
}
int main(int argc, char const *argv[]){
int a,b,m;
while(~scanf("%d%d",&n,&m)&&(n)){
init();
memset(head,-,sizeof(head));
for(int i=;i<m;++i){
scanf("%d%d",&a,&b);
a++,b++;
add(a,b);
}
tarjan();
if(flag)printf("NO\n");
else printf("YES\n");
}
return ;
}
HDU3342:判断有向图中是否存在3元环-Tarjan或拓扑排序的更多相关文章
- hdu3342-判断有向图中是否存在(至少)3元环或回路-拓扑排序
一:题目大意: 给你一个关系图,判断是否合法, 每个人都有师父和徒弟,可以有很多个: 不合法: 1) . 互为师徒:(有回路) 2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的 ...
- <数据结构>XDOJ323.判断有向图中是否有环
问题与解答 问题描述 判断有向图中是否有环. 输入格式 输入数据第一行是一个正整数,表示n个有向图,其余数据分成n组,每组第一个为一个整数,表示图中的顶点个数n,顶点数不超过100,之后为有向图的邻接 ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 有向图和拓扑排序Java实现
package practice; import java.util.ArrayDeque; import java.util.Iterator; import java.util.Stack; pu ...
- Lua中的元表与元方法
[前言] 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了“+”符号,就可以进行类的加法运算.在Lua中也有这个道理 ...
- Expm 10_1 带负权值边的有向图中的最短路径问题
[问题描述] 对于一个带负权值边的有向图,实现Bellman-Ford算法,求出从指定顶点s到其余顶点的最短路径,并判断图中是否存在负环. package org.xiu68.exp.exp10; p ...
- DFS应用——遍历有向图+判断有向图是否有圈
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...
- HDU3342有向图判圈DFS&&拓扑排序法
HDU3342 Legal or Not 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目意思:一群大牛互相问问题,大牛有不会的,会被更厉害 ...
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
随机推荐
- WebService发送 方法
public String disableSaleOut(JSONObject jsonObject) throws ServiceException, MalformedURLException, ...
- Java Number&Math类
Java Number类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double等. 实例 int i = 5000; float gpa = 13.65 ...
- Repeatable Read
在Repeatable Read隔离级别下,一个事务可能会遇到幻读(Phantom Read)的问题. 幻读是指,在一个事务中,第一次查询某条记录,发现没有,但是,当试图更新这条不存在的记录时,竟然能 ...
- (转)OpenFire源码学习之十二:HttpBind&Script Syntax
转:http://blog.csdn.net/huwenfeng_2011/article/details/43417343 HttpSessionManager 该类管理所有通过httpbing连接 ...
- STM32嵌入式开发学习笔记(六):串口通信(上)
本文我们将了解STM32与外部设备通过串口通信的方式. 所谓串口通信,其实是一个类似于计算机网络的概念,它有物理层,比如规定用什么线通信,几伏特算高电平,几伏特算低电平.传输层,通信前要发RTS,CT ...
- python3使用requests和requests_toolbelt上传文件
https://blog.csdn.net/summerpowerz/article/details/80293235 https://blog.csdn.net/lhh08hasee/article ...
- 3、获取APP 内存占用率
关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...
- cesium相关学习网址
cesium相关学习网址: cesium资料大全网址:https://www.cnblogs.com/cesium1/p/10062942.html http://192.168.101. ...
- 前端(十六)—— JavaScript盒子模型、JS动画、DOM、BOM
JS盒子模型.JS动画.DOM.BOM 一.JS盒模型 1.width | height parseInt(getComputedStyle(ele, null).getPropertyValue(' ...
- FTPClient登录慢的问题
java上传文件到ftp上,发现特别慢,debug了一下发现链接正常,ftp.login(username, password)这个登录方法特别慢 解决方案: vi /etc/vsftpd/vsftp ...