Xor路
Xor路
(xor.pas/c/cpp)128MB1s
给定一棵有N个点和N-1条边的树,请你求出树中的最长路径,以及总共有多少条最长路径。
这里路径长度是用xor定义的,即若经过的边的权值为a1, a2, a3,...,an,则这条路径的总权值为 a1 xor a2 xor a3 ... xor an。
输入格式
第1行为一个正整数 N,为点的个数。
第2行至第N行,每行包含三个正整数x,y,z,表示x和y之间有一条权值为z的边。
输出格式
仅一行,包含两个数字,为最长路径的长度和条数。
样例输入
4
1 2 3
2 4 1
1 3 4
样例输出
7 1
样例解释
2-1-3 这条路径,长度为3 xor 4=7。

————————————————————————————————————————————————
首先xor满足 a&a=0
这样之后我们求两个点之间的路径就可以随便找一个点作为树的跟两个点之间的路径就是两个点到跟的路径的xor
这样问题就转换成了给你n个数求两个点亦或和最大值以及方案数
这个n^2明显会超时 但是利用xor的性质 我们可以利用tire来维护 复杂度nlogn
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=3e5+;
int n,x,y,vis[M],q[M];
LL l[][],h[];
LL T,w,d[M],mx,ans,sum;
int first[M],cnt;
struct node{int to,next; LL w;}e[*M];
void ins(int a,int b,LL w){cnt++; e[cnt].to=b; e[cnt].w=w; e[cnt].next=first[a]; first[a]=cnt;}
void insert(int a,int b,LL w){ins(a,b,w); ins(b,a,w);}
void spfa(){
int head=,tail=;
q[]=; vis[]=;
while(head!=tail){
int x=q[head++]; if(head>M) head=;
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(vis[now]) continue;
vis[now]=; q[tail++]=now; d[now]=d[x]^e[i].w;
if(tail>M) tail=;
}
}
}
void insert(int num){
int x=,now;
for(int i=;i>=;i--){
now=(num&(<<i))>>i;
if(!l[x][now]) l[x][now]=++sum;
x=l[x][now];
}
h[x]++;
}
int find(int num){
int x=,now;
for(int i=;i>=;i--){
now=(num&(<<i))>>i;
if(l[x][!now]) x=l[x][!now],T+=(<<i);
else x=l[x][now];
}
return h[x];
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d %d %lld",&x,&y,&w),insert(x,y,w);
spfa();
for(int i=;i<=n;i++) insert(d[i]);
for(int i=;i<=n;i++){
T=; int p=find(d[i]);
if(T>mx) mx=T,ans=p;
else if(T==mx) ans+=p;
}
printf("%lld %lld\n",mx,ans>>);
return ;
}
Xor路的更多相关文章
- 【BZOJ 2115】【WC 2011】Xor
计算1到n的一条路径使得路径上的值xor和最大. 先任意走一条路径计算xor和,然后dfs的时候处理出所有的环的xor和,这样对于所有的环的xor和求线性基,在任意走出的路径的xor和上贪心即可. 正 ...
- 【BZOJ-2115】Xor 线性基 + DFS
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2142 Solved: 893[Submit][Status] ...
- BZOJ2115 [Wc2011] Xor
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...
- BZOJ 2115: [Wc2011] Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MB Submit: 2794 Solved: 1184 [Submit][Stat ...
- BZOJ.2115.[WC2011]Xor(线性基)
题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...
- GO语言的进阶之路-网络安全之proxy
GO语言的进阶之路-网络安全之proxy 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在党的带领下,我们大陆的孩子身心健康还是杠杠的,尤其是像我这种农村孩纸,从来不会像<人 ...
- P4151 [WC2011]最大XOR和路径
P4151 [WC2011]最大XOR和路径 一道妙极了的题. 首先直接从1走到n 然后现在图上有很多环 所以可以在走到n之后走到环上一个点,再走一遍环,再原路返回.这样就会xor上环的权值. 然后只 ...
随机推荐
- MongoDB+nodejs查询并返回数据
const express = require('express');const router = express.Router(); const Monk = require('monk');con ...
- Java 替换word文档文字,指定位置插入图片
先说下 需要的依赖包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ex ...
- vs对某些网络错误的拦截
在编写代码的过程中发现如果在写好网页中的文本框内写入js代码(以<script>1</script>输入为例) vs会自动拦截并报错,如图(密码中我也输入了<script ...
- 安全 - 堡垒机 - Jumpserver
GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...
- 【JAVA】mac配置java环境变量
如果用bash,修改~/.bash_profile 或 ~/.profile: 如果用zsh,修改-/.zshrc 修改这些文件之后,重修打开terminal,配置不会丢 首先确保已经安装了jdk: ...
- vue 判断是否登录,未登录跳转到登录页
网页一进入判断是否登录,未登录跳转到登录页面 router.js export default new Router({ routes: [ { path: '/', name: 'HelloWorl ...
- Redux百行代码千行文档
接触Redux不过短短半年,从开始看官方文档的一头雾水,到渐渐已经理解了Redux到底是在做什么,但是绝大数场景下Redux都是配合React一同使用的,因而会引入了React-Redux库,但是正是 ...
- phpstudy iis版本 php4.4.5 和 php5.6.7目录权限问题
开始用的php4.4.5 +iis 权限设置好了,切换成php5.6.7后目录没有了写入权限,各种百度后未能解决 php4.4.5 +iis 时 iis 匿名身份验证 用户是 IUSR 目 ...
- Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner
D. Robot Vacuum Cleaner time limit per test 1 second memory limit per test 256 megabytes Problem Des ...
- Codeforces Round #460 (Div. 2)-D. Substring
D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...