ZOJ 2475 Benny's Compiler 深搜查找环路

本文详细介绍了如何解决Benny的编译器在处理循环引用时链接器出现问题的问题。通过分析案例输入,采用深度优先搜索的方法来判断文件是否能成功编译,提供了解决此类编译器难题的实用思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1475

 

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

写代码思路要清晰,队友因为算法代码问题WA了很多次,自己重写了一遍。
 要注意 输入数据中x=y的情况 如:1  1 1  1 的结果是Yes

#include <stdio.h>
#include <string.h>

bool visit[105];
bool map[105][105];
int n;

bool res;

void dfs(int x)
{
	int i;
	for(i=1;i<=n;i++)
	{
		if(map[x][i])
		{	
			if(visit[i]==true)
			    res=true;
			visit[i]=true;
			dfs(i);
			visit[i]=false;
		}
		if(res)
			break;
	}
}

int main()
{
	int i,x,y;
	while(scanf("%d",&n)&&(n>=0))
	{
		res=false;
		memset(visit,false,sizeof(visit));
		memset(map,false,sizeof(map));
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&x,&y);
			if(!map[x][y])
			{
				if(x!=y)
				  map[x][y]=true;
				if(map[y][x])            //x==y的情况
					res=true;
			}
		}
		scanf("%d",&x);
		if(res)
			printf("No\n");
		else 
		{
			visit[x]=true;
			dfs(x);
			if(res)
				printf("No\n");
			else 
				printf("Yes\n");
		}
	}
	return 0;
}



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值