您的位置:首页 > 其它

zoj 2475 Catenyms

2011-02-22 18:18 351 查看
DFS搜索。



开始以为是强连通分量,用tarjan套了半天模板不对 = =。。。



直接构图后,拿需要编译的编号为起点深搜即可,如果搜到的点标记过了,说明搜到环了,说明这个环不能被编译,以至于指向这个环的点都不能被编译。因为指向的点是变量在那个点里的。



一直WA,后来想到数据,1 1 1 1.这个应该输出Yes。。。改一下就好了。



#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
#include <limits.h>
#define MAX 101
using namespace std;
int map[MAX][MAX];
int vis[MAX];
int ans,n;
void init()
{
	ans = 1;
	memset(map,0,sizeof(map));
	memset(vis,0,sizeof(vis));
}
void DFS(int x)
{
	int i;
	for(i=1; i<=n; i++)
	{
		if( vis[i] == 1 && map[x][i] )
		{
			ans = 0;
			return ;
		}
		if( map[x][i] == 1 )
		{
			vis[i] = 1;
			DFS(i);
			if( ans == 0 )
				return ;
			vis[i] = 0;
		}
	}
}
int main()
{
	int from,to,i,m;
	while( ~scanf("%d",&n) && n != -1 )
	{
		init();
		for(i=1; i<=n; i++)
		{
			scanf("%d%d",&from,&to);
			if( from != to )
				map[from][to] = 1;
		}
		scanf("%d",&m);
		vis[m] = 1;
		DFS(m);
		if( ans )
			printf("Yes/n");
		else
			printf("No/n");
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: