Dylans loves tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1611    Accepted Submission(s):
388

Problem Description
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 xs

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

 
Input
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)

.

 
Output
For each question ② in each testcase,if the value all
appear even times output "-1",otherwise output the value that appears odd
times.
 
Sample Input
1
3 2
1 2
2 3
1 1 1
1 1 2
1 1 3
 
Sample Output
-1
1

Hint

If 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.

 
Source
 
 
思路:
  判断树上路径是否有出现过奇数次的权值;
  数据保证一条路径上最多一个出现奇数次的权值;
  因为x^x=0,0^x=x的性质;
  我们可以用异或和;
  把一条路径的全部权值都异或;
  如果等于0则不存在,否则就输出;
  同时,因为0^0=0;
  所以我们全部的权值都+1;
  然后,输出时再-1;
 
 
来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 100005 using namespace std; struct TreeNodeType {
int l,r,dis,mid;
};
struct TreeNodeType tree[maxn<<]; struct EdgeType {
int v,next;
};
struct EdgeType edge[maxn<<]; int if_z,t,n,q,dis[maxn],dis_[maxn];
int cnt=,head[maxn],deep[maxn],f[maxn];
int top[maxn],size[maxn],flag[maxn]; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int u,int v)
{
cnt++;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt;
} void search_1(int now,int fa)
{
int pos=cnt++;
deep[now]=deep[fa]+,f[now]=fa;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].v==fa) continue;
search_1(edge[i].v,now);
}
size[now]=cnt-pos;
} void search_2(int now,int chain)
{
top[now]=chain,flag[now]=++cnt;
dis[flag[now]]=dis_[now]+;
int pos=;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].v==f[now]) continue;
if(size[edge[i].v]>size[pos]) pos=edge[i].v;
}
if(pos==) return ;
search_2(pos,chain);
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].v==pos||edge[i].v==f[now]) continue;
search_2(edge[i].v,edge[i].v);
}
} inline void tree_up(int now)
{
tree[now].dis=tree[now<<].dis^tree[now<<|].dis;
} void tree_build(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
if(l==r)
{
tree[now].dis=dis[l];
return ;
}
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
tree_up(now);
} void tree_change(int now,int to,int x)
{
if(tree[now].l==tree[now].r)
{
tree[now].dis=x;
return ;
}
if(to<=tree[now].mid) tree_change(now<<,to,x);
else tree_change(now<<|,to,x);
tree_up(now);
} int tree_query(int now,int l,int r)
{
if(tree[now].l==l&&tree[now].r==r)
{
return tree[now].dis;
}
if(l>tree[now].mid) return tree_query(now<<|,l,r);
else if(r<=tree[now].mid) return tree_query(now<<,l,r);
else return tree_query(now<<,l,tree[now].mid)^tree_query(now<<|,tree[now].mid+,r);
} int solve_do(int x,int y)
{
int pos=;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
pos=pos^tree_query(,flag[top[x]],flag[x]);
x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
pos=pos^tree_query(,flag[x],flag[y]);
if(pos==) return -;
else return pos-;
} int main()
{
in(t);
int lit=t;
while(t--)
{
memset(head,,sizeof(head));
in(n),in(q);cnt=;int u,v;
for(int i=;i<n;i++)
{
in(u),in(v);
edge_add(u,v);
edge_add(v,u);
}
for(int i=;i<=n;i++) in(dis_[i]);
cnt=,search_1(,);
cnt=,search_2(,);
tree_build(,,n);
int type;
for(int i=;i<=q;i++)
{
in(type),in(u),in(v);
if(type) printf("%d\n",solve_do(u,v));
else tree_change(,u,v+);
}
}
return ;
}

AC日记——Dylans loves tree hdu 5274的更多相关文章

  1. hdu 5274 Dylans loves tree

    Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...

  2. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  4. hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. HDU 5274 Dylans loves tree 树链剖分+线段树

    Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...

  6. hdu Dylans loves tree [LCA] (树链剖分)

    Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  7. HDU 5274 Dylans loves tree(树链剖分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...

  8. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

  9. AC日记——Aragorn's Story HDU 3966

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. jenkins+maven+svn 自动化部署

    背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...

  2. vue创建路由,axios前后台交互,element-ui配置使用,django contentType组件

    vue中创建路由 每一个vue组件都有三部分组成 template:放html代码 script:放js相关 style:放css相关 vue中创建路由 1.先创建组件 Course.vue 2.ro ...

  3. leetcode-13-basic-binaryTree

    101. Symmetric Tree 解题思路: 递归的方法如下.分几种情况考虑,如果左子树和右子树都是空,那么返回true:如果不同时为空,返回false:如果都不为空,则 判断其值是否相等,不相 ...

  4. Linux下查看USB设备信息

    首先需要将usbfs挂载一下,然后才能查看.$ mount -t usbfs none /proc/bus/usb$ cat  /proc/bus/usb/devices或者在文件(/etc/fsta ...

  5. jmeter中重要组件及其执行顺序

    jmeter中重要组件有:Sampler,计时器,前置处理器和后置处理器,断言,Controller,Listener和配置原件. 同类组件之间是从上到下的顺序执行,不同组件之间是按照以下的顺序执行的 ...

  6. C指针问题

    <!DOCTYPE html> 多级c指针传值问题 /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / Au ...

  7. [POJ 1003] Hangover C++解题

        Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95164   Accepted: 46128 De ...

  8. [python学习篇][python工具使用篇][1] 编辑,设置等

    1 添加sublime到环境变量 win +r ,输入sysdm.cpl, 在弹出的界面选择高级,选择环境变量,编辑path,添加sublime的安装目录(这是sublime的一种安装方式,另外一种安 ...

  9. AtCoder Regular Contest 083

    C - Sugar Water Time limit : 3sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke ...

  10. 【整理】python中re的match、search、findall、finditer区别

    match 从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾. search 若string中包 ...