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 ...
随机推荐
- js的函数-function
function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...
- 奇技淫巧,还是正统功夫? - Python推导式最全用法
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...
- JS实现鼠标移入DIV随机变换颜色
今天分享一个在 JavaScript中,实现一个鼠标移入可以随机变换颜色,本质就是js的随机数运用. 代码如下: <!DOCTYPE html> <html> <head ...
- How does Go kit compare to Micro?
Go kit - Frequently asked questions https://gokit.io/faq/ How does Go kit compare to Micro? Like Go ...
- nginx proxy pass redirects ignore port
nginx proxy pass redirects ignore port $host in this order of precedence: host name from the request ...
- 20201101gryz模拟赛解题报告
写在前面 2020rp++ 停课的第一场模拟赛 拿上一年的上一年的day1来考的, 结果得分期望220pts,实际135pts,rank3,太菜了 考着考着机房灯突然灭了,当时慌的一批 以为断电代码要 ...
- Linux性能分析:生产环境服务器变慢,诊断思路和性能评估
Linux性能分析:生产环境服务器变慢,诊断思路和性能评估 一.整机:top 二.CPU:vmstat 所有CPU核信息 每个进程使用CPU的用量分解信息 三.内存:free 四.硬盘:df 五.磁盘 ...
- 从官方文档中探索MySQL分页的几种方式及分页优化
概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...
- thymeleaf第一篇:什么是-->为什么要使用-->有啥好处这玩意
Thymeleaf3.0版本官方地址 1 Introducing Thymeleaf Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP . ...
- java封装详解
三大特性之---封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能 ...