题目链接: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+10, maxn = 1e3+20, mod = 1e9+7, 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] = 1;for(int i = 0; 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/2) submax[u] = sz[u];
}
void dfs(int u,int f,int pre) {int flag = 1;for(int i = 0; i < G[u].size(); ++i) {int to = G[u][i];if(to == f) {int temp = n - sz[u];if(temp>n/2 && temp - pre > n/2) flag = 0;}else {if(sz[to] > n/2 && sz[to] - submax[to] > n/2) flag = 0;}}ans[u] = flag;for(int i = 0 ; i < G[u].size(); ++i) {int to = G[u][i];if(to == f) continue;int temp;if(n - sz[to] <= n/2) 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 = 1; i < n; ++i) {scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);}dfs(1,0);dfs(1,0,0);for(int i = 1; i <= n; ++i) cout<<ans[i]<<" ";return 0;
}

转载于:https://www.cnblogs.com/zxhl/p/6574809.html

Codeforces 709E. Centroids 树形DP相关推荐

  1. CodeForces - 766E  (树形dp+二进制)

    C - Mahmoud and a xor trip CodeForces - 766E 补题的时候碰见的一道题目,感觉思路非常巧妙就记录下来了. 题目大意:给出n个点,每个点有一个点权,求所有路径的 ...

  2. Appleman and Tree CodeForces - 461B(树形dp)

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  3. Codeforces 494D Birthday 树形dp (看题解)

    Birthday 没想到平方和能在树上dp出来的... 知道了如何转移, 那么就很好写了... #include<bits/stdc++.h> #define LL long long # ...

  4. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

  5. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

  6. 【Codeforces Round #614(div2)】E-Xenon's Attack on the Gangs(树形dp)

    一.题目链接 https://codeforces.com/contest/1293/problem/E 二.题意 给n个结点,n-1条无向边.即一棵树.我们需要给这n-1条边赋上0~n-2不重复的值 ...

  7. 【Christmas Game】【CodeCraft-21 and Codeforces Round #711 (Div. 2)】【Nim-博弈】【树形DP】【拆分树】

    CodeCraft-21 and Codeforces Round #711 (Div. 2) Christmas Game Nim-博弈 树形DP 拆分树 牛客链接 https://ac.nowco ...

  8. Codeforces 1088E Ehab and a component choosing problem(树形DP)

    Codeforces 1088E Ehab and a component choosing problem(树形DP) 题意 给一棵树,要求从中选一些联通分量,使得平均联通分量重量总和最大.如果有多 ...

  9. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

最新文章

  1. javascript之prototype总结常用方法
  2. R语言-包的安装、载入及使用方法
  3. python ggplot画等值线图,是否可以在Python ggplot上绘制多折线图?
  4. 'Push segues can only be used when the source controller is managed by an instance of UINavigationC
  5. MVC防止CSRF攻击
  6. 亲戚再也看不见我一个人食吉野家了
  7. ces测试数据测试ces测试数据测试ces测试数据测试ces测试数据测试ces测试数据测试
  8. python3 for_Python3: for 表达式
  9. oracle替代变量输出,【Oracle】替代变量
  10. 番石榴的EventBus –简单的发布者/订阅者
  11. WINCE字体动态加载
  12. 网上看到的一个百度实习生笔试题
  13. Redis笔记4-持久化方案
  14. 华为背锅?微博大V质疑华为P30 Pro拍月亮造假 公司称误导观众已开除
  15. SpringBoot:第一篇 新建spring boot 应用
  16. 类序列化之后保存在COOKIES里
  17. 暴风影音 去广告 方法
  18. WIN10配置JAVA环境变量
  19. 【原创】StackDocklet 完美攻略
  20. 苹果电脑如何双开微信

热门文章

  1. Linux安装ntp同步时间
  2. nginx轻松搭建自己的flv流媒体服务器
  3. android用户界面编程技巧——更新请求
  4. 动态实例分割SOLOv2,更快更强更精准!
  5. AI医疗 | 新开源计算机视觉技术用于新生儿胎龄估计
  6. ImageNet又被Long-Short Transformer 霸榜!
  7. 前端与java绘制三维图_使用HTML5 Canvas绘制三维波浪图算法和实现
  8. by group 累加中文字段_EF 求和 GroupBy多个字段
  9. java显示文件_java 显示文件夹结构
  10. 120余家自动驾驶公司的行业汇总