CF1592C. Bakry and Partitioning
原题链接:CF1592C. Bakry and Partitioning
题意:
给定一个\(n\)个点,\(n - 1\)条边的树,并且每个点都有权值\(w_i\),让你最少割掉一条边最多割掉\(k - 1\)条边使得划分后的子树异或和相等。
思路:
首先根据异或的交换律结合律得知:如果所有点的权值\(\sum\nolimits_{i = 1}^n w_i = 0\),那么我们随意切一刀就行,所以这种情况下必然满足题意。
再根据异或的一个性质应用,\(x \,\, \oplus \,\, x \,\, \oplus \,\, x \,\, \oplus \,\, = x\),考虑到,设\(\sum\nolimits_{i = 1}^n w_i = x\),那么我们只需要判断这个树是否可以分成三份异或和都可以等于\(0\)的子树即可,如果可以,那么还有检查\(k\)是否大于\(2\)。
令\(f[u]\)表示以\(u\)为根节点的子树的所有结点的异或和,那么\(DFS\)跑树形\(DP\)即可。
// Problem: C. Bakry and Partitioning
// Contest: Codeforces - Codeforces Round #746 (Div. 2)
// URL: https://codeforces.com/contest/1592/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 10, M = N * 2;
int h[N], e[M], ne[M], idx;
int w[N], f[N], cnt = 0;
int n, k;
int xorsum = 0;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs(int u, int fa) {
f[u] = w[u];
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == fa) continue;
int t = dfs(j, u);
f[u] ^= t;
}
if (f[u] == xorsum) cnt++, f[u] = 0;
return f[u];
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
xorsum = 0, memset(h, -1, sizeof h), idx = 0, cnt = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
cin >> w[i];
xorsum ^= w[i];
f[i] = 0;
}
//case 1:如果数组异或和为0,那么随意切一刀
//case 2:x xor x xor x = x设总和为x,那么分成三份每一部分都是x
//令f[u]为以u为根的子树的异或和
for (int i = 0; i < n - 1; i++) {
int u, v; scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
if (xorsum == 0) puts("YES");
else {
dfs(1, -1);
if (cnt >= 3 && k > 2) puts("YES");
else puts("NO");
}
}
return 0;
}
CF1592C. Bakry and Partitioning的更多相关文章
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- 測試大型資料表的 Horizontal Partitioning 水平切割
FileGroup 檔案群組 :一個「資料庫(database)」可對應一或多個 FileGroup,一個 FileGroup 可由一或多個 file (.ndf) 構成. FileGroup 可讓 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- LintCode Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...
- How to Remove Table Partitioning in SQL Server
In this article we will see how we can remove partitions from a table in a database in SQL server. I ...
- Partitioning & Archiving tables in SQL Server (Part 2: Split, Merge and Switch partitions)
Reference: http://blogs.msdn.com/b/felixmar/archive/2011/08/29/partitioning-amp-archiving-tables-in- ...
- Partitioning & Archiving tables in SQL Server (Part 1: The basics)
Reference: http://blogs.msdn.com/b/felixmar/archive/2011/02/14/partitioning-amp-archiving-tables-in- ...
随机推荐
- mysql根据.frm和.ibd文件恢复数据表
忠人之事受人之托 起因是因为一位朋友的数据库服务器被重装了,只剩下一个zbp_post.frm和zbp_post.ibd文件.咨询我能不能恢复,确实我只用过mysqldump这种工具导出数据 然后进行 ...
- 基于AIidlux平台的自动驾驶环境感知与智能预警
自动驾驶汽车又称为无人驾驶车,是一种需要驾驶员辅助或者完全不需操控的车辆. 自动驾驶分级: 自动驾驶系统的组成部分: 环境感知系统: 自动驾驶系统架构: 自动驾驶数据集: Aidlux的作用: YOL ...
- 规范代码编写风格就用 eslint 和 prettier
eslint 可以用于规范我们的编码,使得项目中的代码风格一致,更利于阅读和维护,而 prettier 可以在当我们代码不符合 eslint 规范是进行部分自动修复. eslint 通过 npm in ...
- JS标识符
什么是标识符? 变量名 函数名 属性名都称为标识符. 定义标识符规范如下 1) 标识符只能由字母 数字 下划线 $组成. 2) 标识符不能以数字开头,例如: 1name. 3) 标识符不能实JS中的关 ...
- .NET Core多线 (5) 常见性能问题
合集:.NET Core多线程温故知新 .NET Core多线程(1)Thread与Task .NET Core多线程(2)异步 - 上 .NET Core多线程(3)异步 - 下 .NET Core ...
- quarkus数据库篇之二:无需数据库也能运行增删改查(dev模式)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇内容并非数据库相关的核心知识,而是对一个 ...
- C#利用Refit实现JWT自动续期
前言 笔者之前开发过一套C/S架构的桌面应用,采用了JWT作为用户的登录认证和授权.遇到的唯一问题就是JWT过期了该怎么办?设想当一个用户正在进行业务操作,突然因为Token过期失效,莫名其妙地跳转到 ...
- 【路由器】OpenWrt 配置使用
目录 Web 界面 汉化 root 密码 ssh 升级 LuCI 美化 锐捷认证 MentoHUST MiniEAP 防火墙 开放端口 端口转发 IPv6 USB 安装 USB 驱动 自动挂载 Ext ...
- 解放双手!ChatGPT助力编写JAVA框架
亲爱的Javaer们,在平时编码的过程中,你是否曾想过编写一个Java框架去为开发提效?但是要么编写框架时感觉无从下手,不知道从哪开始.要么有思路了后对某个功能实现的技术细节不了解,空有想法而无法实现 ...
- 优化Redis缓存淘汰机制解决性能测试中报错率逐渐攀升问题
在某个查询场景的性能测试过程中,遇到了一个问题:测试过程中报错率逐渐攀升.进一步检查后发现,在查询业务所在应用的后台日志和平台应用的后台日志中,都出现了用户登录相关的报错信息.经过排查分析,发现了问题 ...