迷宫城堡
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2944 Accepted Submission(s): 1264
邻接表+深搜
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct node
{
int num;
struct node *next;
}node;
void DFS(node *map,int *visit,int i)
{
node *t;
for(t=map[i].next;t;t=t->next)
{
if(visit[t->num]==0)
{
visit[t->num]=1;
DFS(map,visit,t->num);
}
}
}
int main()
{
int m,n,i,j,x,y,flag;
int *visit;
node *map,*t;
while(scanf("%d%d",&n,&m)&&m||n)
{
flag=1;
map=(node *)malloc((n+1)*sizeof(node));
visit=(int *)malloc((n+1)*sizeof(int));
for(i=0;i<=n;++i)
{
map[i].num=i;
map[i].next=NULL;
}
for(i=0;i<m;++i)
{
scanf("%d%d",&x,&y);
t=(node *)malloc(sizeof(node));
t->num=y;
t->next=map[x].next;
map[x].next=t;
}
for(i=1;flag&&i<=n;++i)//每一个节点都搜索一遍,遇到不能遍历全部的直接跳出输出No
{
memset(visit,0,(n+1)*sizeof(int));//注意这里不能写成sizeof(visit),动态数组这样写不能实现初始化
visit[i]=1;
DFS(map,visit,i);//从第i个节点开始遍历
for(j=1;j<=n;++j)
if(visit[j]==0)
{
flag=0;
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}