Park Visit

hdu4607:http://acm.hdu.edu.cn/showproblem.php?pid=4607

题意:给你一棵树,树上每条边的权值是1,然后然你选择m个点,求遍历m个点的最小花费。

题解:这一题要用到树的性质。首先可以想到的是第一次肯定要选择一条最长的路径,也就是树的直径。至于其余的点怎么考虑呢?画个图可以知道,剩余的点的所产生的花费就是剩余点数的两倍,所以本题的关键就是求树的直径。求树的直径,是有方式的,就是两遍DFS,第一遍DFS找到一个当前点的最远的点,然后从这个最远的点开始,再进行一次DFS,得到的就是树的直径,这里距不证明了。可以用反正法。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 const int N=1e5+10;
 8 vector<int>Q[N];
 9 int n,m,k,u,v;
10 int maxn,start;
11 void DFS(int u,int f,int len){
12     if(len>maxn){
13         maxn=len;
14         start=u;
15     }
16     for(int i=0;i<Q[u].size();i++){
17         if(f!=Q[u][i]){
18             DFS(Q[u][i],u,len+1);
19         }
20     }
21 }
22 int main(){
23     int cas;
24     scanf("%d",&cas);
25     while(cas--){
26           scanf("%d%d",&n,&m);
27          for(int i=1;i<=n;i++)
28              Q[i].clear();
29          for(int i=1;i<n;i++){
30             scanf("%d %d",&u,&v);
31             Q[u].push_back(v);
32             Q[v].push_back(u);
33          }
34         start=0,maxn=0;
35          DFS(1,0,0);
36          maxn=0;
37          DFS(start,0,0);
38          for(int i=1;i<=m;i++){
39             scanf("%d",&k);
40             if(k<=maxn+1)
41                 printf("%d\n",k-1);
42             else
43                 printf("%d\n",maxn+(k-1-maxn)*2);
44          }
45     }
46 }
View Code

 

转载于:https://www.cnblogs.com/chujian123/p/3915988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文行程: |日期|行程安排|酒店住宿| |---|-------|-------| |7.12|晚11:45广州起飞|无| |7.13|14:45到奥克兰—入住酒店Skycity|Skycity| |7.13|下午酒店休息,晚上出门楼下吃饭逛街|Skycity| |7.14|前往霍比特村奥克兰一日游,回Skycity|Skycity| |7.15|游览Skycity和奥克兰市区|Skycity| |7.16|前往基督城 入住基督城酒店|基督城酒店| |7.16|基督城半日游,公园|基督城酒店| |7.17|基督城一日游,晚上住基督城|基督城酒店| |7.18|前往皇后镇,湖边游玩 ,入住里斯酒店|里斯酒店| |7.19|拍照游览山川|里斯酒店| |7.20|游览雪山|里斯酒店| |7.21|箭镇|里斯酒店| |7.22|皇后镇再游 ,入住里斯酒店|里斯酒店| |7.23|回奥克兰,逛顾城故居;入住Skycity,休息;楼下逛;|Skycity| |7.24|休闲游,晚上10点奥克兰飞回广州|无| 英文行程: |Date|Itinerary|Hotel Accommodation| |---|---------|-------------------| |7.12|Depart from Guangzhou at 11:45pm|None| |7.13|Arrive in Auckland at 2:45pm - Check in at Skycity Hotel|Skycity Hotel| |7.13|Rest at the hotel in the afternoon, dinner and shopping in the evening|Skycity Hotel| |7.14|One day trip to Hobbiton Village, return to Skycity Hotel|Skycity Hotel| |7.15|Visit Skycity and Auckland city|Skycity Hotel| |7.16|Travel to Christchurch - Check in at Christchurch Hotel|Christchurch Hotel| |7.16|Half-day tour and park visit in Christchurch|Christchurch Hotel| |7.17|One day tour in Christchurch, spend the night in Christchurch|Christchurch Hotel| |7.18|Travel to Queenstown, Lake visit and check in at The Rees Hotel|The Rees Hotel| |7.19|Photo tour of the mountains and scenery|The Rees Hotel| |7.20|Visit the Snowy Mountains|The Rees Hotel| |7.21|Visit Arrowtown|The Rees Hotel| |7.22|Return to Queenstown, further visit and spend the night at The Rees Hotel|The Rees Hotel| |7.23|Return to Auckland - Visit Gu Cheng residence, check in at Skycity Hotel, rest and explore downstairs|Skycity Hotel| |7.24|Leisure activities - Depart Auckland at 10pm for Guangzhou|None|

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值