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;
}
- HDU168Alien’s Necklace解题报告(长亮杯2008全国大学生程序设计)
- USACO_Broken Necklace解题报告
- 2008湘潭大学程序设计比赛解题报告
- Polya 定理 (附POJ 1286 Necklace of Beads 解题报告)
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem B: The Farmer’s Heritage
- 河南理工大学程序设计(ACM)大赛解题报告
- Goldbach's Conjecture 解题报告
- Eddy's mistakes解题报告
- ZYB's Biology解题报告
- ZYB's Game解题报告
- 『reverse』2016年全国大学生信息安全大赛——珍贵资料解题报告
- A Knight's Journey解题报告(陈小宾)
- K - Goldbach's Conjecture解题报告(陈渊)
- K - Goldbach's Conjecture解题报告(张宇)
- G - There's Treasure Everywhere!解题报告(熊禾强)
- POJ 1739 Tony's Tour 解题报告(插头DP)
- Uva 11038 - How Many O's? 解题报告(计数)
- UVa 10905 Children's Game 解题报告(排序)
- socket 介绍
- Hamming Code ECC (汉明码错误检测与修正)
- csdn 第一篇
- 学黑客必去的网站“黑客基地”:
- PHP and Web Services 学习笔记
- HDU168Alien’s Necklace解题报告(长亮杯2008全国大学生程序设计)
- === 三个等号意味着严格等于
- 再说《山楂树之恋》
- DataTable已属于另一个DataSet问题的解办法
- 自定义clistctrl
- PHP开发工具比较
- 奇偶校验法
- el表达式
- 利用Ajax检测帐户是否存在.