hdu2208 dfs+并查集

 1 #include<stdio.h>
 2 #include<string.h>
 3 int n,m,min,father[15],map[15][15];
 4 int find(int i)
 5 {
 6     if (father[i]!=i) return(find(father[i]));
 7     return(father[i]);
 8 }
 9 void dfs(int now,int need)
10 {
11     int i,j,judge;
12     if (need>=min) return;
13     if (now==n)
14     {
15         if (need<min) min=need;
16         return;
17     }
18     for (i=0;i<now;i++)
19      {
20          judge=1;
21          for (j=0;j<now;j++)
22           if (find(j)==find(i)&&map[now][j]==0) judge=0;
23          if (judge==1){
24              father[now]=find(i);
25              dfs(now+1,need);
26          }
27      }
28      father[now]=now;
29      dfs(now+1,need+1);
30      return;
31 }
32 int main()
33 {
34     int i,j,k;
35     while (~scanf("%d%d",&n,&m))
36     {
37         memset(map,0,sizeof(map));
38         for (i=0;i<n;i++)
39         {
40             father[i]=i;
41             scanf("%d",&k);
42             while (k--)
43             {
44                 scanf("%d",&j);
45                 map[i][j]=map[j][i]=1;
46             }
47         }
48         min=m+1;
49         dfs(1,1);
50         if (min<=m) printf("%YES\n");
51         else printf("NO\n");
52     }
53     return 0;
54 } 

http://acm.hdu.edu.cn/showproblem.php?pid=2208

posted on 2014-07-16 16:15  xiao_xin  阅读(116)  评论(0编辑  收藏  举报

导航