HDU168Alien’s Necklace解题报告(长亮杯2008全国大学生程序设计)

来源:互联网 发布:html获取xml数据 编辑:程序博客网 时间:2024/06/02 20:53

Problem Description

JYY is taking a trip to Mars. To get accepted by the Martians, he decided to make a magic necklace for their king. (Otherwise, JYY will be eaten) Now, he has collected many magic balls, and he is going to string them up.

Unfortunately, only particular pairs of balls can be adjacent in the necklace, otherwise they will explode. Notice that the first and the last ball in the necklace are also adjacent. Besides, the Martians think even numbers are unlucky, so the number of balls in the necklace must be odd. (Of course each ball can be used only once)

A necklace contains at least 3 balls. Because the balls are really precious, JYY wants the necklace has as few balls as possible. (Then he can give rest balls to his GF)

So JYY wonders the number of balls he has to use to make this necklace.

 

Input

The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.

For each input, the first line contains 2 numbers N and M, N is the number of balls JYY collected, and M is the pairs of compatible balls. Balls are numbered from 1 to N. Followed M lines, each contains 2 numbers A and B, means that ball A and ball B are compatible. For each case, 0 < N <= 1,000, 0 < M <= 20,000.

 

Output

If the gift can't be done, just print "Poor JYY." in a line, otherwise, print the minimal number of balls in the necklace. Use the format in the example.

 

Sample Input

2

5 6

1 2

2 4

1 3

3 5

4 3

4 5

2 1

1 2

 

Sample Output

Case 1: JYY has to use 3 balls.

Case 2: Poor JYY.

 

这题时间虽然为5秒但还是超时了很多次,基本思路是,首先要对数据进行离散化处理,然后枚举每一条边,进行深度优先搜索,这时搜索的剪枝非常重要,这题的难点也是在这里。剪枝效果最明显的是,假如枚举某一条边进行搜索时,一个点Ni有可能是很多状态的中间态,这时你不能将每个状态搜到底,要进行剪枝。具体方法是,记录每个状态搜索到这点Ni的最小搜索深度值,如果当前的搜索深度大于点Ni的最小搜索深度值,那么就可以停止继续往下搜索,因为再搜下去的最优解不可能优于当前最优解,以下代码中,用used[]数组记录每个点的最小搜索深度。

#include <iostream>using namespace std;int a[20002],b[20002];int used[1002];int minni,m,n,enab,mm;int data[1002][1002];int conn[1002][1002];int shu[1002];inline void dfs(int nn,int deep){   int i;  if(conn[nn][mm]){   deep++;   if(deep%2&&(deep < minni))     minni = deep;   return ;  }  if(minni == 3) return ;  if(nn == mm){   if(deep%2&&(deep < minni))     minni = deep;   return ;  }  if(deep >= used[nn]&&used[nn])   return ;  else used[nn] = deep;  if(deep >= minni) return ;  for(i  = 0;i < shu[nn];i++)   if(conn[nn][data[nn][i]])    {          dfs(data[nn][i],deep+1);         }

   }int main(){ int cas,i,k,now,c,d; scanf("%d",&cas); for(k = 1;k <= cas;k++) {  minni = 1500;  now = 0;  scanf("%d%d",&n,&m);  memset(conn,0,sizeof(conn));  memset(shu,0,sizeof(shu));   for(i = 0; i < m;i++)   {   scanf("%d%d",&c,&d);   if(!conn[c][d]){    a[now] = c;    b[now++] = d;    data[c][shu[c]++] = d;                            //此处进行离散化处理,也可以用vector类来保存    data[d][shu[d]++] = c;    conn[c][d] = 1;    conn[d][c] = 1;   }

  }

  for(i = 0;i < now;i++)  {   enab = a[i];mm = b[i];   conn[enab][mm] = 0;   conn[mm][enab] = 0;   memset(used,0,sizeof(used));   dfs(a[i],1);   if(minni == 3) break;   conn[enab][mm] = 1;   conn[mm][enab] = 1;  }  if(minni!=1500&&minni!=1) printf("Case %d: JYY has to use %d balls./n",k,minni);  else printf("Case %d: Poor JYY./n",k); } return 0;

}