Dylans loves tree

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

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

 
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.

 

思路:
一道非常简单的题。。要求u-v之前出现次数为奇数的数字,如果没找到就输出-1,找到就输出这个数字。
题目保证了出现为奇数次的数字的个数不超过1,那么直接对u-v异或就好了,最后剩下的如果是0有两种可能:
1.没有出现次数为奇数的数字 。 2.出现次数为奇数的数字为0;
我们只要将输入的数字全部+1就可以避免这种情况了
最后当输出的值为0时没有出现次数为奇数的数字,输出-1,不为0的话直接输出这个数字就好了。
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
struct node{
int to,next;
}e[M];
int sum[M<<],son[M],fa[M],head[M],siz[M],top[M],dep[M],tid[M],rk[M],a[M];
int cnt1,cnt,n;
void add(int u,int v){
e[++cnt1].to = v;e[cnt1].next = head[u];head[u] = cnt1;
e[++cnt1].to = u;e[cnt1].next = head[v];head[v] = cnt1;
} void dfs1(int u,int faz,int deep){
dep[u] = deep;
fa[u] = faz;
siz[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa[u]){
dfs1(v,u,deep+);
siz[u] += siz[v];
if(son[u] == -||siz[v] > siz[son[u]])
son[u] = v;
}
}
} void dfs2(int u,int t){
top[u] = t;
tid[u] = cnt;
rk[cnt] = u;
cnt++;
if(son[u] == -) return;
dfs2(son[u],t);
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v != son[u]&&v != fa[u])
dfs2(v,v);
}
} void pushup(int rt){
sum[rt] = sum[rt<<]^sum[rt<<|];
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = a[rk[l]];
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int c,int l,int r,int rt){
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
int ret = ;
if(L <= m) ret^=query(L,R,lson);
if(R > m) ret^=query(L,R,rson);
return ret;
} int ask(int x,int y){
int sum = ;
int fx = top[x],fy = top[y];
while(fx != fy){
if(dep[fx] < dep[fy]) swap(x,y),swap(fx,fy);
sum ^= query(tid[fx],tid[x],,n,);
x = fa[fx];fx = top[x];
}
if(dep[x] < dep[y]) swap(x,y);
sum^=query(tid[y],tid[x],,n,);
//cout<<sum<<endl;
return sum;
} void init()
{
memset(son,-,sizeof(son));
for(int i = ; i <= *n;i ++){
e[i].to = ;e[i].next = ;head[i] = ;
}
}
int main()
{
int t,u,v,x,y,op,q;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
cnt = ; cnt1 = ;
init();
for(int i = ; i < n-;i ++){
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i = ;i <= n;i ++)
scanf("%d",&x),a[i]=x+;
dfs1(,,); dfs2(,); build(,n,);
while(q--){
scanf("%d",&op);
if(op==){
scanf("%d%d",&x,&y);
//cout<<ask(x,y)<<endl;
if(ask(x,y)!=) printf("%d\n",ask(x,y)-);
else printf("-1\n");
}
else{
scanf("%d%d",&x,&y);
update(tid[x],y+,,n,);
}
}
}
return ;
}

hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)的更多相关文章

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

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

  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

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

  4. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  5. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  6. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  7. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  8. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  9. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  10. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

随机推荐

  1. js 自己项目中几种打开或弹出页面的方法

    自己项目中,几种打开或弹出页面的方法(部分需要特定环境下) var blnTop = false;//是否在顶层显示 ///动态生成模态窗体(通过字符串生成) ///strModalId:模态窗体ID ...

  2. 创世纪 BZOJ3037 & [Poi2004]SZP BZOJ2068

    分析: 树形DP中的一种,基环树DP 针对每一个环跑DP,f[i],g[i]分别表示选或者不选,之后我们注意每次遍历的时候,不要重复遍历. 附上代码: #include <cstdio> ...

  3. PI monitor error process-RESOURCE_NOT_FOUND-转

    事务:sxi_monitor 状态:system error 类型:Request Message Mapping 错误简要:RESOURCE_NOT_FOUND 错误详细信息: <?xml v ...

  4. 20155209林虹宇Exp4 恶意代码分析

    Exp4 恶意代码分析 系统运行监控 使用schtasks指令监控系统运行 新建一个txt文件,然后将txt文件另存为一个bat格式文件 在bat格式文件里输入以下信息 然后使用管理员权限打开cmd, ...

  5. 20155211 网络对抗 Exp9 Web安全基础实践

    20155211 网络对抗 Exp9 Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御? 原理:SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语 ...

  6. 2017-2018 Exp7 网络欺诈技术防范 20155214

    目录 Exp7 网络欺诈技术防范 实验内容 信息收集 知识点 Exp7 网络欺诈技术防范 实验内容 实验环境 主机 Kali 靶机 Windows 10 实验工具 平台 Metaploit 信息收集 ...

  7. controlfile作为RMAN的repository时,对 keep time 的测试

    4月2日,首先查看系统状况: SQL> show parameter control NAME                                 TYPE        VALUE ...

  8. CS100.1x Introduction to Big Data with Apache Spark

    CS100.1x简介 这门课主要讲数据科学,也就是data science以及怎么用Apache Spark去分析大数据. Course Software Setup 这门课主要介绍如何编写和调试Py ...

  9. Seay源代码审计系统的配置和安装

    2014年7月31日 Seay源代码审计系统2.1 时隔刚好一年之久,源代码审计系统再次更新,这次主要优化审计体验,优化了漏洞规则,算是小幅更新,原来使用者打开程序会提示自动更新. 1.优化原有规则, ...

  10. Outlook2013修改数据文件默认存放目录

    转载 当使用outlook 2013新建Email账户的时候,其数据文件(.ost文件)总是被保存在C盘默认目录“C:\Users\用户名\AppData\Local\Microsoft\Outloo ...