URAL 1056 Computer Net(最短路)
Computer Net
Memory limit: 64 MB
Background
net is created by consecutive computer plug-up to one that has already
been connected to the net. Each new computer gets an ordinal number, but
the protocol contains the number of its parent computer in the net.
Thus, protocol consists of several numbers; the first of them is always
1, because the second computer can only be connected to the first one,
the second number is 1 or 2 and so forth. The total quantity of numbers
in the protocol is N − 1 (N is a total number of computers).
For instance, protocol 1, 1, 2, 2 corresponds to the following net:
1 - 2 - 5 |
Problem
Input
Output
Sample
input | output |
---|---|
5 |
1 2 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int vis[N],dis[N],pre[N],head[N];
int n,m,tot=,son,maxn;
struct EDG{int to,next;}edg[N*N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void bfs(int s) {
met(vis,);met(dis,inf);met(pre,);
dis[s] = ;
vis[s] = ;maxn=;
queue<int>q;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;maxn=max(maxn,dis[v]);pre[v]=u;
if(!vis[v]){
q.push(v);vis[v]=;
}
}
}
son=u;
}
}
int main() {
met(head,-);
int a[N],cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m);add(i,m);add(m,i);
}
bfs();int s=son;
bfs(son);int t=son;//printf("!!%d %d\n",s,t);
if(maxn&){
int x=maxn/;
while(t){
if(dis[t]==x||dis[t]==x+)a[cnt++]=t;
t=pre[t];
}
}
else {
int x=maxn/;
while(t){
if(dis[t]==x)a[cnt++]=t;
t=pre[t];
}
}
sort(a,a+cnt);
for(int i=;i<cnt;i++)printf("%d ",a[i]);
printf("\n");
return ;
}
URAL 1056 Computer Net(最短路)的更多相关文章
- URAL 1056(树形DP)
1056. Computer Net Time limit: 2.0 second Memory limit: 64 MB Background Computer net is created by ...
- URAL 1085 Meeting(最短路)
Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...
- URAL 1934 Black Spot(最短路)
Black Spot Time limit: 1.0 secondMemory limit: 64 MB Bootstrap: Jones's terrible leviathan will find ...
- URAL 2069 Hard Rock (最短路)
题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的. 析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多.要么是先横着,再竖着,要么是先横再竖再横,要么 ...
- Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...
- URAL 2034 Caravans(变态最短路)
Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...
- Let‘s play computer game(最短路 + dfs找出所有确定长度的最短路)
Let's play computer game Description xxxxxxxxx在疫情期间迷上了一款游戏,这个游戏一共有nnn个地点(编号为1--n1--n1--n),他每次从一个地点移动 ...
- DP/最短路 URAL 1741 Communication Fiend
题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...
- URAL 1002 Phone Numbers(KMP+最短路orDP)
In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...
随机推荐
- Fedora20的一些个人配置
0,老传统 yum install screenfetch 1,关闭蜂鸣器 edit /etc/bashrc setterm -blength 0#setterm -bfreq 10 #这个可以设置声 ...
- android开机启动过程
Android系统开机主要经历三个阶段: bootloader启动 Linux启动 Android启动 启动文件: 对于机器从通电到加载Linux系统一般需要三个文件:bootloader(引导文件) ...
- windows操作系统日常使用
快捷键使用: 看实例,学经验,我看行. 1.人体学输入设备被禁用怎么办(鼠标被禁用.键盘被禁用) 有一天脑子抽风,把鼠标给禁用了.以前不常用快捷键,这下必须学学怎么使用快捷键了,现在记下来,防止以后脑 ...
- poj2631 树的直径 + bfs
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...
- tableviewcell的取消选中,高亮
1.取消多余cell的分割线 UIView *view = [UIView new]; view.backgroundColor = [UIColor clearColor]; [tableView ...
- 利用pl/sql developer进行远程连接oracle server出现的问题及解决办法
由于本人刚刚给自己的笔记本做了系统,由原来的32位系统编程现在的64位系统,所以,很多软件由于兼容性,不得不重新安装...当我安完了pl/sql developer工具后,就满心欢喜的去连接远程ora ...
- 自定义UIAlertView
You can change accessoryView to any own customContentView in a standard alert view in iOS7 [alertVie ...
- ios 从网络上获取图片并在UIImageView中显示
ios 从网络上获取图片 -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...
- php大力力 [033节] 随便看看:PHP程序员学习C++
php大力力 [033节] 随便看看:PHP程序员学习C++ 2014 兄弟连高洛峰 PHP教程14.1.7 在PHP脚本中操作MySQL数据库4 观看 - 56.com http://www.med ...
- 作业1-我的第一个博客&GuiHub简单练习
自我介绍 姓名:苗中峰(不要问有什么含义,翻着字典取的名,翻到什么字就取了什么字) 性别:男(不解释) ...