【CodeForces 1592C】Bakry and Partitioning
作者:互联网
链接:
题目大意:
一棵树有 \(n\) 个节点,第 \(i\) 个节点的点权为 \(a_i\)。
你需要回答:能不能选择这棵树中的至少 \(1\) 条边、至多 \(k-1\) 条边删除,使得删除完这些边的树每个联通块的点权异或和相等。
思路:
\(a\oplus a=0\) 真的好用,就可以直接搜索了。
代码:
const int N = 1e5 + 10;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * f;
}
int t, n, k;
int a[N];
int head[N], tot;
struct edge {
int to, nxt;
}e[N << 1];
void Add(int u, int v) {
e[++tot] = (edge) {v, head[u]}, head[u] = tot;
}
int cnt, xsum;
int dfs(int u, int fa) {
int ans = a[u];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
int tmp = dfs(v, u);
if (tmp == xsum) cnt++;
else ans ^= tmp;
}
return ans;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
for (t = Read(); t--; ) {
n = Read(), k = Read();
xsum = cnt = tot = 0;
for (int i = 1; i <= n; i++) xsum ^= a[i] = Read();
memset (head, 0, sizeof head);
for (int i = 1; i < n; i++) {
int u = Read(), v = Read();
Add(u, v), Add(v, u);
}
if (!xsum) puts("YES");
else {
if (k >= 3) {
dfs (1, 0);
if (cnt >= 2) puts("YES");
else puts("NO");
} else puts("NO");
}
}
return 0;
}
标签:Partitioning,Bakry,puts,NO,点权,CodeForces,else,ll,getchar 来源: https://www.cnblogs.com/GJY-JURUO/p/15576345.html