BZOJ1912【Tree DP】
来源:互联网 发布:周易参同契 知乎 编辑:程序博客网 时间:2024/06/12 01:00
/* I will wait for you */#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<algorithm>#include<iostream>#include<fstream>#include<vector>#include<queue>#include<deque>#include<set>#include<map>#include<string>#define make make_pair#define fi first#define se secondusing namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;const int maxn=100010;const int maxm=300000;const int maxs=100;const int INF=1<<29;const int P=1000000007;const double error=1e-9;struct edge{ int u,v,w,next; }e[maxm];int n,k,cnt=0,ma=0,mi=0,p;int head[maxn],fi[maxn],se[maxn],q[maxn],dis[maxn],per[maxn];void addedge(int u,int v){e[cnt]=(edge){u,v,1,head[u]},head[u]=cnt++;e[cnt]=(edge){v,u,1,head[v]},head[v]=cnt++;}void bfs(int s){memset(dis,-1,sizeof(dis));int he=1,ta=1;q[1]=p=s;dis[s]=0,per[s]=-1;while(he<=ta){int u=q[he++];for(int i=head[u],v;i!=-1;i=e[i].next)if(dis[v=e[i].v]==-1) per[v]=i,dis[v]=dis[u]+1,q[++ta]=v;}for(int i=1;i<=n;i++) if(dis[i]>dis[p]) p=i,ma=dis[i];}void dfs(int u,int p){fi[u]=se[u]=0;for(int i=head[u],v;i!=-1;i=e[i].next) if((v=e[i].v)!=p){dfs(v,u);int w=e[i].w+fi[v];if(w>fi[u]) se[u]=fi[u],fi[u]=w;else if(w>se[u]) se[u]=w;}if(mi<fi[u]+se[u]) mi=fi[u]+se[u];}int main(){scanf("%d%d",&n,&k);memset(head,-1,sizeof(head));for(int i=1,u,v;i<n;i++) scanf("%d%d",&u,&v),addedge(u,v);bfs(1),bfs(p);for(int i=per[p];i!=-1;i=per[e[i].u]) e[i].w=e[i^1].w=-1;dfs(1,0);printf("%d\n",k==1?2*(n-1)-ma+1:2*(n-1)-ma-mi+2);return 0;}
0 0
- BZOJ1912【Tree DP】
- 【bzoj1912】[Apio2010] patrol 巡逻 树形dp
- [bzoj1912][Apio2010]patrol 巡逻(树上dp)
- [BZOJ1912][Apio2010]patrol 巡逻(dfs+并查集+树形dp)
- tree dp
- BZOJ1912[Apio2010]patrol 巡逻
- 【BZOJ1912】【codevs1613】巡逻
- 【bzoj1912】[Apio2010]patrol 巡逻
- bzoj1912: [Apio2010]patrol 巡逻
- bzoj1912 [Apio2010]patrol 巡逻
- 树状dp Tree of Tree
- 【DP】 hdu4359 Easy Tree DP?
- hdu4359 Easy Tree DP? dp
- HDU 1561(Tree Dp)
- hdu 3586 tree dp
- Tree Cutting (树形DP)
- Tree Dp专题整理
- BZOJ1131【Tree DP】
- AWK的基本使用
- 运行oracle 的aix、linux主机改ip规范
- 分类器训练的过拟合问题和处理方法【未整理完成】
- 关于分别使用CSS3和JQ制作IOS7开关按钮的方法
- 重新温习内部类的收获
- BZOJ1912【Tree DP】
- 对象的初始化过程(包含父类的子类)
- 通过UrlRewriter配置MVC4伪静态
- 位运算+BFS
- 万达“飞凡”电商面对五大特殊难题
- 用map读取文本参数的Configprameter.h
- 这电脑几乎神了:内置蒸汽机不愁电费
- 上海通报“200余人围堵派出所打砸警车”事件
- 【美军机“迫降”台湾背后不可告人的企图 】