http://codeforces.com/problemset/problem/700/B

题目大意:给你一棵树,给你k个树上的点对。找到k/2个点对,使它在树上的距离最远。问,最大距离是多少?

思路:我们可以把树上的这个分成两个集合,然后两边的点的数目相等。符合这个条件的就是树的重心,所以我们只需要找到树的中心就行啦。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
/*
题目大意:
给你一棵树,给你k个树上的点对。找到k/2个点对,使它在树上的距离最远。
问,最大距离是多少?
*/
const int maxn = + ;
int n, k;
vector<int> G[maxn];
bool vis[maxn];
int dp_cnt[maxn]; int dfs_cnt(int u, int fa){
int cnt = vis[u];
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa) continue;
cnt += dfs_cnt(v, u);
}
return dp_cnt[u] = cnt;
} void dfs_ce(int u, int fa, int &ce, int &maxcnt, int treesize){
int tmp = treesize - dp_cnt[u];
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa) continue;
tmp = max(tmp, dp_cnt[v]);
dfs_ce(v, u, ce, maxcnt, treesize);
}
if (maxcnt > tmp){
maxcnt = tmp; ce = u;
}
} LL ans;
void dfs(int u, int fa, int len){
if (vis[u]) {
ans = 1LL * len + ans;
}
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa) continue;
dfs(v, u, len + );
}
} int main(){
scanf("%d%d", &n, &k);
for (int i = ; i <= k * ; i++){
int u; scanf("%d", &u);
vis[u] = true;
}
for (int i = ; i < n; i++){
int u, v; scanf("%d%d", &u, &v);
G[u].pb(v); G[v].pb(u);
}
int treesize = dfs_cnt(, -);
int cetroid, maxcnt = maxn;
dfs_ce(, -, cetroid, maxcnt, treesize);
dfs(cetroid, -, );
printf("%lld\n", ans);
return ;
}

树形dp Codeforces Round #364 (Div. 1)B的更多相关文章

  1. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  2. 树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates

    Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. ana ...

  3. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  4. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  5. 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...

  6. DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

    题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...

  7. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  8. dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...

  9. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

随机推荐

  1. openstack私有云布署实践【4.2 上层代理haproxy+nginx配置 (办公网测试环境)】

    续上一节说明 一开始我也是使用haproxy来做的,但后来方式改了,是因为物理机controller的高配置有些浪费,我需要1组高可用的上层nginx代理服务器来实现其它域名80代理访问,很多办公网测 ...

  2. Kali Rolling 下利用rtl-sdr跟踪飞机路线

    环境 kali rolling 状态:未升级发行版,仅用 "apt-get upgrade " 命令更新了软件 原料 RTL-SDR电视棒一个,芯片是RTL2832U 原理 我国民 ...

  3. HDU 1335 Basically Speaking(进制转换)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1335 Problem Description The Really Neato Calculator ...

  4. (转)eclipse自动补全的设置

    如果你用过Visual Studio的自动补全功能后,再来用eclipse的自动补全功能,相信大家会有些许失望. 但是eclipse其实是非常强大的,eclipse的自动补全没有VS那么好是因为ecl ...

  5. mysql 取出的日期数据缺少时分秒

    项目使用的数据库是mysql  持久层框架是hibernate  利用SQLQuery 获取日期的时候缺少时分秒   将 .addScalar("dealTS",StandardB ...

  6. Llinux环境下编译并使用OpenCV

    http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html http://stacko ...

  7. 13.hibernate的native sql查询(转自xiaoluo501395377)

    hibernate的native sql查询   在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hi ...

  8. deb

    1.APT方式 (1)普通安装:apt-get install softname1 softname2 …; (2)修复安装:apt-get -f install softname1 softname ...

  9. 在block函数中规避错误信息 "capturing self strongly in this block is likely to lead to a retain cycle”

    以形如 _fontValueChangedBlock = ^(){ [self.fontSmallButton addTarget:self action:@selector(btnFontSmall ...

  10. 从MySQL全库备份中恢复某个库和某张表【转】

    从MySQL全库备份中恢复某个库和某张表 一.全库备份-A [root@mha2 backup]#mysqldump -uroot -p123456 --default-character-set=u ...