This article is copyright ljh2000 and Blog Park is shared, welcome reprint, but must retain this statement, and give the original link, thank you for your cooperation.
this article ljh2000
Author Blog: http://www.cnblogs.com/ljh2000-jump/
Reprint please specify the source, infringement must investigate, retain the final interpretation right! DescriptionInputThe first line consists of two integers n, K (1≤k≤2). Next n–1 line, two integers a, b per line, indicates that there is a road between village A and B (1≤a, b≤n). Outputoutputs an integer that represents the minimum patrol distance that can be reached after a new K-road is created. Sample Input8 1
1 2
3 1
3 4
5 3
7 5
8 5
5 6Sample Output OneHINT
10% of the data, n≤1000, K = 1;
30% of the data, K = 1;
80% of the data, the number of villages adjacent to each village does not exceed 25;
90% of the data, the number of villages adjacent to each village does not exceed 150;
100% of the data, 3≤n≤100,000, 1≤k≤2.
Positive Solution: Tree-shaped DPProblem Solving Report:This question is very interesting, I remember to have done a topic called the Tour, just is the situation of k=1, the result of this question called Patrol xd when considering k=1, our answer is very easy to think of is n-1-the longest chain +1, because if you can add a side, because I want to reduce as much as possible, then I just need to put the longest chain of the end and end, there is no need to go back and forth, plus one is added to this new added edge. but what about the time of k=2? First, think about the longest chain. However, when the k=1 has been used for a period of time, k=2 how to know and k=1 do not overlap it? very simple, we do k=1 after the longest chain on the right side of all modified to 1, and then run the longest chain on it. One might wonder if the 1 side was chosen, is that the equivalent or two times it was chosen? But consider the first time this side of the time to add one, the second time the addition is-1, the equivalent of this side did not produce any contribution. You can draw a drawing and you will find that the equivalent is to turn two interlaced chains into two separate chains. This is an excellent practice. so the final total complexity is O (n).
1 //It's made by ljh20002#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - using namespacestd; -typedefLong LongLL; the Const intINF = (1<< -); - Const intMAXN =100011; - Const intMAXM =200011; - intN,K,ECNT,NEXT[MAXM],TO[MAXM],W[MAXM]; + intf[maxn][2],FIRST[MAXN],G[MAXN],P[MAXN]; - intAns,root,ans,son,son2; + AInlineintGetint () at { - intw=0, q=0;CharC=GetChar (); - while((c<'0'|| C>'9') && c!='-') C=getchar ();if(c=='-') q=1, c=GetChar (); - while(c>='0'&& c<='9') w=w*Ten+c-'0', C=getchar ();returnQ? -w:w; - } -InlinevoidLinkintXintY) {next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=1; } inInlinevoidDfsintXintFA) { - intnow,son=0, son2=0; to for(intI=first[x];i;i=Next[i]) { + intV=to[i];if(V==FA)Continue; DFS (V,X); now=f[v][0]+W[i]; - if(now>f[x][0]) son=g[x],son2=p[x],f[x][1]=f[x][0],f[x][0]=now,g[x]=v,p[x]=i;Else if(now>f[x][1]) f[x][1]=now,son=v,son2=i; the } * if(f[x][0]+f[x][1]>ans) {ans=f[x][0]+f[x][1]; Root=x; Son=son; Son2=Son2;} $ }Panax Notoginseng -InlinevoidWork () { theN=getint (); K=getint ();intx, y; for(intI=1; i<n;i++) {x=getint (); y=getint (); link (x, y); link (y,x);} +Dfs1,0); ans=2* (n1)-ans+1;if(k==1) {printf ("%d", Ans);return ; } A if(f[root][1]>0) {X=son; w[son2]=-1; while(G[x]) {w[p[x]]=-1; x=g[x];} } theX=root; while(G[x]) w[p[x]]=-1, X=g[x]; ans=0; Memset (F,0,sizeof(f)); +Dfs1,0); ans-=ans-1; printf"%d", Ans); - } $ $ intMain () - { - Work (); the return 0; -}
BZOJ1912 [Apio2010]patrol Patrol