AGC001 C - Shorten Diameter【枚举】
一开始没看到要保证最后是树……所以一定要从叶子开始删
枚举重心,如果k是偶数,那么按当前重心提起来deep大于k/2的全都要切掉,这样枚举重心然后取min即可
奇数的话就是枚举直径中间的边,然后从两边的点分别dfs删点
#include<iostream>
#include<cstdio>
using namespace std;
const int N=5005;
int n,k,m,h[N],cnt,ans=1e9,sm;
struct qwe
{
int ne,no,to;
}e[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].no=u;
e[cnt].to=v;
h[u]=cnt;
}
void dfs(int u,int fa,int de)
{
if(de>m)
sm++;
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fa)
dfs(e[i].to,u,de+1);
}
int main()
{
n=read(),k=read(),m=k/2;
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y),add(y,x);
}
if(k&1)
{
for(int i=1;i<=cnt;i+=2)
{
sm=0;
dfs(e[i].no,e[i].to,0);
dfs(e[i].to,e[i].no,0);
ans=min(ans,sm);
}
}
else
{
for(int i=1;i<=n;i++)
{
sm=0;
dfs(i,0,0);
ans=min(ans,sm);
}
}
printf("%d\n",ans);
return 0;
}
AGC001 C - Shorten Diameter【枚举】的更多相关文章
- Shorten Diameter
Shorten Diameter Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 600 points P ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- ACC 001 C - Shorten Diameter 图论
题目: Problem Statement Given an undirected tree, let the distance between vertices \(u\) and \(v\) be ...
- AtCoderACGC001C Shorten Diameter
Description: 给定一个\(n\)个点的树,要求删去最少的点使树的致直径不超过k Solution: 如果\(k\)为偶数,对于最终状态一定是以每一个点为根任何点的深度不能超过\(k/2\) ...
- 【AtCoder】AGC001
AGC001 A - BBQ Easy 从第\(2n - 1\)个隔一个加一下加到1即可 #include <bits/stdc++.h> #define fi first #define ...
- AGC001[BCDE] 题解
A没意思 F太难 所以大概近期的AGC题解都是BCDE的 然后特殊情况再说 开始刷AGC的原因就是计数太差 没有脑子 好几个学长都推荐的AGC所以就开始刷了 = = 大概两天三篇的速度?[可能也就最开 ...
- A*G#C001
AGC001 A BBQ Easy 贪心. https://agc001.contest.atcoder.jp/submissions/7856034 B Mysterious Light 很nb这个 ...
- 【AGC板刷记录】
这个帖子,是在自己学知识点累了的时候就看看\(AGC\)的题目来休息. 而且白天上课可以做( AGC-001 \(A\ BBQ Easy\) 考虑从小到大排,相邻两个取为一对. BBQ Easy #i ...
- RE:从零开始的AGC被虐(到)生活(不能自理)
RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...
随机推荐
- PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...
- sql性能分析语句
SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical_reads N'物理读取总次数' ,tota ...
- html-webpack-plugin 中使用 title选项设置模版中的值无效
原文地址:https://segmentfault.com/q/1010000004555431 webpack.config.js配置: var webpack = require("we ...
- String类中的equals是如何重写的
我们知道String中的equals方法是被重写过的,因为object的equals方法是比较的对象的内存地址,而String的equals方法比较的是对象的值. 首先几个知识点: 基本数据类型==比 ...
- 【Codeforces】Gym 101608G WiFi Password 二分+线段树
题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...
- tf.stack和tf.unstack
import tensorflow as tf a = tf.constant([1,2,3]) b = tf.constant([4,5,6]) c1 = tf.stack([a,b],axis=0 ...
- STM32中IO口的8中工作模式
该文摘自:http://blog.csdn.net/kevinhg/article/details/17490273 一.推挽输出:可以输出高.低电平,连接数字器件:推挽结构一般是指两个三极管分别受两 ...
- Could not load the "xxx.png" image referenced from a nib in the bundle with identifier "com.xxxx"
打印台logs: Could not load the "xxx.png" image referenced from a nib in the bundle with iden ...
- python 动态添加属性及方法及“__slots__的作用”
1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...
- 基于OpenCV的面部交换
需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...