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. k8s搭建WebUI--Dashborad管理界面

    k8s的webUI管理界面可以更好更直观更便捷的让我们去管理我们的k8s集群. 我们知道,由于某些原因我们无法直接拉取dashboard的镜像,但是国内有些人已经将镜像下载到dockerhub中可以给 ...

  2. 文件处理seek以及修改内容的两种方式

    f.seek(offset,whence)offset代表文件的指针的偏移量,单位是字节byteswhence代表参考物,有三个取值# 0:参照文件的开头# 1:参照当前文件指针所在位置# 2: 参照 ...

  3. python基础之文件处理总结

    读文件: with open('contacts.txt', 'r', encoding='utf-8') as f: data = f.read() 二进制模式读 使用场景:网络传输(视频.图片或进 ...

  4. Linux学习-函式库管理

    动态与静态函式库 首先我们要知道的是,函式库的类型有哪些?依据函式库被使用的类型而分为两大类,分别是静态 (Static) 与动态 (Dynamic) 函式库两类. 静态函式库的特色: 扩展名:(扩展 ...

  5. Linux学习-备份的种类、频率与工具的选择

    完整备份之累积备份 (Incremental backup) 还原的考虑 如果是完整备份的话.若硬件出问题导致系统损毁时,只要将完整备份拿出来,整个给他倾倒回去硬盘, 所有事情就搞定了!有些时候 (例 ...

  6. Linux学习-服务器硬件数据的收集

    以系统内建 dmidecode 解析硬件配备 系统有个名为 dmidecode 的软件,它可以解析 CPU 型号.主板型号与内存相 关的型号等等~ [root@study ~]# dmidecode ...

  7. Linux任务计划、周期性任务执行

    Linux任务计划.周期性任务执行 周期性任务执行: cron 守护进程(crond):服务,不间断地运行于后台 # service crond {start|stop|status|restart} ...

  8. android自动化测试之Monkey--从参数讲解、脚本制作到实战技巧

    视频: http://v.youku.com/v_show/id_XODcyMjM1MDA4.html?from=y1.2-1-87.4.4-1.1-1-2-3 PPT: http://www.doc ...

  9. MongoDB学习-->Spring Data Mongodb-->MongodbTemplate

    配置文件application-dev.yml: server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: ma ...

  10. JSON Undefined 问题

    在IE6和IE7浏览器下或在IE8-IE10浏览器文档模式为IE7及以下时,控制台会报错:JSON is undefined. 这种错误在IE6和IE7浏览器下出现很正常,因为JSON在IE8+浏览器 ...