Alien’s Necklace

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1526    Accepted Submission(s): 415

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.
 

题意:让找到一个环使得组成环的点数为奇数且点数至少为3,。

由于一个点能够多个途径到达,但从一点出发第一次到达的肯定是最短路径,又题目要求的奇偶性,每次到达一个点步数的奇偶性进行标记。

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
#define N 1005
#define ll __int64
const int inf=0x7fffffff;
vector<int>g[N];
int vis[N][2];
struct node
{
int u,t;
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
int bfs(int s)
{
int i,u,v;
priority_queue<node >q;
node cur,next;
cur.u=s;
cur.t=1;
memset(vis,0,sizeof(vis));
vis[s][1]=1; //奇数点。用一个珠子
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
u=cur.u;
for(i=0;i<g[u].size();i++)
{
next.u=v=g[u][i];
next.t=cur.t+1;
if(v==s&&next.t%2==0&&next.t>3) //结点处到达两次。故应该减一
return next.t-1;
if(!vis[v][next.t%2])
{
vis[v][next.t%2]=1;
q.push(next);
}
}
}
return inf;
}
int main()
{
int i,n,m,u,v,tt,cnt=0;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
g[i].clear();
while(m--)
{
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
int ans=inf;
for(i=1;i<=n;i++)
ans=min(ans,bfs(i));
if(ans==inf)
printf("Case %d: Poor JYY.\n",++cnt);
else
printf("Case %d: JYY has to use %d balls.\n",++cnt,ans);
}
return 0;
}

以下这样的方法是用一个数组记录到达该点的步数,当再次訪问该点时。说明出现环,直接推断奇偶性就能够了。

又要求所用珠子数目大于三,所以要用一个pre变量记录上一个节点的标号,推断是否是两点直接成环。

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
#define N 1005
#define ll __int64
const int inf=0x7fffffff;
vector<int>g[N];
int vis[N][2];
int ans;
struct node
{
int u,t,pre;
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
int bfs(int s)
{
int i,u,v,t;
priority_queue<node >q;
node cur,next;
cur.u=s;
cur.t=1;
cur.pre=-1;
memset(vis,0,sizeof(vis));
vis[s][1]=1;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
u=cur.u;
for(i=0;i<g[u].size();i++)
{
next.u=v=g[u][i];
next.t=cur.t+1;
next.pre=u;
if(v==cur.pre)
continue;
if(vis[v][0]) //偶数点
{
t=cur.t+vis[v][0];
if(t%2==0)
return t-1;
}
else if(vis[v][1])
{
t=cur.t+vis[v][1];
if(t%2==0)
return t-1;
}
else
{
vis[v][next.t%2]=next.t;
q.push(next);
}
}
}
return inf;
}
int main()
{
int i,n,m,u,v,tt,cnt=0;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
g[i].clear();
while(m--)
{
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
ans=inf;
for(i=1;i<=n;i++)
ans=min(ans,bfs(i));
if(ans==inf)
printf("Case %d: Poor JYY.\n",++cnt);
else
printf("Case %d: JYY has to use %d balls.\n",++cnt,ans);
}
return 0;
}

hdu 1689 Alien’s Necklace (bfs层次图剪枝)的更多相关文章

  1. POJ3967Ideal Path[反向bfs 层次图]

    Ideal Path Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1754   Accepted: 240 Descri ...

  2. hdu 1689 求奇环bfs关键是层次图

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> usin ...

  3. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

  4. hdu 1044 BFS(压缩图)+DFS

    题意:              给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路:           看完这个题目果断 BFS+三维的mark ...

  5. 使用Architecture Explorer分析应用程序及使用层次图

    使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer ...

  6. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  7. UIKit框架类层次图

    学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

  8. uva 10983 Buy one, get the rest free 二分判定层次图

    二分枚举租用飞机的最大花费,然后用小于等于最大花费的边构建层次图(依据时间) 构图思路:   利用二元组(x,y)表示 x天y城市 1. e天有飞机从a城市飞到b城市,能够承载x人,则添加单向边 ( ...

  9. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

随机推荐

  1. linux-ngnix服务(一)

    httpd MPM: prefork:进程模型,两级结构,主进程master负责生成子进程,每个子进程负责响应一个请求 worker:线程模型,三级结构,主进程master负责生成子进程,每个子进程负 ...

  2. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...

  3. 爬虫cookie

    Cookie Cookie 是指某些网站服务器为了辨别用户身份和进行Session跟踪,而储存在用户浏览器上的文本文件,Cookie可以保持登录信息到用户下次与服务器的会话. Cookie原理 HTT ...

  4. xfce-openvas9

    1安装OpenVas 第一步,添加PPA源,在这我用的是一台新装的Ubuntu安装OpenVas,运行以下命令就可以进行安装 root@ubuntu:~# add-apt-repository ppa ...

  5. 四丶人生苦短,我用python【第四篇】

    1 基本数据类型 数字    int 字符串   str 布尔值   bool 列表       list 元组       tuple 字典       dict >>>type( ...

  6. move_uploaded_file failed to open stream permission denied

    Make sure that: IWPG_user, where user is a system user of the subscription who has rights to "R ...

  7. Unity3D for iOS初级教程:Part 3/3

    转自Unity 3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这份教程是由教程团队成员 ...

  8. 算法复习——高斯消元(ssoi)

    题目: 题目描述 Tom 是个品学兼优的好学生,但由于智商问题,算术学得不是很好,尤其是在解方程这个方面.虽然他解决 2x=2 这样的方程游刃有余,但是对于下面这样的方程组就束手无策了.x+y=3x- ...

  9. cf493E Vasya and Polynomial

    Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...

  10. GFS, HDFS, Blob File System架构对比

    分布式文件系统很多,包括GFS,HDFS,淘宝开源的TFS,Tencent用于相册存储的TFS (Tencent FS,为了便于区别,后续称为QFS),以及Facebook Haystack.其中,T ...