您的位置:首页 > 其它

JZOJ5483. 【清华集训2017模拟11.26】简单路径

2017-11-30 16:14 495 查看

Description

给定一棵带边权的树,选择两条没有公共边的简单路径(长度可以为0),使得所有在任意一条路径上的边的异或和尽量大。

Input

第一行一个数n表示点数,点的编号是0到n-1。

接下来一行(n-1)个数,第i个数表示编号为i的点的父亲编号,保证这个编号小于i。

接下来一行(n-1)个数,第i个数表示编号为i的点到它父亲的边的边权。

Output

输出一行一个数表示答案。

Sample Input

输入1:

9

0 0 2 2 4 4 5 6

13 16 12 11 3 1 4 2

输入2:

12

0 0 2 0 1 2 2 4 6 1 5

628 589 815 864 459 507 733 239 904 592 818

Sample Output

输出1:

31

样例解释1:

两条路径可以是4->6->8和0->2->3。

输出2:

1017

Data Constraint

对于50%的数据,n<=100。

对于100%的数据,n<=1000,边权均为不超过1000的非负整数。

题解

要求两条不相交的路的异或和,

考虑一下如果两条相交的路径异或在一起,

那么中间重复部分的权值异或起来就会变成0,

所以,答案就是求任意两条路径的异或和,

数据范围是边权小于等于1000,

也就是异或出来的数都是小于1024的,

就直接用个桶存着,就好了。

如果边权再大一些,就可以用trie。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define ll long long
#define N 1003
#define db double
#define P putchar
#define G getchar
#define mo 998244353
#define x_ (x<<1)
#define y_ (y<<1)
using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
ll w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}

ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll abs(ll x){return x<0?-x:x;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}

void mx(int& x,int y){x=x>y?x:y;}

int f
,s
,v
,n,x,sum,t,y;
int ans,z[10],g[N*4];

int main()
{
z[0]=1;
for(int i=1;i<10;i++)
z[i]=z[i-1]<<1;
read(n);
for(int i=1;i<n;i++)
read(x),f[i]=x;
for(int i=1;i<n;i++)
read(v[i]),s[i]=s[f[i]]^v[i];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
x=y=1;
sum=s[i]^s[j];
t=0;
for(int k=9;k>=0;k--)
{
if(sum&z[k])
{
x=x_+1;
if(g[y_])t+=z[k],y=y_;else y=y_+1;
}
else
{
x=x_;
if(g[y_+1])t+=z[k],y=y_+1;else y=y_;
}
g[x]++;
}
mx(ans,t);
}
printf("%d",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  trie树