Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

网上怎么也找不到正确程序,于是我就贴一个出来方便大家食用=.=

Posted by pan64271 at 2015-06-29 08:57:05 on Problem 1139
刚从PASCAL转C++,所以写的是PASCAL风格的,表介意~


#include<cstdio>
#include<cstring>
using namespace std;
int text,n,f1,f2;
int map[500][500],q[3][500],h[3],t[3],f[500],flag[500],a[3];
void bfs(int x)
{
	h[x]=1;
	t[x]=1;
	q[x][1]=a[x];
	memset(f,0,sizeof(f));
	f[a[x]]=1;
	while (h[x]<=t[x])
	{
		for (int i=1;i<=n;i++)
		{
		  if ((!f[i])&&((map[q[x][h[x]]][i]==x)||(map[q[x][h[x]]][i]==3)))
		  {
		  	f[i]=1;
		  	q[x][++t[x]]=i;
		  }
	    }
		h[x]++;
	}
}
void write1()
{
	int i,j,k;
	k=0;
	memset(flag,0,sizeof(flag));
	for (int i=1;i<=t[1];i++)
	  for (int j=1;j<=t[2];j++)
	    if (q[1][i]==q[2][j])
	    {
	    	k=1;
	    	flag[q[1][i]]=1;
	    }
	if (k)  
	  printf("Y ");
	else
	  printf("N ");
}
void write2()
{
	int i,j,k,h,t;
	int q[500];
	h=1;
	t=1;
	q[h]=a[2];
	memset(f,0,sizeof(f));
	f[a[2]]=1;
	while (h<=t)
	{
		for (int i=1;i<=n;i++)
		  if ((!flag[i])&&(!f[i])&&(map[q[h]][i]>=2))
		  {
		  	f[i]=1;
		  	q[++t]=i;
		  	if (map[i][a[2]]==2)
		  	{
		  		printf("Y\n");
		  		return;
		  	}
		  }
		h++;
	}
	printf("N\n");
}
void work()
{
	int x,y;
	//freopen("1139.in","r",stdin);
	scanf("%d",&text);
	for (int k=1;k<=text;k++)
	{
		scanf("%d%d%d",&n,&a[1],&a[2]);
		memset(map,0,sizeof(map));		
		while (scanf("%d%d",&x,&y),(x!=-1)&&(y!=-1))
		{
			map[x][y]=1;
		}
		while (scanf("%d%d",&x,&y),(x!=-1)&&(y!=-1))
		{
			map[x][y]+=2;
		}
	    memset(h,0,sizeof(h));
	    memset(t,0,sizeof(t));
      	bfs(1);
	    bfs(2);
		write1();
    	write2();
	}
}
int main()
{
	work();
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator