题意+题解:

  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. python模块/文件/日期时间

    文件操作:

  2. qmake奇淫技巧之字符串宏定义

    阅读本文大概需要3.3分钟 我们平时在软件开发过程中需要定义一些宏,以便在代码中调用,这样每次不需要修改代码,只需要修改外部编译命令就可以得到想要的参数,非常方便 比如我们想在软件介绍中显示软件版本, ...

  3. 【Linux】ethtool 用法

    ethtool命令用于获取以太网卡的配置信息,或者修改这些配置.这个命令比较复杂,功能特别多. 语法 ethtool [ -a | -c | -g | -i | -d | -k | -r | -S | ...

  4. 【Linux】rsync错误解析

    rsync: Failed to exec ssh: No such file or directory (2) rsync error: error in IPC code (code 14) at ...

  5. DOI技术扫盲一

    DOI:  desktop office intergration   桌面办公软件集成简单的将,就是我们在Windows桌面中打开的办公软件(如:word,excel,pdf等等)可以在SAP系统进 ...

  6. 使用Canal作为mysql的数据同步工具

    一.Canal介绍 1.应用场景 在前面的统计分析功能中,我们采取了服务调用获取统计数据,这样耦合度高,效率相对较低,目前我采取另一种实现方式,通过实时同步数据库表的方式实现,例如我们要统计每天注册与 ...

  7. uni-app开发经验分享二: uni-app生命周期记录

    应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...

  8. CTO也糊涂的常用术语:功能模块、业务架构、用户需求、文档……

    功能模块.业务架构.需求分析.用户需求.系统分析.功能设计.详细设计.文档.业务.技术--很多被随口使用的名词,其实是含糊甚至错误的. 到底含糊在哪里,错误在哪里,不仅仅是新手软件开发人员糊涂,许多入 ...

  9. HADOOP 之坑

    hadoop 标签: ubuntu hdfs API 概述 通过API访问hdfs文件系统,出现错误:WARN util.Shell:Did not find winutils.exe:{} HADO ...

  10. 使用“2”个参数调用“SetData”时发生异常:“程序集“

    使用"2"个参数调用"SetData"时发生异常:"程序集"Microsoft.VisualStudio.ProjectSystem.VS. ...