ZOJ 2475 Benny's Compiler 순환 로 를 샅 샅 이 뒤 져 라.

주소: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
"); else { visit[x]=true; dfs(x); if(res) printf("No
"); else printf("Yes
"); } } return 0; }

좋은 웹페이지 즐겨찾기