牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)
牛客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字典树+启发式合并)的更多相关文章
- 牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
传送门:https://www.nowcoder.com/acm/contest/203/I 题意: 求每个大都会到最近的一个大都会的距离. 思路: 把每个大都会设为起点,跑一遍最短路.在跑最短路的时 ...
- 牛客网国庆集训派对Day6 题目 2018年
链接:https://www.nowcoder.com/acm/contest/206/A来源:牛客网 Birthday 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576 ...
- 牛客网国庆集训派对Day5 题目 2018年
链接:https://www.nowcoder.com/acm/contest/205/L来源:牛客网参考博客:https://blog.csdn.net/HTallperson/article/de ...
- 牛客网国庆集训派对Day4题目 2018年
链接:https://www.nowcoder.com/acm/contest/204/A来源:牛客网 深度学习 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他 ...
- 牛客网国庆集训派对Day3题目 2018年
链接:https://www.nowcoder.com/acm/contest/203/D来源:牛客网 Shopping 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...
- 牛客练习赛37C 筱玛的迷阵探险 双向搜索+字典树
题意 筱玛是个快乐的男孩子.寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险.迷阵可以看做一个的矩阵A,每个格子上有一个有一个数Ai,j.入口在左上角的(1,1)处,出口在右下角的(n,n)处.每一 ...
- 牛客15334 Easygoing Single Tune Circulation(后缀自动机+字典树)
传送门:Easygoing Single Tune Circulation 题意 给定n个字符串 s[i],再给出m个查询的字符串 t[i],问 t[i] 是否为某个 s[i] 循环无限次的子串. 题 ...
- 18/9/9牛客网提高组Day1
牛客网提高组Day1 T1 中位数 这好像是主席树??听说过,不会啊... 最后只打了个暴力,可能是n2logn? 只过了前30% qwq #include<algorithm> #in ...
- 国庆集训 Day1 T2 生成图 DP
国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...
随机推荐
- JavaScript--查看代码运行效率console.time()与console.timeEnd()用法
程序运行时间计算: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Java练习 SDUT-2749_区域内点的个数
区域内点的个数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数 ...
- 01-常见Dos命令、Java历史、Java跨平台、配置Path环境变量、第一个HelloWorld例子
常见Dos命令 dir: 列出当前目录下的文件以及文件夹 md: 创建目录 rd: 删除目录 cd: 进入指定目录 del: 删除文件 copy: 复制文件 xcopy: 复制目录 tree: 列出目 ...
- spingboot项目在windows环境中运行时接收参数及日志中文乱码
1.logback.xml配置 appender中添加 <param name="Encoding" value="UTF-8" /> <co ...
- 突破!阿里云CDN实现毫秒级全网刷新
通常在某网站使用了CDN节点来实现内容分发加速后,当源站内容更新的时候,CDN刷新系统会通过提交刷新请求将CDN节点上的指定缓存内容强制过期.当用户访问的时候,CDN节点将回源获取最新内容返回给用户, ...
- BUAA 623 Chem is Try!
http://oj55.bianchengla.com/problem/623/ 好久没写过题解了,昨天做了一道挺恶心的题目,贴一下代码上来.看了一下提交状况,好像我的代码挺短的了,至少我找不到比我短 ...
- 详解如何在Laravel中增加自定义全局函数
http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...
- Getting started with the basics of programming exercises_2
1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...
- Websocket 简单对话:静态网页与pycharm对话
WebSocket websocket 是一种在单个Tcp连接上进行双全工通信的协议.websocket通信协议于2011年被IETF定为标准RFC6455,并 由RFc7936补充规范.WebSoc ...
- centos安装hdp
1. 准备6和7的 YUM源包 1.1 centos 下载后解压到同一个目录 http://mirrors.163.com/centos/6/isos/x86_64/CentOS-6.9-x86_64 ...