题目直通车:http://codeforces.com/problemset/problem/1029/E

思路大意:在树上做dp,依次更新ar数组,ar[i]表示以i为根节点的子树对答案的最小贡献值,依次更新即可,具体细节见代码

/*
13
1 2
1 3
1 4
4 5
4 6
4 7
7 8
7 9
7 10
10 11
10 12
10 13 output:2
*/ #include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const ll mod=1e9;
const int maxn=2e5+;
const int maxm=1e6+;
const double eps=1e-;
int m,n;
int ar[maxn];
vector<int> ve[maxn];
int dis[maxn],ans,arTwo[maxn];
int sum,maxx;
//arTwo[i]表示i节点与1相连时,以i为根的子树中需要与1相连的个数
//ar[i]表示以i为根节点的子树中需要与1相连的个数,即对答案的贡献值的最少值,dp更新ar即可
int func(int u,int pre,int num){
int mid=;
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
if(num==)mid += func(v,u,num-);
else mid+=ar[v];
}
return min(mid, ar[u]);
}
void dfs2(int u,int pre){
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
dfs2(v,u);
}
arTwo[u] = func(u,pre,);
ar[u]=arTwo[u]+;//u节点与1相连的情况
int sumMid=,maxMid=-n;
for(int i=;i<ve[u].size();i++){//u节点的一个子节点与1相连的情况
int v=ve[u][i];
if(v==pre)continue;
sumMid += ar[v];
maxMid = max(maxMid, ar[v]-arTwo[v]-);
}
if(sumMid==)sumMid=,maxMid=;//特判子节点的情况
ar[u] = min(ar[u], sumMid-maxMid);
}
void funcDfs(int u,int pre,int num){
if(num==){
int mid=;
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
mid+=ar[v];
}
ans += min(ar[u], mid);
return ;
}
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
funcDfs(v,u,num-);
}
} int main()
{
scanf("%d",&n);
fill(ar,ar+n+,n);
fill(arTwo,arTwo+n+,n);
for(int i=;i<n;i++){
int a,b;scanf("%d%d",&a,&b);
ve[a].push_back(b);
ve[b].push_back(a);
}
ans=;
dfs2(,-);
funcDfs(,,);
printf("%d\n",ans);
return ;
}

Codeforces 1029 E. Tree with Small Distances(树上dp)的更多相关文章

  1. Codeforces 1111 E. Tree(虚树,DP)

    题意 有一棵树,q个询问,每次询问,指定一个点做树根,再给定k个点,要求把这些点分成不超过m组的方案数,分配的限制是任意两个有祖先关系的点不能分在同一组.题目还保证了所有的询问的k加起来不超过1e5. ...

  2. CF E .Tree with Small Distances(树上的贪心)

    题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...

  3. HDU6446 Tree and Permutation(树上DP)

    传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  4. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  5. 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances

    E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...

  6. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  7. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  8. 【题解】彩色树 51nod 1868 虚树 树上dp

    Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...

  9. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

随机推荐

  1. python学习笔记十六:读取JSON文件

    读取JSON文件可以用JSON库,示例代码: #coding:utf-8 import json with open("msg.json") as jsonfile: json_d ...

  2. WordCount 基础功能

    软测第一次作业 该项目在码云上的地址: https://gitee.com/zhege/WordCount 一,概述 WordCount的基础功能需求分析大致如下:对程序设计语言源文件统计字符数.单词 ...

  3. Python学习1,代码

      看了好久的网上视频,今天尝试着写了一串代码: _author_ = "Happyboy" produce_list = [ ('Iphone',5800), ('Mac Pro ...

  4. day06_01 上节回顾

    1.0 extend 扩展方法及"+"的对比 "+"不会改变数组的内容,而extend会改变数组的内容 2.0 sort扩展sorted() a = [1,2, ...

  5. 孤荷凌寒自学python第二十九天python的datetime.time模块

     孤荷凌寒自学python第二十九天python的datetime.time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.time模块是专门用来表示纯时间部分的类. ...

  6. Windows后续处理工作

    1.远程桌面开启,应预先开启windows防火墙,并放行“远程桌面”(TCP 3389)端口,防止用户自行开启防火墙时操作错误. 2.防火墙高级安全-需放行ICMP 3.补丁更新,更新完重启 4.本地 ...

  7. 1102 Invert a Binary Tree (25 分)(二叉树遍历)

    二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换  所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...

  8. html 网页注意事项

    html 知识总结; 1.内外边距 去掉浮动 *{ margin:0; padding:0; } 2.清除浮动 .clearfix:after { content:""; disp ...

  9. HDU 4655 Cut Pieces 找规律+简单计数

    解法参考:http://blog.csdn.net/a601025382s/article/details/9840125 #include <cstdio> #include <c ...

  10. P3153 [CQOI2009]跳舞

    题目描述 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会”单向喜欢“) ...