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]和 线段树维护区间和 . 建树的时候初始 ...
随机推荐
- 【Java_基础】JVM内存模型与垃圾回收机制
1. JVM内存模型 Java虚拟机在程序执行过程会把jvm的内存分为若干个不同的数据区域来管理,这些区域有自己的用途,以及创建和销毁时间. JVM内存模型如下图所示 1.1 程序计数器 程序计数器( ...
- (26)zabbix脚本报警介质自定义(钉钉)
zabbix机器人告警配置 首先在钉钉中创建一个群然后设置群机器人添加自定义机器人(webhook...) 添加后复制其中的webhook地址到报警脚本dingding.py中的webhook=... ...
- fshc之请求仲裁机制(from mcu and cache)
1.arbiter模块本身放在sclk时钟域,但是输入都是来之HCLK时钟域. 2.当MCU/CACHE访问FSHC时,FSHC不接受其他请求,FSHC只可以同时处理一个请求的操作. 3.如果原子操作 ...
- gnu make规则记录
1. $(shell CMD) 名称: 执行 shell 命令函数 功能: 在新的 shell 中执行 CMD 命令 返回值: CMD 在 shell 中执行的结果 例如:PLATFORM=$(she ...
- POJ 1651 区间DP Multiplication Puzzle
此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) ...
- IntelliJ IDEA 类和方法注释的生成以及Javadoc的简单使用记录
idea,设置类注释和,方法注释的常见的设置方法(不同的版本设置方法有所偏差,简单记录一些目前自己在使用的方法,) 方法注释:在keyMap中搜索Fix doc comment ,后点击右键设置一个快 ...
- z作业二总结
这是我的第二次作业,之前在课上所学的我发现已经忘得差不多了,这次的作业让我做的非常累,感觉整个人生都不太好了. 作业中的知识点:int(整型) float(单精度) double(双精度) char( ...
- excel设置单元格为文本
可以使用分裂功能,解决单元格无法设置成文本的问题.
- 多重部分和 poj1742
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- 暑假训练Round1——G: Hkhv的水题之二(字符串的最小表示)
Problem 1057: Hkhv的水题之二 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...