原题链接: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. 从module_init看内核模块

    开篇 module_init是linux内核提供的一个宏, 可以用来在编写内核模块时注册一个初始化函数, 当模块被加载的时候, 内核负责执行这个初始化函数. 在编写设备驱动程序时, 使用这个宏看起来理 ...

  2. 2023-08-06:小青蛙住在一条河边, 它想到河对岸的学校去学习 小青蛙打算经过河里 的石头跳到对岸 河里的石头排成了一条直线, 小青蛙每次跳跃必须落在一块石头或者岸上 给定一个长度为n的数组ar

    2023-08-06:小青蛙住在一条河边, 它想到河对岸的学校去学习 小青蛙打算经过河里 的石头跳到对岸 河里的石头排成了一条直线, 小青蛙每次跳跃必须落在一块石头或者岸上 给定一个长度为n的数组ar ...

  3. 【算法】用c#实现德州扑克卡牌游戏规则

    德州扑克是一种牌类游戏,可多人参与,它的玩法是,玩家每人发两张底牌,桌面依次发5张公共牌,玩家用自己的两张底牌和5张公共牌自由组合,按大小决定胜负. 使用c#完成功能Hand()以返回手牌类型和按重要 ...

  4. SpringBoot 测试实践 - 2:单元测试与集成测试

    单元测试 vs. 集成测试 只编写单测,无法测试方法之间的集成情况,而且某些需求可能会修改多个方法,这可能会影响方法对应的单测,涉及到大量的相关单测的修改,这样的维护成本很高 可以把重心放在完善集成测 ...

  5. 银河麒麟SP2 auditd服务内存泄露问题

    这几天遇到基于海光服务器的银河麒麟V10 SP2版本操作系统出现内存无故增长问题. 排查发现auditd服务,占用了大量内存. 我的环境是银河麒麟V10 SP2 524,audit版本audit-3. ...

  6. C++算法之旅、04 基础篇 | 第一章

    常用代码模板1--基础算法 - AcWing ios::sync_with_stdio(false) 提高 cin 读取速度,副作用是不能使用 scanf 数据输入规模大于一百万建议用scanf 快速 ...

  7. 多层前馈神经网络及BP算法

    一.多层前馈神经网络 首先说下多层前馈神经网络,BP算法,BP神经网络之间的关系.多层前馈[multilayer feed-forward]神经网络由一个输入层.一个或多个隐藏层和一个输出层组成,后向 ...

  8. Chrome 手机端网页如何使用开发者模式

    chrome 手机端网页如何调试 在Chrome手机端,你可以使用Chrome开发者工具来调试网页.下面是一些步骤: 首先,确保你的手机已经开启开发者模式.打开USB调试功能或可以通过USB连接或无线 ...

  9. MongoDB 中使用 explain 分析创建的索引是否合理

    MongoDB 中如何使用 explain 分析查询计划 前言 查询计划 explain explain 1.queryPlanner 2.executionStats 3.allPlansExecu ...

  10. Redis系列23:性能优化指南

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...