1912: [Apio2010]patrol 巡逻

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 1034  Solved: 562
[Submit][Status][Discuss]

Description

Input

第一行包含两个整数 n, K(1 ≤ K ≤ 2)。接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n)。

Output

输出一个整数,表示新建了K 条道路后能达到的最小巡逻距离。

Sample Input

8 1
1 2
3 1
3 4
5 3
7 5
8 5
5 6

Sample Output

11

HINT

10%的数据中,n ≤ 1000, K = 1; 
30%的数据中,K = 1; 
80%的数据中,每个村庄相邻的村庄数不超过 25; 
90%的数据中,每个村庄相邻的村庄数不超过 150; 
100%的数据中,3 ≤ n ≤ 100,000, 1 ≤ K ≤ 2。

Source

Solution

发现加边实际上就是形成环,使得一条路径可以直接绕回来,不用原路返回

那么K==1的时候,边一定用来连最长的路径(树的直径)那么DFS出即可

K==2的时候同理,不过需要次短,同样DFS,对第一次DFS求得的路径做些修改,置成-1就可以

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
#define maxn 100010
int n,k,ans,zj,s;
struct EdgeNode{int next,to,len;}edge[maxn<<];
int head[maxn],cnt=;int road[maxn]={},croad[maxn]={};
void add(int u,int v,int w)
{
cnt++;
edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].len=w;
}
void insert(int u,int v,int w) {add(u,v,w); add(v,u,w);}
int DFS(int now,int fa)
{
int maxd=,cmaxd=;
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=fa)
{
int len=DFS(edge[i].to,now)+edge[i].len;
if (len>maxd) cmaxd=maxd,maxd=len,croad[now]=road[now],road[now]=i;
else if (len>cmaxd) cmaxd=len,croad[now]=i;
}
if (maxd+cmaxd>zj) zj=maxd+cmaxd,s=now;
// printf("%d %d %d %d\n",now,fa,maxd,cmaxd);
return maxd;
}
int main()
{
n=read(); k=read();
for (int u,v,i=; i<=n-; i++) u=read(),v=read(),insert(u,v,);
DFS(,); ans=*(n-)-zj+;
if (k==)
{
for (int i=road[s]; i; i=road[edge[i].to]) edge[i].len=edge[i^].len=-;
for (int i=croad[s]; i; i=road[edge[i].to]) edge[i].len=edge[i^].len=-;
zj=; DFS(,); ans=ans-zj+;
}
printf("%d\n",ans);
return ;
}

自己的代码写炸了,不知为何..

【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)的更多相关文章

  1. 图论--树的直径--DFS+树形DP模板

    #include <iostream> #include <cstring> using namespace std; //maxv:源点能到的最远点,maxdis:最远点对应 ...

  2. [BZOJ 1912] patrol 巡逻

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1912 Algorithm: K=0:res=(n-1)*2   每条边恰好走2遍 K=1 ...

  3. 【HDOJ2196】Computer(树的直径,树形DP)

    题意:给定一棵N个点树,询问这个树里面每个点到树上其他点的最大距离. n<=10000 思路:设f[u,1],f[u,2]为以U为根向下的最长与次长,g[u,1],g[u,2]为从哪个儿子转移来 ...

  4. bzoj 1912 : [Apio2010]patrol 巡逻 树的直径

    题目链接 如果k==1, 显然就是直径. k==2的时候, 把直径的边权变为-1, 然后在求一次直径. 变为-1是因为如果在走一次这条边, 答案会增加1. 学到了新的求直径的方法... #includ ...

  5. bzoj 3302&2447&2103 树的双中心 树形DP

    题目: 题解: bzoj 3302 == 2447 == 2103 三倍经验 首先我们考虑枚举两个中心的位置,然后统计答案. 我们发现,一定有一部分点离第一个中心更近,另一部分点离第二个中心更近 如果 ...

  6. bzoj 4871: [Shoi2017]摧毁“树状图”【树形dp】

    做不来--参考https://www.cnblogs.com/ezyzy/p/6784872.html #include<iostream> #include<cstdio> ...

  7. BZOJ_3124_[Sdoi2013]直径_树形DP

    BZOJ_3124_[Sdoi2013]直径_树形DP Description 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵 ...

  8. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  9. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

随机推荐

  1. Codevs 1860 最大数 string大法好,STL万岁。。

    题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...

  2. [py]文件 字符串 列表特例

    文件 readlines 列表 readline 字符串 read 字符串   列表---拆分---小列表   f=file('test.log','r') for line in f.readlin ...

  3. 读懂IL代码就这么简单(三)完结篇

    一 前言 写了两篇关于IL指令相关的文章,分别把值类型与引用类型在 堆与栈上的操作区别详细的写了一遍 这第三篇也是最后一篇,之所以到第三篇就结束了,是因为以我现在的层次,能理解到的都写完了,而且个人认 ...

  4. .net破解一(反编译,反混淆-剥壳)

    大家好,前段时间做数据分析,需要解析对方数据,而数据文件是对方公司内部的生成方式,完全不知道它是怎么生成的. 不过还好能拿到客户端(正好是C#开发)所以第一件事就是用Reflector编译,但是没有想 ...

  5. [leetcode]算法题目 - Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  6. Linux之我见

    Linux哲学之美 linux就像是一个哲学的最佳实践.如果非要对它评价,我真的不知道该怎么赞叹,我只能自豪的说着:“linux的美丽简直让人沉醉.” 我只能说是我处在linux学习的修炼之路上的一个 ...

  7. javascript正则表达式验证IP,URL

    验证IP function isIP(ipstr){ var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[ ...

  8. 【JavaEE企业应用实战学习记录】struts2登录

    <%-- login.jsp Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To chang ...

  9. C++类功能扩展预留五招

    第一招虚函数 通过派生类来进行功能扩展是基本的面向对象的方式,这种方式大如下: class base { public: virtual ~base(){} virtual void fun() { ...

  10. $(document).ready()和window.onload的区别

    来源于: The window.onload event fires when a document is completely downloaded to the browser. This mea ...