hdu1269

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n, m;
vector<int>vec[10005];
int vis[10005];
int dfn[10005], low[10005];
int belong[10005];
int st[10005];
int tim, cnt, top;

void dfs(int u){
	dfn[u] = low[u] = ++tim;
	int len = vec[u].size();
	vis[u] = 1;
	st[++top] = u;
	for (int i = 0; i < len; i++){
		int v = vec[u][i];
		if(!dfn[v]){
			dfs(v);
			low[u] = min(low[u], low[v]);
		}else{
			if(vis[v] && low[u] > dfn[v]){
				low[u] = dfn[v];
			}
		}
	}
	if(dfn[u] == low[u]){
		cnt++;
		int x;
		do{
			x = st[top--];
			vis[x] = 0;
			belong[x] = cnt;
		}while(x != u);
	}
}

void Tarjan(){
	memset(vis, 0, sizeof(vis));
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(belong, 0, sizeof(belong));
	tim = cnt = top = 0;
	for (int i = 1; i <= n; i++){
		if(!dfn[i]){
			dfs(i);
		}
	}
	int flag = 0;
	for (int i = 1; i <= n; i++){
		if(belong[i] != cnt){
			flag = 1;
			break;
		}
	}
	if(flag) printf("No\n");
	else printf("Yes\n");
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	while(scanf("%d %d", &n, &m) == 2){
		if(!n && !m) break;
		int u, v;
		for (int i = 1; i <= n; i++) vec[i].clear();
		for (int i = 1; i <= m; i++){
			scanf("%d %d", &u, &v);
			vec[u].push_back(v);
		}
		Tarjan();
	}

	return 0;
}
/**/

 

全部评论

相关推荐

蚂蚁 基架java (n+6)*16 签字费若干
点赞 评论 收藏
分享
勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务