Step1:首先定义一个点的权值为与其相连边的异或和。那么修改一条路径,权值改变的只有两个端点。边权都为0和点权都为0实质相同。

Step2:那么现在和树的结构就没有什么关系了。每次选两个点,然后同时异或上一个值。求最小次数。

Step3:首先权值为0的不用修改了,贪心先把权值一样的两两分组。那么就会剩下至多15个数。因为只有15个数,考虑状压。如果几个数异或值为0,那么显然存在方案n-1次全部消完。那么就可以子集转移。

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 1e5 + 10, Limit = 1 << 15;
int val[N], cnt[15], res[Limit], f[Limit];
int main() {
int n;
read(n);
for (int i = 1; i < n; i++) {
int x, y, z; read(x), read(y), read(z);
x++; y++; val[x] ^= z; val[y] ^= z;
}
for (int i = 1; i <= n; i++) cnt[val[i]]++;
int ans = 0, S = 0;
for (int i = 1; i <= 15; i++)
ans += cnt[i] / 2, S |= (cnt[i] & 1) << (i - 1);
for (int i = 1, cnt; i < Limit; i++) {
cnt = 0;
for (int j = 1; j <= 15; j++)
if ((i >> (j - 1)) & 1) res[i] ^= j, cnt++;
f[i] = cnt - 1;
}
for (int i = 1; i < Limit; i++)
if (!res[i])
for (int j = i; j; j = (j - 1) & i)
if (!res[j]) f[i] = min(f[i], f[j] + f[i ^ j]);
printf("%d\n", ans + f[S]);
return 0;
}

AT3913 XOR Tree(巧妙转换+状压dp)的更多相关文章

  1. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  2. 【思维题 状压dp】APC001F - XOR Tree

    可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...

  3. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  4. Codeforces 429C Guess the Tree(状压DP+贪心)

    吐槽:这道题真心坑...做了一整天,我太蒻了... 题意 构造一棵 $ n $ 个节点的树,要求满足以下条件: 每个非叶子节点至少包含2个儿子: 以节点 $ i $ 为根的子树中必须包含 $ c_i ...

  5. HDU 6984 - Tree Planting(数据分治+状压 dp)

    题面传送门 傻逼卡常屑题/bs/bs,大概现场过得人比较少的原因就是它比较卡常罢(Fog 首先对于这样的题我们很难直接维护,不过注意到这个 \(n=300\) 给得很灵性,\(k\) 比较小和 \(k ...

  6. 「算法笔记」状压 DP

    一.关于状压 dp 为了规避不确定性,我们将需要枚举的东西放入状态.当不确定性太多的时候,我们就需要将它们压进较少的维数内. 常见的状态: 天生二进制(开关.选与不选.是否出现--) 爆搜出状态,给它 ...

  7. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  8. 【62测试】【状压dp】【dfs序】【线段树】

    第一题: 给出一个长度不超过100只包含'B'和'R'的字符串,将其无限重复下去. 比如,BBRB则会形成 BBRBBBRBBBRB 现在给出一个区间[l,r]询问该区间内有多少个字符'B'(区间下标 ...

  9. POJ 1185 炮兵阵地(状压DP)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26426   Accepted: 10185 Descriptio ...

随机推荐

  1. SAS学习笔记23 线性回归、多元回归

    线性回归 由样本资料计算的回归系数b和其他统计量一样,存在抽样误差,因此,需要对线性回归方程进行假设检验 1.方差分析 2.t检验 相关系数的假设检验 相关系数(correlation coeffic ...

  2. MyBatis_01 框架

    Mybatis概述 Mybatis是什么 Mybatis是一个持久层框架.   Mybatis的作用 Mybatis是一个持久层框架,当然作用就是操作数据库的(增删改查).   为什么需要学习Myba ...

  3. 动手篇:简单的注册界面与防SQL注入(续)

    注册前先判断用户名是否已经存在,通过if (SqlHelper.Exists(strSql))查询判断是否已经存在.没存在则进行一个数据插入数据库操作 string strSql1 = string. ...

  4. 怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数

    1. 向上取整使用: Math.ceil() Math.ceil(0.1); Math.ceil(1.9); 2. 向下取整使用: Math.floor() Math.floor(0.1); Math ...

  5. Android 自定义控件之 日期选择控件

    效果如下: 调用的代码: @OnClick(R.id.btn0) public void btn0() { final AlertDialog dialog = new AlertDialog.Bui ...

  6. edk2中子目录介绍

    edk2中子目录介绍 来源 https://blog.csdn.net/rikeyone/article/details/80760204 参考:https://github.com/tianocor ...

  7. javascript的装载和执行

    前言 为什么要采用js来create一个script标签,设置src然后append到head中,而不是直接使用script标签,这样不是更简单点吗? javascript的装载和执行 首先,我想说一 ...

  8. java轻松玩转localdatetime

    废话不多说,直接上代码 //时间戳转LocalDateTime public static LocalDateTime getLocalDateTime(long timestamp) { Insta ...

  9. flex 布局方式

    开始啦 1. flex-direction 有关主轴的对齐方式 column 自上到下 row 自左到右 -->默认值 row-reverse 自右到左 column-reverse 自下到上 ...

  10. .Net给图片加水印,并解决“无法从带有索引像素格式的图像创建Graphics对象”问题

    using (Image img = Image.FromFile(savePath)) { //如果原图片是索引像素格式之列的,则需要转换 if (img.PixelFormat!=null) { ...