博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CQOI2017】小Q的棋盘
阅读量:4557 次
发布时间:2019-06-08

本文共 1496 字,大约阅读时间需要 4 分钟。

题解

根据题意,不回头是最好的(显然法)

\(dfs\)找到最长链,设长度为\(\mathrm{L}\),然后分类讨论:

  • 如果\(\mathrm{L} > m\),答案就是\(m + 1\)

  • 否则显然可以多走\(m-\mathrm{L} + 1\)步,可以多访问\((m-\mathrm{L} + 1) / 2\)步,于是答案就是

    \(min\{n, \mathrm{L} + (m-\mathrm{L} + 1) / 2\}\)

证明???这一辈子都不可能的

代码

#include
#include
#include
#include
#define RG register#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)#define clear(x, y) memset(x, y, sizeof(x))inline int read(){ int data = 0, w = 1; char ch = getchar(); while(ch != '-' && (!isdigit(ch))) ch = getchar(); if(ch == '-') w = -1, ch = getchar(); while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar(); return data * w;}const int N(110);struct edge { int next, to; } e[N << 1];int head[N], e_num, n, m, maxdep, vis[N];inline void add_edge(int from, int to){ e[++e_num] = (edge) {head[from], to}; head[from] = e_num;}void dfs(int x, int dep){ maxdep = std::max(maxdep, dep), vis[x] = 1; for(RG int i = head[x]; i; i = e[i].next) { int to = e[i].to; if(vis[to]) continue; dfs(to, dep + 1); }}int main(){#ifndef ONLINE_JUDGE file(cpp);#endif n = read(), m = read(); for(RG int i = 1, a, b; i < n; i++) a = read(), b = read(), add_edge(a, b), add_edge(b, a); dfs(0, 1); if(m < maxdep) printf("%d\n", m + 1); else printf("%d\n", std::min(n, maxdep + (m - maxdep + 1) / 2)); return 0;}

转载于:https://www.cnblogs.com/cj-xxz/p/10340700.html

你可能感兴趣的文章
Shell 正则表达式
查看>>
Docker run命令参数整理
查看>>
qt-opencv配置mingw编译器
查看>>
CSS之Medial Queries的另一用法:实现IE hack的方法
查看>>
oo第三单元总结
查看>>
linux-CentOS6.4下安装oracle11g详解
查看>>
实力为王 八年DBA经验谈
查看>>
2-sat 问题 【例题 Flags(2-sat+线段树优化建图)】
查看>>
ext3.2 右击动态添加node的treepanel
查看>>
Database links
查看>>
1035 插入与归并(25 分)
查看>>
STL中排序函数的用法(Qsort,Sort,Stable_sort,Partial_sort,List::sort)
查看>>
如何解决php 生成验证码图片不显示问题
查看>>
PHP,javascript实现大文件上传
查看>>
c#图像处理算法学习
查看>>
webApi之FromUri和FromBody区别
查看>>
【SoapUI】http接口测试
查看>>
各种工具网站
查看>>
数据库事务
查看>>
xe7 控件升级
查看>>