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. Ubuntu下kaldi安装

    该文章为博主原创,如若转载请注明出处:https://www.cnblogs.com/fengleixue/p/9482202.html 因公司业务需要需使用kaldi语音识别工具,现将kaldi环境 ...

  2. jenkins+svn+pipeline+kubernetes部署java应用(二)

    在jenkins中只能通过http的方式获取svn的数据,所以需要配置svn的http访问方式 一.安装http服务端和mod_dav_svn插件 由于Subversion需要版本化的控制,因此标准的 ...

  3. MySQL查询时,查询结果如何按照where in数组排序

    MySQL查询时,查询结果如何按照where in数组排序 在查询中,MySQL默认是order by id asc排序的,但有时候需要按照where in 的数组顺序排序,比如where in的id ...

  4. vscode 实时预览 编辑markdown 插件 Markdown Preview Enhanced

    说明地址: https://shd101wyy.github.io/markdown-preview-enhanced/#/zh-cn/?id=markdown-preview-enhanced

  5. 使用python3调用MyQR库生成动态二维码(附源代码)

    可生成普通二维码.带图片的艺术二维码(黑白与彩色).动态二维码(黑白与彩色). GitHub:https://github.com/sylnsfar/qrcode 中文版:https://github ...

  6. Android后台的linux一直保持唤醒状态,不进入睡眠

    由于要做Android手机的电池续航测试,是不能插usb的,所以把case放到sh文件中,之后push到手机里,执行的. 但是出现个问题,假如case中有很长时间的sleep操作,关闭手机屏幕,这样l ...

  7. Mysql显示所有数据库

    show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ ...

  8. net clr via c sharp chap1-- note

    Tag-> 托管代码 Tag-> .NET Framework 系统环境检测 Tag-> 设置平台 Tag-> 查询64或32位机 Tag-> IL编译成机器指令 Tag ...

  9. MyBatis拦截器打印不带问号的完整sql语句方法

    /* Preparing: SELECT * FROM tb_user WHERE id = ? AND user_name = ?  目标是打印:SELECT * FROM tb_user WHER ...

  10. xml ,html,xhtml

    html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...