题意+题解:

  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. 3610:20140827:161308.483 No active checks on server: host [192.168.1.10] not found

    3610:20140827:161308.483 No active checks on server: host [192.168.1.10] not found

  2. 【Linux】rsync模板配置问题

    ------------------------------------------------------------------------------------------------- | ...

  3. C语言字符串结束符“\0”

    介绍 '\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以这么写.'\0'就是 字符串结束标志. '\0'是转译字符,意思是告诉编译器,这不是字符0,而是空字符.空字符\0对应 ...

  4. printf函数输出格式总结

    printf函数格式 函数描述: printf("[格式化字符串]", [参数链表]); 函数声明: int printf(const char *format, ...) ; 输 ...

  5. Python安装教程之anaconda篇

    [导读]我们知道,Python的功能非常强大.那么对于迫切想学习Python的新手同学来说,第一件事情可能需要了解python是什么?能用来做什么?语法结构是怎样的?这些我们几句话很难介绍清楚,后续会 ...

  6. pytorch——预测值转换为概率,单层感知机

    softmax函数,可以将算出来的预测值转换成0-1之间的概率形式 导数的形式 import torch import torch.nn.functional as F x=torch.tensor( ...

  7. SQL Server 日志收缩方法

    在日常运维中,有时会遇到"The transaction log for database 'xxxx' is full due to 'ACTIVE_TRANSACTION'." ...

  8. Oracle 常用命令大全(持续更新)

    数据库 ----数据库启动 & 关闭 启动数据库 SQL> startup nomount; SQL> alter database mount; SQL> alter da ...

  9. java虚拟机入门(一)-jvm基础

    转行学java之前,总是听着大佬们说着java像个渣男一样可以跨平台,一次编译到处运行,瞬间,我就坚定了学java的信念,哎呀妈呀,得劲.真的学java之后,好像渣男也不是那么好学的,尤其这货的必杀技 ...

  10. ByteDance 2019 春招题目

    牛客网字节跳动笔试真题:https://www.nowcoder.com/test/16516564/summary 分了 2 次做,磕磕碰碰才写完,弱鸡悲鸣. 1. 聪明的编辑 题目:Link . ...