牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)

题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和

题解:

根据异或的性质我们可以得到 \(sum_{(u, v)}=sum_{(u, 1)} \bigoplus 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("*********\n")
#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)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
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("\n");
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\n",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 ? '\n' : ' ');
}
return 0;
}

牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)的更多相关文章

  1. 牛客2018国庆集训派对Day3 I Metropolis 多源最短路径

    传送门:https://www.nowcoder.com/acm/contest/203/I 题意: 求每个大都会到最近的一个大都会的距离. 思路: 把每个大都会设为起点,跑一遍最短路.在跑最短路的时 ...

  2. 牛客网国庆集训派对Day6 题目 2018年

    链接:https://www.nowcoder.com/acm/contest/206/A来源:牛客网 Birthday 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576 ...

  3. 牛客网国庆集训派对Day5 题目 2018年

    链接:https://www.nowcoder.com/acm/contest/205/L来源:牛客网参考博客:https://blog.csdn.net/HTallperson/article/de ...

  4. 牛客网国庆集训派对Day4题目 2018年

    链接:https://www.nowcoder.com/acm/contest/204/A来源:牛客网 深度学习 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他 ...

  5. 牛客网国庆集训派对Day3题目 2018年

    链接:https://www.nowcoder.com/acm/contest/203/D来源:牛客网 Shopping 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...

  6. 牛客练习赛37C 筱玛的迷阵探险 双向搜索+字典树

    题意 筱玛是个快乐的男孩子.寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险.迷阵可以看做一个的矩阵A,每个格子上有一个有一个数Ai,j.入口在左上角的(1,1)处,出口在右下角的(n,n)处.每一 ...

  7. 牛客15334 Easygoing Single Tune Circulation(后缀自动机+字典树)

    传送门:Easygoing Single Tune Circulation 题意 给定n个字符串 s[i],再给出m个查询的字符串 t[i],问 t[i] 是否为某个 s[i] 循环无限次的子串. 题 ...

  8. 18/9/9牛客网提高组Day1

    牛客网提高组Day1 T1 中位数 这好像是主席树??听说过,不会啊... 最后只打了个暴力,可能是n2logn? 只过了前30%  qwq #include<algorithm> #in ...

  9. 国庆集训 Day1 T2 生成图 DP

    国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...

随机推荐

  1. nodeJs学习-13 router

    const express=require('express'); var server=express(); //目录1:/user/ var routeUser=express.Router(); ...

  2. python字符串、元组常用操作

    常用字符串操作函数: #Author:CGQ name="I \tam ChenGuoQiang" print(name.capitalize())#首字母大写,其他都小写 pri ...

  3. 解决ArcMap绘图错误

    这几天在用ArcMap对shapefile做矢量化的过程中,遇到一件特别蛋疼的事,ArcMap竟然会出现绘图错误.如下所示: 纠结了许久之后,终于在Esri社区找到了解决办法:帮助文档中说 “检查属性 ...

  4. SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...

  5. TensorFlow的 卷积层

    用 TensorFlow 做卷积 让我们用所学知识在 TensorFlow 里构建真的 CNNs.在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias.这在很大程度上 ...

  6. @atcoder - Japanese Student Championship 2019 Qualification - F@ Candy Retribution

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 请找到满足以下条件的长度为 N 的非负整数序列 A1, A2, ...

  7. 注意 Laravel 清除缓存 php artisan cache:clear 的一个坑

    Laravel 的命令 php artisan cache:clear 用来清除各种缓存,如页面,Redis,配置文件等缓存,它会清空 Redis 数据库的全部数据,比如默认使用的 Redis 的 数 ...

  8. ViewPager封装工具类: 轻松实现APP导航或APP中的广告栏

    相信做app应用开发的,绝对都接触过ViewPager,毕竟ViewPager的应用可以说无处不在:APP第一次启动时的新手导航页,APP中结合Fragment实现页面滑动,APP中常见的广告栏的自动 ...

  9. AUTOSSH设置ssh隧道,实现反向代理访问内网主机

    内网主机上配置: autossh -M -CNR :localhost: ubuntu@123.207.121.121 可以实现将访问主机123.207.121.121的1234端口的数据,通过隧道转 ...

  10. python -- 类中--内置方法

    isinstance 和  issubclass isinstance(obj,b)  检查是否obj是否是类b的对象 class A(object):pass class B(A):pass b=B ...