Strategic game POJ - 1463 dfs
题意+题解:
1 //5
2 //1 1
3 //2 1
4 //3 1
5 //1 1
6 //给你5个点,从下面第二行到第五行(称为i行),每一行两个数x,y。表示i和x之间有一条边。这一条边的长度为y
7 //你需要找出来每一个点与所有点相距的最大值。并且在最后输出。
8 //要注意这是一棵树,为什么这样说呢。因为你只有n-1条边,而且每一个点都至少有一条边
9
10 //这样的话我们可以用bfs找出来随意一个点x距离其他点的距离。然后找到其中那个与x相距最大的点x1,再用他去跑一遍bfs
11 //再找出来距离这个点x1相距最大的点x2。再跑一遍bfs。让这些距离取最大值输出就可以了
12 //
13 //因为你第一次找到的那个点肯定是图的深度最深的叶节点x1。在用这个x1找另一个叶节点x2,其他点与与所有点的最大距离肯定
14 //在这两个点之间。。因为这两个点就是深度最大的
15 #include<stdio.h>
16 #include<string.h>
17 #include<iostream>
18 #include<algorithm>
19 #include<queue>
20 using namespace std;
21 const int maxn=20010;
22 int cnt,head[maxn],deap1[maxn],vis[maxn],deap2[maxn],n,deap3[maxn];
23 queue<int>r;
24 struct edge
25 {
26 int u,v,next,w;
27 }e[maxn];
28 void init(int deap[maxn])
29 {
30 for(int i=1;i<=n;++i)
31 deap[i]=0;
32 }
33 void add_edge(int x,int y,int z)
34 {
35 e[cnt].u=x;
36 e[cnt].v=y;
37 e[cnt].w=z;
38 e[cnt].next=head[x];
39 head[x]=cnt++;
40 }
41 void bfs(int x,int deap[maxn])
42 {
43 while(!r.empty()) r.pop();
44 init(vis);
45 vis[x]=1;
46 r.push(x);
47 while(!r.empty())
48 {
49 int u=r.front();
50 r.pop();
51 for(int i=head[u];i!=-1;i=e[i].next)
52 {
53 int v=e[i].v;
54 if(!vis[v])
55 {
56 vis[v]=1;
57 deap[v]=deap[u]+e[i].w;
58 r.push(v);
59 }
60 }
61 }
62 }
63 int main()
64 {
65 while(~scanf("%d",&n))
66 {
67 memset(head,-1,sizeof(head));
68 cnt=0;
69 for(int i=2;i<=n;++i)
70 {
71 int x,y;
72 scanf("%d%d",&x,&y);
73 add_edge(i,x,y);
74 add_edge(x,i,y);
75 }
76 init(deap1);
77 bfs(1,deap1);
78 int ans1=0,id1=-1;//printf("**\n");
79 for(int i=1;i<=n;++i)
80 {
81 if(ans1<deap1[i])
82 {
83 ans1=deap1[i];
84 id1=i;
85 }
86 }
87 init(deap2);
88 if(id1!=-1)
89 {
90 bfs(id1,deap2);
91 }
92 ans1=0,id1=-1;
93 for(int i=1;i<=n;++i)
94 {
95 if(ans1<deap2[i])
96 {
97 ans1=deap2[i];
98 id1=i;
99 }
100 }
101 init(deap3);
102 if(id1!=-1)
103 {
104 bfs(id1,deap3);
105 }
106 for(int i=1;i<=n;++i)
107 {
108 printf("%d\n",max(deap1[i],max(deap2[i],deap3[i])));
109 }
110 }
111 return 0;
112 }
Strategic game POJ - 1463 dfs的更多相关文章
- 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463
B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...
- Strategic game POJ - 1463 【最小点覆盖集】
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- Strategic game(POJ 1463 树形DP)
Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 7490 Accepted: 3483 De ...
- (树形DP)Strategic game POJ - 1463
题意: 给你一棵树,树的每一个节点可以守护与其相连的所有边,问你最少用多少个节点可以守护这整棵树 思路: 仔细思考不难发现,要想守护一条边,边的两个端点必须有一个可以被选(两个都选也可以),然后这个问 ...
- Strategic game POJ - 1463
题目链接 依旧是树形dp啦,一样的找根节点然后自下而上更新即可 设\(dp_{i,0}\)表示第i个不设,\(dp_{i,1}\)表示第i个设一个,容易得到其状态转移 \(dp_{i,0} = \su ...
- Strategic game POJ - 1463 树型dp
//题意:就是你需要派最少的士兵来巡查每一条边.相当于求最少点覆盖,用最少的点将所有边都覆盖掉//题解://因为这是一棵树,所以对于每一条边的两个端点,肯定要至少有一个点需要放入士兵,那么对于x-&g ...
- poj 1463 Strategic game DP
题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS Memory Limit: 10000K Tot ...
- poj 1463 Strategic game
题目链接:http://poj.org/problem?id=1463 题意:给出一个无向图,每个节点只有一个父亲节点,可以有多个孩子节点,在一个节点上如果有一位战士守着,那么他可以守住和此节点相连的 ...
- POJ 1463 Strategic game(二分图最大匹配)
Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...
随机推荐
- Error: Could not open input file: /usr/java/jdk1.7.0_07/jre/lib/jsse.pack
[root@localhost ~]# rpm -ivh jdk-7u7-linux-i586.rpm Preparing... ################################### ...
- 原生工程接入Flutter实现混编
前言 上半年我定的OKR目标是帮助团队将App切入Flutter,实现统一技术栈,变革成多端融合开发模式.Flutter目前是跨平台方案中最有潜力实现我们这个目标的,不管是Hybird还是React ...
- LVS负载均衡NAT模式原理介绍以及配置实战
LVS基本原理 流程解释: 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间 PREROUTING 链首先会接收到用户请求,判断目标 IP 确定是本机 IP ...
- SpringBoot 报错: Circular view path [readingList] 解决办法
spring boot报错: Circular view path [readingList]: would dispatch back to the current handler URL [/re ...
- Java NIO ———— Buffer 缓冲区详解 入门
引言缓冲区是一个用于特定基本类型的容器.由java.nio 包定义,所有缓冲区都是 Buffer 抽象类的子类. Java NIO 中的 Buffer ,主要用于与NIO 通道进行交互.数据从通道存入 ...
- Centos GitLab 配置
如果重启之后,gitlab-ctl restart报"runsv no running"错,先运行下面命令 systemctl start gitlab-runsvdir.serv ...
- 使用eventfd创建一个用于事件通知的文件描述符
https://www.jianshu.com/p/57cc1d7d354f nat穿透代码c++
- SQLyog破解30天到期
开始--运行中输入regedit.找到regedit.exe 文件 点击regedit.exe...就把注册表编辑器打开了 我们需要找到记录软件使用实现的数据...找到HKEY-CURRENT ...
- KMP算法 字符串匹配(看猫片)
前言 此篇笔记根据自己的理解和练习心得来解释算法,只代表个人观点,如有不足请指出(我刚学QWQ) 浅谈字符串匹配 设想一个场景,假设你是一个净化网络语言环境的管理员,每天需要翻阅大量的文章和帖子来查找 ...
- LOJ10100
原题来自:CEOI 1996 一个电话线公司(简称 TLC)正在建立一个新的电话线缆网络,他们连接了若干个地点,编号分别从 1 到 N,没有两个地点有相同的号码,这些线是双向的并且能使两个地点保持通讯 ...