原题链接: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. 【go语言】2.1.3 函数的定义和使用

    在 Go 语言中,函数是一种代码抽象和复用的方式.函数可以接受参数,执行特定的操作,并返回结果. 函数的定义 函数的定义以 func 关键字开始,后面跟着函数名.参数列表.返回值列表(可选)以及函数体 ...

  2. Go的函数定义

    格式: 1 func function_name( [parameter list] ) [return_types] { 2 函数体 3 } 注释: func:函数由 func 开始声明 funct ...

  3. 青少年CTF平台 Web签到

    题目说明 Web一星简单题,Web签到. 直接启动环境,等待30秒左右访问题目链接. 做题过程 进入后,题目好像没有告诉我们什么有用的信息, F12,看遍了题目源码,也没有发现flag,正当我怀疑这个 ...

  4. Mybatis开发中的常用Maven配置

    Mybatis导入Maven配置 <!-- MyBatis导入 --> <dependency> <groupId>org.mybatis</groupId& ...

  5. 带你走进数仓大集群内幕丨详解关于作业hang及残留问题定位

    本文分享自华为云社区<[带你走进DWS大集群内幕]大集群通信:作业hang.残留问题定位>,作者: 雨落天穹丶. 前言: 测试过程中,我们会遇到这样一种情况,我的作业都执行很久了,为啥还不 ...

  6. [Arch小贴士]在这里记录一些自己使用的小技巧

    哈喽!Arch 目录 0x00 设置开机自启动软件 首先 最后 0x01 ASLR开关 0x00 设置开机自启动软件 首先 首先进入目录/usr/share/applications,找到你要的那个软 ...

  7. Java Maven POM配置参考

    介绍 什么是POM? POM代表"项目对象模型".它是一个名为pom.XML的文件中保存的Maven项目的XML表示. 快速概览 这是一个直接位于POM项目元素下的元素列表.请注意 ...

  8. 以程序员为视角,关于商品详情API接口的说明

    ​ 商品详情API接口是现代电商平台中非常重要的一部分,它提供了获取淘宝商品详细信息的能力.作为一个程序员,了解如何调用这个API接口是非常关键的. 首先,我们需要明确的是,API接口是应用程序与服务 ...

  9. 【python技巧】替换文件中的某几行

    [python技巧]替换文件中的某几行 1. 背景描述 最近在写一个后端项目,主要的操作就是根据用户的前端数据,在后端打开项目中的代码文件,修改对应位置的参数,因为在目前的后端项目中经常使用这个操作, ...

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

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