原题链接: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. Linux下实现程序开机自启(rc.local 和 systemctl)

    需求: 实现了一个程序,需要在ubuntu上跑起来.能开机自启,且崩溃了也能自己重启.有两种实现方式,个人推荐第二种. 方式1: 实现方式: 直接将要启动程序的运行命令加在 /etc/rc.local ...

  2. 关于bzoj3306(树)的一些反思

    1.加零大法好,用好没烦恼 2.不要瞎开long long 3.万物皆可变成wa 4.如果超时,试图把循环中中的东西拉到外面来

  3. 如何配置Linux的互信

    如何配置Linux的互信? 这里针对的是root用户的,普通用户家目录/home/test/.ssh. 1.在客户端生成公钥私钥对 [root@auto1 ~]# ssh-keygen -t rsa ...

  4. 如何正确使用:has和:nth-last-child

    我们可以用CSS检查,以了解一组元素的数量是否小于或等于一个数字.例如,一个拥有三个或更多子项的grid.你可能会想,为什么需要这样做呢?在某些情况下,一个组件或一个布局可能会根据子元素的数量而改变. ...

  5. [golang]使用logrus自定义日志模块

    简介 logrus是一个第三方日志库,性能虽不如zap和zerolog,但方便易用灵活.logrus完全兼容标准的log库,还支持文本.JSON两种日志输出格式. 特点 相较于标准库,logrus有更 ...

  6. 基于weave实现docker跨主机网络通信

    前言 IP: 192.168.0.10 192.168.0.11 系统版本:centos 7 weave版本:2.8.1,下载地址:https://git.io/weave docker版本:18.0 ...

  7. Mybatis操作数据库流程源码

    Java操作数据库需要经过3个大步骤: 获取数据库连接 执行SQL语句 关闭数据库连接 Mybatis将这几个步骤进行了封装,将获取数据库连接的给工作交给了SqlSessionFactory,将执行S ...

  8. HDFS核心概念与架构

    HDFS简介 HDFS是Hadoop项目的核心子项目,在大数据开发中通过分布式计算对海量数据进行存储与管理,它基于流数据模式访问和处理超大文件的需求而开发,可以运行在廉价的商用服务器上,为海量数据提供 ...

  9. KRpano项目微信出现"关于潜在的违法或违规内容"

    最近,部分小伙伴反应某些KRPano项目在微信中,出现"关于潜在的未发或违规内容"而无法播放的问题,会看到下图中的提示: 出现原因 这个问题是由于KRPano项目中的webvr.j ...

  10. 使用docker搭建seafile服务器

    工作需要在单位和家里的不同电脑上同步指定文件夹及其内容.对比了一些解决方案,最终还是选择熟悉的seafile来做. 需要按照官方文档进行seafile的安装,选择官方推荐的docker方式快速部署. ...