Codeforces 709E. Centroids 树形DP
题目链接:http://codeforces.com/contest/709/problem/E
题意:
给你一棵树,你可以任删一条边和加一条边,只要使得其仍然是一棵树,输出每个点是否都能成为重心
题解:
做题多动手,画一画;
假设要求当前节点i是否能经过操作成为重心,将它提起来为根,那么可以知道,就是选其中一颗子树提到以根为父亲的情况使得删去根之后每个子树点数和最多不超过n/2
回来看,这颗子树要么来自自身子树下,要么来自父亲节点,这个时候我们dfs出需要的东西,也就是能够提出来的子树点数和最大的且不超过n/2的那颗是多少,再进行判断
我的做法:找到每个节点以下的节点数目和能提出来的最多的数目 以及 最多 和 次多转移来的节点
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, maxn = 1e3+, mod = 1e9+, inf = 2e9; vector<int > G[N];
int ans[N],submax[N],max1[N],max2[N],sz[N],n;
void dfs(int u,int f) {
sz[u] = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) continue;
dfs(to,u);
sz[u] += sz[to];
if(submax[to] > submax[u]) {
submax[u] = submax[to];
max2[u] = max1[u];
max1[u] = to;
}
else if(submax[max2[u]] < submax[to]) max2[u] = to;
}
if(sz[u] <= n/) submax[u] = sz[u];
}
void dfs(int u,int f,int pre) {
int flag = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) {
int temp = n - sz[u];
if(temp>n/ && temp - pre > n/) flag = ;
}
else {
if(sz[to] > n/ && sz[to] - submax[to] > n/) flag = ;
}
}
ans[u] = flag;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) continue;
int temp;
if(n - sz[to] <= n/) temp = n - sz[to];
else {
if(to == max1[u]) temp = max(pre,submax[max2[u]]);
else temp = max(pre,submax[max1[u]]);
}
dfs(to,u,temp);
}
}
int main() {
int u,v;
scanf("%d",&n);
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(,);
dfs(,,);
for(int i = ; i <= n; ++i) cout<<ans[i]<<" ";
return ;
}
Codeforces 709E. Centroids 树形DP的更多相关文章
- codeforces 709E E. Centroids(树形dp)
题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- Codeforces Round #474-E(树形dp)
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- Codeforces 431C - k-Tree - [树形DP]
题目链接:https://codeforces.com/problemset/problem/431/C 题意: 定义一个 $k$ 树,即所有节点都有 $k$ 个儿子节点,相应的这 $k$ 条边的权重 ...
- Codeforces 161D(树形dp)
\(dp[v][k]\)代表以\(v\)的子树为起点,以点\(v\)为终点长度为\(k\)的方案有多少种. 转移只需将子树加和:计算\(ans\)由两部分组成,一是\(dp[v][k]\),另一部分是 ...
- CF708C Centroids(树形DP)
发现变重心就是往重心上割,所以\(\text{up and down}\),一遍统计子树最大\(size\),一遍最优割子树,\(down\),\(up\)出信息,最后\(DFS\)出可行解 #inc ...
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- Codeforces 743D:Chloe and pleasant prizes(树形DP)
http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- Linux用户和组的概念
目 录 第1章 用户和组存在的关系 1 1.1 我们现在所使用的操作系统都是多用户操作系统 1 1.2 id命令查看当前登陆的用户信息 1 1.3 用户UID的分类 1 1.4 ...
- POJ 1905 Expanding Rods (求直杆弯曲拱起的高度)(二分法,相交弦定理)
Description When a thin rod of length L is heated n degrees, it expands to a new length L' = (1+n*C) ...
- c#如何判断textbox中输入的数据是datatime型的
()你好,标准的方法是用一个验证控件:RangeValidator,把type设为DateTime,最大值设为'3000-1-1'或者别的,最小值最好设为'1900-1-1'. ()程序里面自己验证: ...
- 理工个人积分赛最后一场(FZU)G - 五子棋,坑爹的大水题~~
Problem 1490 五子棋 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 五子棋是起源于中国古代的传统黑白 ...
- BZOJ4551 - [TJOI2016]树
Portal Description 给出一棵\(n(n\leq10^5)\)个点的以\(1\)为根的有根树,进行\(Q(Q\leq10^5)\)次操作: 标记一个点\(x\). 询问\(x\)的祖先 ...
- BZOJ1693: [Usaco2007 Demo]Asteroids
n<=500 *n的格子,给m<=10000个格子有人,一炮可以清掉一行或一列的人(莫名的爽!)求最少几炮干掉所有人. 经典二分图模型!行成点,列成点,一个点就连接一行一列,表示这一行或这 ...
- PHP 基础复习 2018-06-17
(1)AJAX Asynchronous JavaScript And XML(异步 JavaScript 及 XML) (2)创建 XMLHttpRequest 对象 不同的浏览器使用不同的方法来创 ...
- eclipse 修改Java代码 不用重新启动tomcat
例子: 1.在tomcat server.xml文件配置加上这句话: <Context debug="0" docBase="C:\Users\admin\Desk ...
- 洛谷——P2256 一中校运会之百米跑
P2256 一中校运会之百米跑 题目背景 在一大堆秀恩爱的**之中,来不及秀恩爱的苏大学神踏着坚定(?)的步伐走向了100米跑的起点.这时苏大学神发现,百米赛跑的参赛同学实在是太多了,连体育老师也忙不 ...
- Spring实战Day2
创建对象之后如何体现对象之间的依赖? Spring容器负责创建Bean和依赖注入,那么Spring是怎么将Bean装配在一起的呢? Spring提供了三种方式装配机制 1.隐式的bean发现机制和自动 ...