首页 > 其他 > 详细

BZOJ [Cqoi2017] 小Q的棋盘

时间:2018-02-21 18:37:11      阅读:181      评论:0      收藏:0      [点我收藏+]

题解:枚举最后在哪里停止,然后剩下的步数/2

也就是找最大深度

枚举终止位置算是一种思路吧

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=10009;

int n,m;
int maxdep;

int cntedge;
int head[maxn];
int to[maxn<<1],nex[maxn<<1];
void Addedge(int x,int y){
	nex[++cntedge]=head[x];
	to[cntedge]=y;
	head[x]=cntedge;
}

void Dfs(int x,int fa,int dep){
	maxdep=max(maxdep,dep);
	for(int i=head[x];i;i=nex[i]){
		if(to[i]==fa)continue;
		Dfs(to[i],x,dep+1);
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n-1;++i){
		int x,y;
		scanf("%d%d",&x,&y);
		Addedge(x+1,y+1);
		Addedge(y+1,x+1);
	}
	
	Dfs(1,1,0);
	
	if(m<=maxdep)printf("%d\n",m+1);
	else printf("%d\n",min(n,(m-maxdep)/2+maxdep+1));
	return 0;
}

  

BZOJ [Cqoi2017] 小Q的棋盘

原文:https://www.cnblogs.com/zzyer/p/8457010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!