您的位置:首页 > 其它

ZOJ 2475 Benny's Compiler(DFS)

2013-05-09 19:27 399 查看
主要用的是dfs

注意如果 x 引用x 那么是可以的.

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn = 110;
int n, e;
bool g[maxn][maxn], vis[maxn];

bool dfs(int u){
vis[u] = 1;
for (int i = 1; i <= n; ++i){
if(g[u][i] && u != i){
if(vis[i])return false;
else{
vis[i] = 1;
if(!dfs(i)) return false;
vis[i] = 0;
}
}
}
return true;
}
int main(){
while (scanf("%d", &n) && n > 0){
memset(vis, 0, sizeof(vis));
memset(g, false, sizeof(g));
for (int i = 0; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
g[u][v] = 1;
}
scanf("%d", &e);
if(dfs(e)){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: