ZOJ 10th Anniversary Contest - C


水题,DFS+记忆化

View Code
 1 #include<stdio.h>
2 #include<stdlib.h>
3 int n,m,s,d,e[105][105],ok[105],cover[105];
4 int find(int x)
5 {
6 int i;
7 if(ok[x]==1)return 1;
8 for(i=1;i<=e[x][0];i++)
9 if(cover[e[x][i]]==0)
10 {
11 cover[e[x][i]]=1;
12 ok[x]=ok[x]|find(e[x][i]);
13 cover[e[x][i]]=0;
14 }
15 return ok[x];
16 }
17 int main()
18 {
19 int i,x,y,ans;
20 while(scanf("%d%d%d%d",&n,&m,&s,&d)!=EOF)
21 {
22 memset(e,0,sizeof(e));
23 memset(ok,0,sizeof(ok));
24 memset(cover,0,sizeof(cover));
25 for(i=1;i<=m;i++)
26 {
27 scanf("%d%d",&x,&y);
28 e[x][0]++;e[x][e[x][0]]=y;
29 e[y][0]++;e[y][e[y][0]]=x;
30 }
31 ok[d]=1;cover[s]=1;
32 find(s);
33 ans=0;
34 for(i=0;i<n;i++)
35 if(ok[i]!=1)ans++;
36 printf("%d\n",ans);
37 }
38 return 0;
39 }

你可能感兴趣的:(test)