牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)
题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和
题解:
根据异或的性质我们可以得到 (sum_{(u, v)}=sum_{(u, 1)} igoplus sum_{(v, 1)})那么我们可以预处理出所有简单路径上的异或值
对于路径上的最大权值来说,建图后,我们可以将边权进行排序,对于每一个权值为(w_i(1-n))的连通块
现在我们已经得到了当前边权所在的连通块了,所以我们需要计算答案
也就是在这个边权所在的连通块内,计算出这个路径上所有边的异或和的最大值,我们可以用01字典树求出一个联通块内异或和的最大值
由于连通块的权值是从1开始的,所以对于权值为2的连通块来说,他是可以合并权值为1的块,我们用并查集将小的联通块往大的联通块上合并,这个就是启发式合并啦,基于一种贪心的思想
因为最多有n个联通块,合并的复杂度最多就是log(n)然后每次计算答案是log级别的,所以总的复杂度是nloglog级别的
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O = /O
// ____/`---'\____
// .' | |// `.
// / ||| : |||//
// / _||||| -:- |||||-
// | | - /// | |
// | \_| ''---/'' | |
// .-\__ `-` ___/-. /
// ___`. .' /--.-- `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;` _ /`;.`/ - ` : | |
// `-. \_ __ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********
")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
"
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct EDGE {
int u, v, w, nxt;
EDGE() {};
EDGE(int _u, int _v, int _w) {
u = _u;
v = _v;
w = _w;
}
} edge[maxn << 1], G[maxn];
int head[maxn], tot;
bool cmp(EDGE a, EDGE b) {
return a.w < b.w;
}
void add_edge(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
int pre[maxn];
void dfs(int u, int fa) {
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
pre[v] = pre[u] ^ edge[i].w;
dfs(v, u);
}
}
struct Trie {
int id[maxn];
int ch[maxn * 400][2];
int cnt;
void init() {
memset(ch, 0, sizeof(ch));
cnt = 0;
}
void insert(int rt, int x) {
bitset<20> bit;
bit = x;
for(int i = 19; i >= 0; i--) {
if (!ch[rt][bit[i]])
ch[rt][bit[i]] = ++cnt;
rt = ch[rt][bit[i]];
}
}
int query(int rt, int x) {
bitset<20> bit;
bit = x;
int res = 0;
for(int i = 19; i >= 0; i--) {
bool flag = true;
int id = bit[i] ^ 1;
if(!ch[rt][id]) {
flag = false;
id ^= 1;
}
if(flag) res += 1 << i;
rt = ch[rt][id];
}
return res;
}
} trie;
int fa[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if(x != y) {
fa[y] = x;
}
}
int n;
int sz[maxn];
vector<int> vec[maxn];
void init() {
memset(head, -1, sizeof(head));
tot = 0;
trie.init();
trie.cnt = n + 1;
pre[1] = 0;
for(int i = 1; i <= n; i++) {
fa[i] = i;
sz[i] = 1;
vec[i].clear();
trie.id[i] = i;
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
init();
for(int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
G[i] = EDGE(u, v, w);
}
dfs(1, 1);
// for(int i = 1; i <= n; i++) {
// printf("%d ", pre[i]);
// }
// printf("
");
for (int i = 1; i <= n; ++i) {
trie.insert(trie.id[i], pre[i]);
vec[i].push_back(pre[i]);
}
// for(int i=1;i<=n;i++){
// printf("%d
",fa[i]);
// }
sort(G + 1, G + n, cmp);
for (int i = 1; i < n; ++i) {
int x = G[i].u, y = G[i].v;
int fx = find(x), fy = find(y);
if (sz[fx] > sz[fy]) {
swap(x, y);
swap(fx, fy);
}
int res = 0;
for (auto it : vec[fx]) {
res = max(res, trie.query(trie.id[fy], it));
}
for (auto it : vec[fx]) {
trie.insert(trie.id[fy], it);
vec[fy].push_back(it);
}
fa[fx] = fy;
sz[fy] += sz[fx];
printf("%d%c", res, i == n - 1 ? '
' : ' ');
}
return 0;
}