CF1656E Equal Tree Sums 题解
思路分析
自认为是一道很好的构造题,但是我并不会做。
看了题解后有一些理解,在这里再梳理一遍巧妙的思路。
我们先来看这样的一张图:

我们发现当去掉叶子节点的父亲时,剩下树的价值和等于叶子节点的价值和,显然全是正的不太可能。
对于叶子节点我们不妨设他们的权值都是 \(1\) ,此时若删去最大的祖先节点,可以发现第二层节点的权值差正好和其儿子的个数差相同。
于是我们初步推断出权值大小可能和儿子的个数有着密切的关系。(实际就是这样)
上面的问题好像没有什么突破口了,我们转而考虑如何确定一个节点的正负问题。
考虑到删掉一个节点后每个子树权值大小相等。猜测出每个节点的权值的正负是交错进行的。
直接用官方题解的话说:
黑白染色,黑点为正,白点为负,绝对值等于当前节点的度数。
这还是很好理解的,就上图而言,当删掉一个节点后,它的权值分配给它所有的子树。
所以每个块的的权值和为 \(1\) 或 \(-1\) 。
Code
代码的话直接模拟即可。
#include <bits/stdc++.h>
#define file(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define Enter putchar('\n')
#define quad putchar(' ')
namespace IO {
template <class T> inline void read(T &a);
template <class T, class ...rest> inline void read(T &a, rest &...x);
template <class T> inline void write(T x);
template <class T, class ...rest> inline void write(T x, rest ...a);
}
#define N 100005
int T, n, x, y, in[N], ans[N];
std::vector <int> dis[N];
inline void dfs(int now, int father, int flag) {
ans[now] = in[now] * flag;
for (int t : dis[now]) {
if (t == father) continue;
dfs(t, now, -flag);
}
}
signed main(void) {
IO::read(T);
while (T--) {
IO::read(n);
for (int i = 1; i <= n; i++) dis[i].clear(), in[i] = 0;
for (int i = 1, x, y; i < n; i++) {
IO::read(x, y);
in[x] ++;
in[y] ++;
dis[x].push_back(y);
dis[y].push_back(x);
}
dfs(1, 0, 1);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
Enter;
}
}
namespace IO {
template <class T> inline void read(T &a) {
T s = 0, t = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-')
c = getchar();
if (c == '-')
c = getchar(), t = -1;
while (c >= '0' && c <= '9')
s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
a = s * t;
}
template <class T, class ...rest> inline void read(T &a, rest &...x) {
read(a); read(x...);
}
template <class T> inline void write(T x) {
if (x == 0) putchar('0');
if (x < 0) putchar('-'), x = -x;
int top = 0, sta[50] = {0};
while (x)
sta[++top] = x % 10, x /= 10;
while (top)
putchar(sta[top] + '0'), top --;
return ;
}
template <class T, class ...rest> inline void write(T x, rest ...a) {
write(x); quad; write(a...);
}
}
CF1656E Equal Tree Sums 题解的更多相关文章
- [LeetCode] Equal Tree Partition 划分等价树
Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...
- 663. Equal Tree Partition 能否把树均分为求和相等的两半
[抄题]: Given a binary tree with n nodes, your task is to check if it's possible to partition the tree ...
- [LeetCode] 663. Equal Tree Partition 划分等价树
Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...
- 【LeetCode】663. Equal Tree Partition 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- Lintcode375 Clone Binary Tree solution 题解
[题目描述] For the given binary tree, return a deep copy of it. 深度复制一个二叉树,给定一个二叉树,返回一个他的克隆品. [题目链接] www. ...
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- [CF1204E]Natasha,Sasha and the Prefix Sums 题解
前言 本文中的排列指由n个1, m个-1构成的序列中的一种. 题目这么长不吐槽了,但是这确实是一道好题. 题解 DP题话不多说,直接状态/变量/转移. 状态 我们定义f表示"最大prefix ...
- csps-s模拟测试62,63Graph,Permutation,Tree,Game题解
题面:https://www.cnblogs.com/Juve/articles/11631298.html permutation: 参考:https://www.cnblogs.com/clno1 ...
随机推荐
- 用 getElementsByTagName() 来获取,父元素指定的子元素
1. html 结构 <ul> <li>知否知否,应是等你好久11</li> <li>知否知否,应是等你好久11</li> <li&g ...
- contact 和 footer 的页面制作
1. html 结构 <section id="contact"> <div class="contact-form bg-primary p-2&qu ...
- VMware虚拟机无法安装Win11解决方法 (暂时全网最全方案)
目录 1.现象 1.蓝屏重启 2.如下图示,无法启动 2.解决方案 2.1 Hyper-V方案 2.2 禁用 Device Guard(系统:win11) 2.3 升级虚拟机VMware pro的版本 ...
- Failed to load resource: the server responded with a status of 404 ()
今天遇到了一个一开始感觉很莫名其妙的报错 在编写页面的时候把原先写在html页面里的js代码单独拿出来做成一个JavaScriptUtil文件,放在了和html页面同一个目录下.运行之后在对应的页面c ...
- Linux下的计划任务—crontab
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 来自:Linux就该这么学 crond 是linux下用 ...
- 百度SEO算法技术的局限性,怎么做才能有收益
不知道大家有没有发现,我们使用百度的频率在减少,就算有时遇到一些问题,需要用百度来寻找答案,也会经常遇到搜索不到答案的情况.到底是出了什么问题?难道网络上的资源不够丰富了?浩如烟海的互联网,居然搜索不 ...
- netty系列之:netty中常用的字符串编码解码器
目录 简介 netty中的字符串编码解码器 不同平台的换行符 字符串编码的实现 总结 简介 字符串是我们程序中最常用到的消息格式,也是最简单的消息格式,但是正因为字符串string太过简单,不能附加更 ...
- .netcore6.0自己配置swagger
环境:.net core6.0 一.安装依赖包:Swashbuckle.AspNetCore 二.右击项目->属性->生成->输出,勾选文档文件,然后配置文件生成路径,注意是相对路径 ...
- cut-列过滤
列过滤命令. 语法 cut [选项] 要过滤的字符串 选项 -f 以字段为单位进行分割 -c 以字符为单位进行分割 -b 以字节为单位进行分割 -d 以分割符为单位进行分割,分隔符可以是"冒 ...
- 解决 AMD 笔记本不插电源时屏幕偏暗的问题
办法:关掉显卡设置里的 Vari-Bright 选项 最近换了锐龙版的笔记本,用着还不错,就是不插电源时看屏幕亮度不太适应,整体偏暗,有点费眼,差点就觉得 AMD 不 Yes 了.然后网上一顿找,发现 ...