| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
网上怎么也找不到正确程序,于是我就贴一个出来方便大家食用=.=刚从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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator