题目链接: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. 生成优惠券,并将优惠券存入Mysql

    #coding:utf-8 import random import string import MySQLdb def gen_charint(filename, width =4, num=5): ...

  2. 剑指Offer(书):数值的整数次方

    题目:给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 分析: * 要注意以下几点:* 1.幂为负数时,base不能为0,不然求的时候是对 ...

  3. linux 配置文件要不要加上#! /bin/bash

    现在一般的linux系统默认的shell都是bash.所以但很多unix系统可能会用bourne shell.csh或者ksh等来作为用户默认shell 如果你在写shell脚本的时候,用的语法只有b ...

  4. 更换SpringBoot中默认的web容器为jetty

    在pom文件中:<dependency> <groupId>org.springframework.boot</groupId> <artifact>s ...

  5. Golang 编写 Tcp 服务器

    Golang 作为广泛用于服务端和云计算领域的编程语言,tcp socket 是其中至关重要的功能.无论是 WEB 服务器还是各类中间件都离不开 tcp socket 的支持. Echo 服务器 拆包 ...

  6. PS修图知识小结

    PS修图知识小结   [1]人去除红眼. 1.用红眼工具,框选红眼不分. 2.用椭圆选区工具,选择红眼部分 3..创建调整图层,亮度.对比度.加大对比度. 4.选择红眼部分.创建调整图层,可选颜色,减 ...

  7. luogu1447 [NOI2010]能量采集

    考虑暴力,答案显然是 \(\sum_{i=1}^n\sum_{j=1}^m(2(\gcd(i,j)-1)+1)=\sum_{i=1}^n\sum_{j=1}^m(2\gcd(i,j)-1)\). 考虑 ...

  8. 大数据学习——安装zooleeper

    1 alt+p,上传zookeeper-3.4.5.tar.gz 2 解压安装包 ,安装在apps目录下 tar -zxvf zookeeper-3.4.5.tar.gz -C apps 3 删除zo ...

  9. 亲历dataguard的一些经验问答题

    问题1:是否log_archive_dest_n=service中进程使用lgwr时(如log_archive_dest_2='service=DBSTD LGWR SYNC'),备库就一定要建立st ...

  10. Perforce share workspace between linux and windows

    p4 workspace 跨平台共享 (linux 和 window 共享) 用来存放代码的目录: linux存放代码目录: /home/username/ windows 上map network ...