hdu1269 tarjan算法

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=1e4+10;
const int M=1e5+10;
struct Edge{
	int to,nxt;
}edge[M];
int head[N];
int tot;
void init(){
	memset(head,-1,sizeof head);
	tot=0;
}
void addEdge(int u,int v){
	edge[tot].to=v;
	edge[tot].nxt=head[u];
	head[u]=tot++;
}
int n,m;
queue<int> Q;
int vis[N];
int id;
int getId(int x){
	if(vis[x]==0) vis[x]=++id; 
	return vis[x];
}
int low[N];
void dfs(int u){
	low[u]=u;
	for(int k=head[u];~k;k=edge[k].nxt){
		int v=edge[k].to;
		if(!vis[v]){
			getId(v);
			dfs(v);
		} 
		low[u]=min(low[u],low[v]);
	}

}
bool tarjan(){
	while(Q.size()) Q.pop();
	memset(vis,0,sizeof vis);
	id=0;
	getId(1);
	dfs(1);
	for(int i=1;i<=n;i++) if(vis[i]==0 || low[i]!=1) return false;
	return true;
}
int main()
{
    //freopen("in","r",stdin);
    //freopen("out.txt","w",stdout);
	while(scanf("%d%d",&n,&m)==2){
		if(n==0) break;
		init();
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			addEdge(u,v);
		}
		puts(tarjan()?"Yes":"No");
	}
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值