hdu 1689 Alien’s Necklace (bfs层次图剪枝)
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
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.
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.
2
5 6
1 2
2 4
1 3
3 5
4 3
4 5
2 1
1 2
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层次图剪枝)的更多相关文章
- POJ3967Ideal Path[反向bfs 层次图]
Ideal Path Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 1754 Accepted: 240 Descri ...
- hdu 1689 求奇环bfs关键是层次图
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> usin ...
- UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)
大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...
- hdu 1044 BFS(压缩图)+DFS
题意: 给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路: 看完这个题目果断 BFS+三维的mark ...
- 使用Architecture Explorer分析应用程序及使用层次图
使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer ...
- 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,最先 ...
- UIKit框架类层次图
学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.
- uva 10983 Buy one, get the rest free 二分判定层次图
二分枚举租用飞机的最大花费,然后用小于等于最大花费的边构建层次图(依据时间) 构图思路: 利用二元组(x,y)表示 x天y城市 1. e天有飞机从a城市飞到b城市,能够承载x人,则添加单向边 ( ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
随机推荐
- (52)zabbix_sender提交item数据
zabbix_sender是什么?有什么作用 zabbix获取key值有超时时间,如果自定义的key脚本一般需要执行很长时间,这根本没法去做监控,那怎么办呢?使用zabbix监控类型zabbix tr ...
- ES5中新增的forEach等新方法的一些使用声明
转载地址:http://www.zhangxinxu.com/wordpress/?p=3220 一.前言-索引 ES5中新增的不少东西,了解之对我们写JavaScript会有不少帮助,比如数组这块, ...
- Linux服务器的弱口令检测及端口扫描
一.弱口令检测--John the Ripper John the Ripper工具可以帮助我们扫描出系统中密码安全性较低的用户,并将扫描后的结果显示出来. 1.安装John the Ripper: ...
- Day13有参装饰器,三元表达式,匿名函数
多个装饰器: 加载顺序:由下而上 执行顺序:由上而下 有参装饰器: 闭包,给函数传参的一种方法 当装饰器内需要参数时,可以采用闭包形式给其传参,第三层函数接收完参数时,就变为无参装饰器 三元表达式: ...
- 杭电 1159 Common Subsequence
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- Cypress EZ-USB FX3 DMA模式下的串口通讯
由于公司设备升级后出了问题,需要对USB驱动进行修改,原本使用的是寄存器模式进行UART传输,但是由于FX3寄存器模式会出现长时间延时等待的问题,不得不对其传输模式进行修改.虽然赛普拉斯的EZ-USB ...
- Python (Page Object实例)
Page Object是Selenium自动化测试项目开发实践的最佳设计模式之一,通过对界面元素和功能模块的封装减少冗余代码,同时在后期维护中,若元素定位或功能模块发生变化,只需要调整页面元素或功能模 ...
- Cocos2d-JS 实现将TiledMap中的每个 tile 变成物理精灵的方法
How to generate a physics body from a tiledmap 鄙人在网上找了许久都未找到方法,说句实在话,Cocos2d官方给出的与物理引擎相关的内容真的不是很多, ...
- pytion3--文档字符串:__doc__
除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看.从语法上来说,这类注释是写成字符串,放在模块文档.函数以及类语句的顶端.就在任何可执行程序代码前(#注释在其前也没问 ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...