http://www.lydsy.com/JudgeOnline/problem.php?id=3402

又是spfa水题。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=20005, Q=N*10, oo=~0u>>2;
int q[Q], front, tail, d[N], vis[N], ihead[N], cnt, n, m;
struct ED { int to, next; }e[Q];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void spfa() {
q[tail++]=1;
for1(i, 2, n) d[i]=oo;
vis[1]=1;
while(front!=tail) {
int v, u=q[front++]; if(front==Q) front=0; vis[u]=0;
for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+1) {
d[v]=d[u]+1;
if(!vis[v]) {
vis[v]=1;
q[tail++]=v; if(tail==Q) tail=0;
}
}
}
} int main() {
read(n); read(m);
for1(i, 1, m) {
int u=getint(), v=getint();
add(u, v);
}
spfa();
int mx=0, ans=0, id=oo;
for1(i, 1, n) mx=max(d[i], mx);
for1(i, 1, n) if(d[i]==mx) { ++ans; id=min(i, id); }
printf("%d %d %d", id, mx, ans);
return 0;
}

Description

    贝茜在和约翰玩一个“捉迷藏”的游戏.
    她正要找出所有适合她躲藏的安全牛棚.一共有 N(2≤N≤20000)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发.所有的牛棚由M(1≤M≤50000)条双向路连接,每条双向路连 接两个不同的牛棚.所有的牛棚都是相通的.贝茜认为同牛棚1距离最远的的牛棚是安全的.两个牛棚间的距离是指,从一个牛棚到另一个牛棚最少需要通过的道路 数量.请帮贝茜找出所有的安全牛棚.

Input

    第1行输入两个整数N和M,之后M行每行输入两个整数,表示一条路的两个端点.
   

Output

仅一行,输出三个整数.第1个表示安全牛棚(如果有多个,输出编号最小的);第2个表示牛棚1和安全牛棚的距离;第3个表示有多少个安全的牛棚.

Sample Input

6 7
3 6
4 3
3 2
1 3
1 2
2 4
5 2

Sample Output

4 2 3

HINT

Source

【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)的更多相关文章

  1. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    题目 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MB Description     贝 ...

  2. BZOJ—— 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3402 Description     贝茜在和约翰玩一个“捉迷藏”的游戏.     她正要找出所有适 ...

  3. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏(最短路)

    这个= =一看就是最短路了= = PS:最近有点懒 = = 刚才看到一道平衡树的裸题还嫌懒不去写= =算了等刷完这堆水题再去理= = CODE: #include<cstdio>#incl ...

  4. 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 78  Solved: 6 ...

  5. BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 4 ...

  6. B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路

    直接最短路板子,dij堆优化. 题干: 题目描述 贝茜在和约翰玩一个“捉迷藏”的游戏. 她正要找出所有适合她躲藏的安全牛棚.一共有N(≤N≤)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发. ...

  7. bzoj 1576: [Usaco2009 Jan]安全路经Travel【spfa+树链剖分+线段树】

    这几天写USACO水题脑子锈住了--上来就贪心,一交就WA 事实上这个是一个叫最短路树的东西,因为能保证只有一条最短路,所以所有最短路合起来是一棵以1为根的树,并且在这棵树上,每个点被精灵占据的路是它 ...

  8. 【BZOJ】【1941】【SDOI2010】Hide and Seek

    KD-Tree 一开始看错题了

  9. bzoj:1941: [Sdoi2010]Hide and Seek

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 531  Solved: 295[Submi ...

随机推荐

  1. docker学习笔记二:常用命令

    docker学习笔记二:常用命令 查看docker常用命令 docker --help 返回结果如下: 其中常用的命令如下: 1.image相关操作 展示所有的image: 删除image: rmi ...

  2. Cocos2d-x设置吞没单击属性来避免精灵重叠被点击后的事件续传

    代码如下: Size visibleSize = Director::getInstance()->getVisibleSize(); /* create two sprites which h ...

  3. [iOS开发] 使用Jenkins自动打包并上传至蒲公英

    设置构建触发器 Poll SCM H/2 * * * * 设置 构建脚本 # #xodebuild & jenkins 自动构建并上传至pgyer.com #2017年5月9日 # #定义一些 ...

  4. 安卓Eclipse开发人员的福音

    我们知道.谷歌已经放弃对Eclipse(ADT)的维护更新了.如今官网上也找不到ADT的下载链接了,我们大多数同学仍在使用的ADT版本号可能已经非常老了,预计大多数的SDK版本号仅仅到4.4,而,在尝 ...

  5. 【Linux】监控系统的状态

    1.w命令 w命令是一个很强大的命令,该命令显示的信息比较丰富.以下是我的虚拟机w命令的一个展示 从上图我们可以看到: 第一行从左面开始显示的信息依次为:时间.系统运行时间.登陆用户数.平均负载 第二 ...

  6. Android 6.0 超级简单的权限申请 (Permission)

    代码地址如下:http://www.demodashi.com/demo/13369.html 背景描述 随着Android系统的不断升级,谷歌对用户的隐私是越来越注重了,给我们开发者带来了更多的繁琐 ...

  7. oracle将一个表中字段的值赋值到另一个表中字段(批量)

    面积表中数据错误,现将面积表中的sfmj字段的值改为居民信息表中匹配字段的值 update (select s.name name1,s2.name name2 from simple s,simpl ...

  8. CSS经验库

    1.兼容360浏览器 字体大小设置 开发中需要使用em单位 font-size: 0.83em; font-family: "Arial"; -webkit-text-size-a ...

  9. 自实现部分string类的功能

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class MyString { publi ...

  10. python3 JSON对象的学习

    import jsonname = { 'alex':[22,'M'], 'tony':[21,'F'],} name_after = json.dumps(name)name_after = nam ...