链接:https://ac.nowcoder.com/acm/contest/5670/B
来源:牛客网
题目描述
Mr. W got a new graph with N vertices and N - 1 edges. It’s a connected graph without cycles. Each edge should have an ugly value. To make the graph more beautiful, Mr. W hope you can help him modify it. You can delete or add one edge with an ugly value at a time and you can do it as many times as you want. But the following conditions should be satisfied at any time:
- The graph is connected.
- For each cycles in the graph, the XOR sum of all ugly values in the cycle is 0.
Mr. W wants to know the minimum sum of ugly values of all edges in the graph.
输入描述:
The first line contains one integerN(2 \leq N \leq 100000)N(2≤N≤100000).
Next N - 1 lines each contains three integers U, V, W (0 \leq U,V < N, 0 \leq W < 2^{30})U,V,W(0≤U,V<N,0≤W<2
30
), denoting an edge between vertex U and V with ugly value W.
输出描述:
One integer, the minimum sum of ugly values of all edges in the graph.
示例1
输入
复制
6
0 1 1
1 2 4
1 3 3
0 4 5
0 5 2
输出
复制
7
题意:
给一张树,可以加边或者删边,但要满足环上边的权值异或为0,且图连通。求最后得到的最小边权和。
思路:
就是CF888G的变形,改一下就能过了。
模拟一下可以发现,不管怎么加边和删边,两个点的的路径异或值不变,也就是两点加边的值不变。只需要记录根节点到每个节点的路径异或值,就可以得到任意两点连边对应权值。
于是题目就变形成了每个点有一个权值,两点连边的边权为权值异或,求这个完全图的最小生成树。
而对于异或最小生成树,
这是一张完全图,当然是不能用kruskal或者prim。
但是最小生成树除了这两个算法,还有一个叫做boruvka的算法,大致思路是维护每个连通块连出的最短边,然后每个连通块择优,选上最短边。
用在本题上,我们可以建立01字典树,那么每次对于连出的0出边和1出边,各有一个连通块,要连通这两个连通块,则需要从左子树(右子树)中枚举一个点,然后再利用01字典树的性质,得到在另一半对应的最小值。
因为对于两个点异或值,肯定是高位相等的越多,值越小,所以从01字典树最高位往下走是符合boruvka算法的
对于每个分出两个子树的点都这样处理,这类似分治的过程,复杂度肯定是够的。
#include<iostream>
#include<vector>
#include<map>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
int L[maxn * 40],R[maxn * 40];
int ch[maxn * 40][2],tot;
int n,root;
int a[maxn];
void Insert(int &now,int x,int dep) {
if(!now) now = ++tot;
L[now] = min(L[now],x);R[now] = max(R[now],x);
if(dep < 0) return;
int bit = a[x] >> dep & 1;
Insert(ch[now][bit],x,dep - 1);
}
ll query(int now,int val,int dep) {
if(dep < 0) return 0;
int bit = (val >> dep) & 1;
if(ch[now][bit]) return query(ch[now][bit],val,dep - 1);
return query(ch[now][bit ^ 1],val,dep - 1) + (1 << dep);
}
ll dfs(int now,int dep) {
if(dep < 0) return 0;
if(R[ch[now][0]] && R[ch[now][1]]) {
ll mi = INF;
for(int i = L[ch[now][0]];i <= R[ch[now][0]];i++) {
mi = min(mi,query(ch[now][1],a[i],dep - 1));
}
return dfs(ch[now][0],dep - 1) + mi + dfs(ch[now][1],dep - 1) + (1 << dep);
}
if(R[ch[now][0]]) return dfs(ch[now][0],dep - 1);
if(R[ch[now][1]]) return dfs(ch[now][1],dep - 1);
return 0;
}
void solve() {
sort(a + 1,a + 1 + n);
memset(L,0x3f,sizeof(L));
for(int i = 1;i <= n;i++) Insert(root, i, 30);
printf("%lld\n",dfs(root,30));
}
int head[maxn],nex[maxn],to[maxn],val[maxn],TOT;
void add(int x,int y,int z) {
to[++TOT] = y;
nex[TOT] = head[x];
val[TOT] = z;
head[x] = TOT;
}
void DFS(int x,int fa,ll sum) {
a[x] = sum;
for(int i = head[x];i;i = nex[i]) {
int v = to[i],w = val[i];
if(v == fa) continue;
DFS(v,x,sum ^ w);
}
}
int main()
{
scanf("%d",&n);
for(int i = 1;i < n;i++) {
int x,y,z;scanf("%d%d%d",&x,&y,&z);
x++;y++;
add(x,y,z);add(y,x,z);
}
DFS(1,-1,0);
solve();
return 0;
}