题目直通车: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. Jmeter mysql性能测试

    一:首先建立jdbc connection configuration,设置参数如图 1.variable name 参数名称,与后面的sample中设置的variable name一致.含义为:通过 ...

  2. 自动化测试(三)如何用python写个双色球

    写一个程序,输入N就产生N条双色球号码 红球  6     01-33 蓝球  1     01-16 产生的双色球号码不能重复,写到一个文件里面,每一行是一条 红球: 01 03 05 07 08  ...

  3. 获取访问者ip及其所在城市

    原本使用新浪Api,然后发现不行了,以下小编重新查找了几个,个人推荐太平洋的接口 1.首先获取真实ip $ip = $_SERVER['REMOTE_ADDR']; 2.要知道的Api接口 几个接口$ ...

  4. Eclipse安装使用

    1.访问https://www.eclipse.org/downloads/下载最新的Eclipse工具包或者百度通过其他路径下载需要的版本 2.下载完成后将压缩包进行解压的得到相应的文件 3.进入解 ...

  5. Python学习-day18 Web框架

    众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  6. Escape From The Earth 逃离地球

    1.对Tags进行管理 设置一个全局的类,类似如下: public class Tags:MonoBehaviour{ public const string player="Player& ...

  7. hdu 1085 给出数量限制的母函数问题 Holding Bin-Laden Captive!

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  8. hihoCoder #1902 字符替换

    解法 这题比赛时过的人很多,我却没思路,糊里糊涂写了个强联通分量,得了 80 分. 这题思路是这样的. 一个替换操作可以看做一个有向边,所以题目实际上给出了一个有向图 $G$,一个节点代表一个字母. ...

  9. SPOJ 1825 Free Tour | 终极之树分治

    求树上最长路径使得经过的拥挤节点个数不超过K //欢迎访问这个博客!http://www.cnblogs.com/luyouqi233/p/8036828.html #include<cstdi ...

  10. 【BZOJ 2458 最小三角形】

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1551  Solved: 549[Submit][Status][Discuss] Descripti ...