题目链接: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的更多相关文章

  1. codeforces 709E E. Centroids(树形dp)

    题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  2. Codeforces Round #474-E(树形dp)

    一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...

  3. Choosing Capital for Treeland CodeForces - 219D (树形DP)

    传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional  ...

  4. Codeforces 431C - k-Tree - [树形DP]

    题目链接:https://codeforces.com/problemset/problem/431/C 题意: 定义一个 $k$ 树,即所有节点都有 $k$ 个儿子节点,相应的这 $k$ 条边的权重 ...

  5. Codeforces 161D(树形dp)

    \(dp[v][k]\)代表以\(v\)的子树为起点,以点\(v\)为终点长度为\(k\)的方案有多少种. 转移只需将子树加和:计算\(ans\)由两部分组成,一是\(dp[v][k]\),另一部分是 ...

  6. CF708C Centroids(树形DP)

    发现变重心就是往重心上割,所以\(\text{up and down}\),一遍统计子树最大\(size\),一遍最优割子树,\(down\),\(up\)出信息,最后\(DFS\)出可行解 #inc ...

  7. 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 ...

  8. Codeforces 743D:Chloe and pleasant prizes(树形DP)

    http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. js 函数节流和防抖

    js 函数节流和防抖 throttle 节流 事件触发到结束后只执行一次. 应用场景 触发mousemove事件的时候, 如鼠标移动. 触发keyup事件的情况, 如搜索. 触发scroll事件的时候 ...

  2. 第一章:systemverilog简介

    1.为何要学systemverilog ..... 2.systemverilog起源 ..... 3.systemverilog标准历程 systemverilog3.0 for 综合 system ...

  3. mac 终端path配置出错,命令无法使用

    mac 命令行中修改path的时候,不小心把path修改错了,而且还 source 了,然后发现只能使用 cd 命令,ls vi 命令都不能使用了. 解决办法,执行下面的语句 export PATH= ...

  4. BootStrap学习01框架搭建

    中文文档:https://v3.bootcss.com/css/ 开发工具 WebStorm 一.新建项目08bootstrap 引入bootstrap-3.3.7,引入jQuery,引入holder ...

  5. HDU 2462 The Luckiest number

    The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...

  6. CD(01背包)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

  7. UVA10673 上下界问题

    #include <iostream> #include<cstdio> using namespace std; #define LL long long LL a,b,m, ...

  8. 解决Genymotion运行Android 5.0一直卡在开机界面

    在一些机器,启动genymotion 的android5.0版模拟器时,会卡在启动界面,一直启动不了. 这是因为要求的开启虚拟选项没有打开,在第一次启动时,会有提示,但可能大家没有注意(我也没注意到, ...

  9. 【SPOJ694&705】Distinct Substrings(后缀数组)

    题意:求一个字符串的不相同的子串个数 n<=1000 思路:这是一道论文题 ..]of longint; n,i,m,ans,v,cas:longint; ch:ansistring; proc ...

  10. 【Java源码】集合类-ArrayDeque

    一.类继承关系 ArrayDeque和LinkedList一样都实现了双端队列Deque接口,但它们内部的数据结构和使用方法却不一样.根据该类的源码注释翻译可知: ArrayDeque实现了Deque ...