原题链接:CF1592C. Bakry and Partitioning

题意:

给定一个\(n\)个点,\(n - 1\)条边的树,并且每个点都有权值\(w_i\),让你最少割掉一条边最多割掉\(k - 1\)条边使得划分后的子树异或和相等。

思路:

  1. 首先根据异或的交换律结合律得知:如果所有点的权值\(\sum\nolimits_{i = 1}^n w_i = 0\),那么我们随意切一刀就行,所以这种情况下必然满足题意。

  2. 再根据异或的一个性质应用,\(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的更多相关文章

  1. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  2. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  5. 測試大型資料表的 Horizontal Partitioning 水平切割

    FileGroup 檔案群組 :一個「資料庫(database)」可對應一或多個 FileGroup,一個 FileGroup 可由一或多個 file (.ndf) 構成. FileGroup 可讓 ...

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

  7. LintCode Palindrome Partitioning II

    Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...

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

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

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

随机推荐

  1. 暑假刷题记 B

    动态规划 字符串 杂题 A:Animals and Puzzle B:Vanya and Treasure 根号分治. 实际上是从 \((1, 1)\) 先找一个 \(1\),再找一个 \(2\dot ...

  2. 清理MySQL的binlog日志

    前言 MySQL的binlog是以二进制形式打印的日志,没设置自动删除的话,时间长了就会占用大量存储空间.删除MySQL的binlog日志有两种方法:自动删除和手动删除. 自动删除 永久生效:修改My ...

  3. Ubuntu安装后续工作

    更新源: sudo gedit /etc/apt/sources.list 清华的源 deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial ma ...

  4. quarkus数据库篇之四:本地缓存

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<quarkus数据库篇> ...

  5. 零代码基础,一分钟教你快速搭建微信ChatGPT机器人!

    本教程收集于:ChatGPT聊天机器人搭建全攻略汇总:精心整理 Github登录账号后,先Forck下仓库:https://github.com/zhayujie/chatgpt-on-wechat ...

  6. 1.15 自实现GetProcAddress

    在正常情况下,要想使用GetProcAddress函数,需要首先调用LoadLibraryA函数获取到kernel32.dll动态链接库的内存地址,接着在调用GetProcAddress函数时传入模块 ...

  7. Markdown初识

    1.标题 一级标题 ctrl+1......六级标题 ctrl+6 2.字体 加粗 ctrl+B 斜体 ctrl+I 下划线 ctrl+ U 3.引用 大于号加任意键 4.分割线 "---& ...

  8. 【krpano】KRPano测试开发专用浏览器

    KRPano技术解密网站:http://www.krpano.tech 在开发KRPano项目时候,每次测试时均需要打开测试服务器,或者上传到空间中才能进行测试,十分不方便. 因此本站提供了免费的KR ...

  9. Codechef - Longest AND Subarray(位运算)

    题目大意   给定一个正整数N,其序列为[1, 2, 3, ..., N],找到一个长度最大的连续子列,使得其所有元素取与运算的结果为正(最终输出只需要输出最大长度即可). 思路   刚开始可能并不好 ...

  10. destoon关于archiver归档的性能优化

    今天在处理一个项目时候发现archiver单个模块归档超过百万数据,打开速度就特慢,所以打开archiver下index.php文件进行分析,发现有句sql作怪 1 $result = $db-> ...