ZOJ 2475 Benny's Compiler 有向图判读点是否存在环内
2013-11-25 10:11
441 查看
点击打开链接
Benny's Compiler
Time Limit: 2 Seconds Memory Limit: 65536 KB
These days Benny has designed a new compiler for C programming language. His compilation system provides a compiler driver that invokes the language preprocessor, compiler, assembler
and linker. C source file (with .C suffix) is translated to relocatable object module first, and then all modules are linked together to generate an executable object file.
The translator (preprocessor, compiler and assembler) works perfectly and can generate well optimized assembler code from C source file. But the linker has a serious bug -- it cannot
resolve global symbols when there are circular references. To be more specific, if file 1 references variables defined in file 2, file 2 references variables defined in file 3, ... file n-1 references variables defined in file n and file n references variables
defined in file 1, then Benny's linker walks out because it doesn't know which file should be processed first.
Your job is to determine whether a source file can be compiled successfully by Benny's compiler.
Input
There are multiple test cases! In each test case, the first line contains one integer N, and then N lines follow. In each of these lines there are two integers Ai and Bi, meaning that
file Ai references variables defined in file Bi (1 <= i <= N). The last line of the case contains one integer E, which is the file we want to compile.
A negative N denotes the end of input. Else you can assume 0 < N, Ai, Bi, E <= 100.
Output
There is just one line of output for each test case. If file E can be compiled successfully output "Yes", else output "No".
Sample Input
4
1 2
2 3
3 1
3 4
1
4
1 2
2 3
3 1
3 4
4
-1
Sample Output
No
Yes
Author: ZHENG, Lu
Source: Zhejiang Provincial Programming Contest 2005
给你n个点,然后给你点与点的连接关系,构成一个有向图,让你判读一个点是否在一个环内。若在,则输出No,否则输出Yes。
用dfs深搜,如果搜到已经访问过的点,则说明该点在环内,否则不再环内。
Benny's Compiler
Time Limit: 2 Seconds Memory Limit: 65536 KB
These days Benny has designed a new compiler for C programming language. His compilation system provides a compiler driver that invokes the language preprocessor, compiler, assembler
and linker. C source file (with .C suffix) is translated to relocatable object module first, and then all modules are linked together to generate an executable object file.
The translator (preprocessor, compiler and assembler) works perfectly and can generate well optimized assembler code from C source file. But the linker has a serious bug -- it cannot
resolve global symbols when there are circular references. To be more specific, if file 1 references variables defined in file 2, file 2 references variables defined in file 3, ... file n-1 references variables defined in file n and file n references variables
defined in file 1, then Benny's linker walks out because it doesn't know which file should be processed first.
Your job is to determine whether a source file can be compiled successfully by Benny's compiler.
Input
There are multiple test cases! In each test case, the first line contains one integer N, and then N lines follow. In each of these lines there are two integers Ai and Bi, meaning that
file Ai references variables defined in file Bi (1 <= i <= N). The last line of the case contains one integer E, which is the file we want to compile.
A negative N denotes the end of input. Else you can assume 0 < N, Ai, Bi, E <= 100.
Output
There is just one line of output for each test case. If file E can be compiled successfully output "Yes", else output "No".
Sample Input
4
1 2
2 3
3 1
3 4
1
4
1 2
2 3
3 1
3 4
4
-1
Sample Output
No
Yes
Author: ZHENG, Lu
Source: Zhejiang Provincial Programming Contest 2005
给你n个点,然后给你点与点的连接关系,构成一个有向图,让你判读一个点是否在一个环内。若在,则输出No,否则输出Yes。
用dfs深搜,如果搜到已经访问过的点,则说明该点在环内,否则不再环内。
#include<stdio.h> #include<string.h> #define M 107 int g[M][M],vis[M],flag,n; void dfs(int u) { for(int i=1;i<=n;i++) { if(vis[i]&&g[u][i]){flag=0;return;} if(g[u][i]) { vis[i]=1; dfs(i); vis[i]=0; } } } int main() { while(scanf("%d",&n)!=EOF&&n!=-1) { memset(vis,0,sizeof(vis)); memset(g,0,sizeof(g)); int u,v; flag=1; for(int i=1;i<=n;i++) { scanf("%d%d",&u,&v); if(u!=v)//如果不加,会WA,已验证 g[u][v]=1; } scanf("%d",&u); vis[u]=1; dfs(u); if(flag)printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- ZOJ 2475 Benny's Compiler(DFS)
- zoj 2475 Benny's Compiler
- zoj 2475 Benny's Compiler
- mysql 判读两个字符串是否存在交集(类似1,2,3字符串)
- poj 2585,zoj 2193 Window Pains【拓扑排序思想判断图是否存在环】
- zoj 1248 || poj 1474 Video Surveillance(判断多边形是否存在核)
- zoj 2475 Benny's Compiler
- zoj 2475 Benny's Compiler
- 判断数据库,函数名,表名,存储过程名称等是否存在
- asp.net 检查文件夹和文件是否存在
- HDU 1863 畅通工程 (最小生成树是否存在)
- 【41】递增数组判断是否存在两个树等于给定值
- 如何验证会员系统中用户的邮箱是否真实存在
- 3.13 判读是否是对称素数
- 网购保修是否存在问题
- hdoj 4324 Triangle LOVE 【拓扑排序判断是否存在可行解】
- php检测邮箱地址是否存在
- java判定数组或集合是否存在某个元素
- 获取应用程序目录,判断文件是否存在
- BAT循环判断进程是否存在 bat 如果存在则继续判断 不存在则重新运行