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

最新文章

  1. linux命令初识
  2. Java中Eclipse的使用
  3. sql server pivot/unpivot 行列互转
  4. Oracle补习班第九天
  5. 关于for循环删除数组内容出现的问题
  6. SQL Server常用语句
  7. 【Android】如何写一个JsBridge
  8. TableViewCell的分割线显示不全解决方法
  9. 列出当前ARM开发板系统加载的模块
  10. ASP.NET MVC模型部分验证
  11. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)
  12. jQuery(一)、核心
  13. 【记录】IntelliJ IDEA—IDEA2018-2019激活
  14. 查询rman 备份信息集
  15. Sitecore xDB基础知识 - 识别用户,联系人,访客,客户
  16. day18(javaEE三大组件之一servlet(简介(一)))
  17. BestCoder Round #27
  18. [Winform]关于cefsharp触屏设备长按文本内容,崩溃问题的修复
  19. 2、Android-UI(关于Nine-Patch图片)
  20. android popupwindow位置显示

热门文章

  1. odoo之可选择多个内容显示问题
  2. Linux学习笔记(第十一章)
  3. 20155308《网络对抗》Exp6 信息搜集与漏洞扫描
  4. Python学习系列:PyCharm CE 安装与测试
  5. 让vim成为VS的编辑器
  6. 【ORACLE】碎片整理
  7. stl源码剖析 详细学习笔记 配接器
  8. Asp.Net_抓包解析xml文件为json
  9. Istio全景监控与拓扑
  10. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历