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;
}

你可能感兴趣的:(hdu1269 tarjan算法)