您的位置:首页 > 其它

Dylans loves tree(hdu 5274 树链剖分+线段树模板)

2018-03-01 19:33 399 查看

Dylans loves tree

[align=center]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1884    Accepted Submission(s): 480

[/align]

[align=left]Problem Description[/align] Dylans is given a tree with N nodes.

All nodes have a value A[i].Nodes on tree is numbered by 1∼N.

Then he is given Q questions like that:

①0 x y:change node x′s value to y

②1 x y:For all the value in the path from x to y,do they all appear even times?

For each ② question,it guarantees that there is at most one value that appears odd times on the path.

1≤N,Q≤100000, the value A[i]∈N and A[i]≤100000 
[align=left]Input[/align] In the first line there is a test number T.
(T≤3 and there is at most one testcase that N>1000)

For each testcase:

In the first line there are two numbers N and Q.

Then in the next N−1 lines there are pairs of (X,Y) that stand for a road from x to y.

Then in the next line there are N numbers A1..AN stand for value.

In the next Q lines there are three numbers(opt,x,y). 
[align=left]Output[/align] For each question ② in each testcase,if the value all appear even times output "-1",otherwise output the value that appears odd times. 
[align=left]Sample Input[/align]
13 21 22 31 1 11 1 21 1 3 [align=left]Sample Output[/align]
-11HintIf you want to hack someone,N and Q in your testdata must smaller than 10000,and you shouldn't print any space in each end of the line.  题意:给定一棵树,每个节点都有一个权值,现在有2种操作: 0:把x节点的权值修改为y;  1:查询节点x到节点y的路径中是否有出现奇数次的权值,现保证最多只有一个权值出现奇数次,若有,输出这个权值,若没有,输出-1;
思路:由于保证最多只有一个权值出现奇数次,那么把路径中的所有权值异或一下就是那个出现奇数次的值,若没有出现奇数次的权值,就得到0。

有一个坑点:他给的权值是自然数,也就是说包含0,若出现奇数个0,异或一下还是为0,这与我们想要的不符。我们的解法是把他给的权值全部+1,修改时也把修改为的值+1,最后答案-1即可,这就巧妙的避开了0。

在一棵树上做路径的区间查询,自然想到树链剖分,然后用线段树去维护异或值即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAX = 1e5+10;

struct Edge{
int to,next;
}edge[MAX*2];

int n,q;
int a[MAX],val[MAX];
int head[MAX],len;
int cnt;
//树链剖分
int siz[MAX],top[MAX],son[MAX],dep[MAX],faz[MAX],tid[MAX];
//线段树
int tree[MAX<<2];

void add(int from,int to)
{
edge[len].to=to;
edge[len].next=head[from];
head[from]=len++;
}

//****树链剖分*******

void dfs1(int root,int fa,int depth)
{
siz[root]=1;
dep[root]=depth;
faz[root]=fa;
son[root]=0;
for(int i=head[root];i!=-1;i=edge[i].next)
{
if(edge[i].to==fa)
continue;
dfs1(edge[i].to,root,depth+1);
siz[root]+=siz[edge[i].to];
if(siz[edge[i].to]>siz[son[root]])
son[root]=edge[i].to;
}
}

void dfs2(int root,int tp)
{
top[root]=tp;
tid[root]=++cnt;
if(son[root])
dfs2(son[root],tp);
for(int i=head[root];i!=-1;i=edge[i].next)
{
if(edge[i].to==son[root]||edge[i].to==faz[root])
continue;
dfs2(edge[i].to,edge[i].to);
}
}

//*****************

//****线段树*******

void PushUp(int root)
{
tree[root]=tree[root<<1]^tree[root<<1|1];
}

void build(int l,int r,int root)
{
if(l==r)
{
tree[root]=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
PushUp(root);
}

void update(int x,int y,int l,int r,int root)
{
if(l==r)
{
tree[root]=y;
return;
}
int mid=(l+r)>>1;
if(x<=mid)
update(x,y,l,mid,root<<1);
else
update(x,y,mid+1,r,root<<1|1);
PushUp(root);
}

int query(int L,int R,int l,int r,int root)
{
if(L<=l&&r<=R)
{
return tree[root];
}
int mid=(l+r)>>1;
int ans=0;
if(L<=mid)
ans^=query(L,R,l,mid,root<<1);
if(R>mid)
ans^=query(L,R,mid+1,r
c042
,root<<1|1);
return ans;
}

//*****************

//基于树链的查询
int change(int x,int y)
{
int ans=0;
int f1=top[x];
int f2=top[y];
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(x,y);
swap(f1,f2);
}
       
        //数据结构的修改/查询
        ans^=query(tid[f1],tid[x],1,n,1);

        x=faz[f1];
f1=top[x];
}
if(tid[x]>tid[y])
swap(x,y);

    //数据结构的修改/查询
    ans^=query(tid[x],tid[y],1,n,1);

    return ans;
}

int main()
{
int T;
int Case=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&q);
memset(head,-1,sizeof(head));
memset(a,0,sizeof(a));
len=0;
cnt=0;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs1(1,0,1);
dfs2(1,1);
for(int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
val[i]+=1; //+1
}
for(int i=1;i<=n;i++)
a[tid[i]]=val[i];
build(1,n,1);
for(int i=1;i<=q;i++)
{
int opt,x,y;
scanf("%d%d%d",&opt,&x,&y);
if(opt==0)
{
update(tid[x],y+1,1,n,1); //y+1
}
else
{
printf("%d\n",change(x,y)-1); //ans-1
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: