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. 虚拟机-Debian服务器配置

    目的:用虚拟机中的Debian 8 操作系统作为web服务器 一.安装操作系统 首先要在vmware中安装一个debian操作系统,由于要让在局域网中的其他计算机能访问到此虚拟操作系统,因此在vmwa ...

  2. R语言--输入输出

    基本输入输出 输入: readline, edit, fix 输出: print, cat 输出重定向 sink #基本输入输出 x=readline('请输入:') #读取输入,一行为一个字符串 x ...

  3. 使用navicat连接mysql时报错:2059 - authentication plugin 'caching_sha2_password'

    首先从本地登录mysql数据库,进入mysql控制台,输入如下命令: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_passwo ...

  4. 20155204《网络对抗》Exp 6 信息搜集与漏洞扫描

    20155204<网络对抗>Exp 6 信息搜集与漏洞扫描 一.实验后回答问题 1.哪些组织负责DNS,IP的管理. 互联网名称与数字地址分配机构,简称ICANN机构,决定了域名和IP地址 ...

  5. 20155226《网络攻防》 Exp5 MSF基础应用

    20155226<网络攻防> Exp5 MSF基础应用 基础问题回答 1.用自己的话解释什么是exploit,payload,encode? exploit : Exploit的英文意思就 ...

  6. 20155233 《网络对抗》Exp7 网络欺诈技术防范

    应用SET工具建立冒名网站 1.要让冒名网站在别的主机上也能看到,需要开启本机的Apache服务,并且要将Apache服务的默认端口改为80,先在kali中使用netstat -tupln |grep ...

  7. 2017-2018-2 《网络对抗技术》 20155319 第二周 Exp1 PC平台逆向破解(5)M

    2017-2018-2 <网络对抗技术> 20155319 第二周 Exp1 PC平台逆向破解(5)M 一.实践目标 1.1实践介绍 本次实践的对象是一个名为pwn1的linux可执行文件 ...

  8. 20155334 《网络攻防》 Exp7 网络欺诈防范

    20155334 <网络攻防> Exp7 网络欺诈防范 一.基础问题回答 通常在什么场景下容易受到DNS spoof攻击 同一局域网下,以及各种公共网络. 在日常生活工作中如何防范以上两攻 ...

  9. python3获取文件及文件夹大小

    获取文件大小 os.path.getsize(file_path):file_path为文件路径 >>> import os >>> os.path.getsize ...

  10. CEPH LIO iSCSI Gateway

    参考文档: Ceph Block Device:http://docs.ceph.com/docs/master/rbd/ CEPH ISCSI GATEWAY:http://docs.ceph.co ...