题意+题解:

  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的更多相关文章

  1. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  2. Strategic game POJ - 1463 【最小点覆盖集】

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...

  3. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

  4. (树形DP)Strategic game POJ - 1463

    题意: 给你一棵树,树的每一个节点可以守护与其相连的所有边,问你最少用多少个节点可以守护这整棵树 思路: 仔细思考不难发现,要想守护一条边,边的两个端点必须有一个可以被选(两个都选也可以),然后这个问 ...

  5. Strategic game POJ - 1463

    题目链接 依旧是树形dp啦,一样的找根节点然后自下而上更新即可 设\(dp_{i,0}\)表示第i个不设,\(dp_{i,1}\)表示第i个设一个,容易得到其状态转移 \(dp_{i,0} = \su ...

  6. Strategic game POJ - 1463 树型dp

    //题意:就是你需要派最少的士兵来巡查每一条边.相当于求最少点覆盖,用最少的点将所有边都覆盖掉//题解://因为这是一棵树,所以对于每一条边的两个端点,肯定要至少有一个点需要放入士兵,那么对于x-&g ...

  7. poj 1463 Strategic game DP

    题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS   Memory Limit: 10000K Tot ...

  8. poj 1463 Strategic game

    题目链接:http://poj.org/problem?id=1463 题意:给出一个无向图,每个节点只有一个父亲节点,可以有多个孩子节点,在一个节点上如果有一位战士守着,那么他可以守住和此节点相连的 ...

  9. POJ 1463 Strategic game(二分图最大匹配)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

随机推荐

  1. Manjaro Linux 5.9.11-3安装和配置全局截图工具FlameShot教程

    背景说明 截图工具是日常适用频率较高的一种系统工具,在Linux下也有不少常用截图工具,如deepin-screenshot等,但是今天我们要介绍的是FlameShot--一款更加精致的Linux全局 ...

  2. 【Nginx】配置nginx图片服务器

    想通过nginx来访问服务器上的图片 可以搭建一个nginx图片服务器. 做法如下: 先安装nginx,这里直接用yum来进行安装的 安装方法如下: https://blog.csdn.net/iml ...

  3. 【Linux】rsync的相关用途

    Rsync,代表"remote sync",它是本地和远程主机文件同步工具.它只同步更改的文件,以此实现最小化传输数据. 我使用Ubuntu 16.04做为例子,但是你可以把它应用 ...

  4. 【Oracle】instr()函数详解

    1)instr()函数的格式  (俗称:字符查找函数) 格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( strin ...

  5. C#中foreach的实现原理

    C#中foreach的实现原理 在探讨foreach如何内部如何实现这个问题之前,我们需要理解两个C#里边的接口,IEnumerable 与 IEnumerator. 在C#里边的遍历集合时用到的相关 ...

  6. ctfhub技能树—文件上传—00截断

    什么是00截断 相关教程:http://www.admintony.com/%E5%85%B3%E4%BA%8E%E4%B8%8A%E4%BC%A0%E4%B8%AD%E7%9A%8400%E6%88 ...

  7. 使用line_profiler对python代码性能进行评估优化

    性能测试的意义 在做完一个python项目之后,我们经常要考虑对软件的性能进行优化.那么我们需要一个软件优化的思路,首先我们需要明确软件本身代码以及函数的瓶颈,最理想的情况就是有这样一个工具,能够将一 ...

  8. kernel升级模式RKS让人振奋

    前几天刚将我的ERP内核从701_rel 升级到721_ext_rel,看到721的说明了讲到,对于这次的更新,支持RKS(Rolling Kernel Switch)了,简单的讲,就是以后对于内核的 ...

  9. 目标检测的评价指标(TP、TN、FP、FN、Precision、Recall、IoU、mIoU、AP、mAP)

    1. TP TN FP FN ​ GroundTruth 预测结果 TP(True Positives): 真的正样本 = [正样本 被正确分为 正样本] TN(True Negatives): 真的 ...

  10. es_python_操作

    获取es索引 https://www.itranslater.com/qa/details/2583886977221264384