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. 01将图片嵌入到Markdown文档中

    将图片内嵌入Markdown文档中 将图片嵌入Markdown文档中一直是一个比较麻烦的事情.通常的做法是将图片存入本地某个路径或者网络存储空间,使用URL链接的形式插入图片: ![image][ur ...

  2. json转换为字典

    str---dict ata_dict=json.loads(data)

  3. 【jquery】 选中复选框 和 return false 的影响

    $('id').attr('checked',true); return false;   如果后面接上return false 的话,复选框的钩钩不会改变,但是.is(':checked')仍然能检 ...

  4. python3 完全平方数(循环)

    题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...

  5. INDEX && PRIMARY KEY && UNIQUE KEY

    When I have do some sql tody, some confusion come up to me. Its about the index && PRIMARY K ...

  6. 远程连接MYSQL8.0服务器问题

    title: 远程连接MYSQL8.0服务器问题 date: 2018-07-07 11:02:26 updated: tags: [MYSQL,坑] description: keywords: c ...

  7. [git 学习篇] 提交文件

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...

  8. [译]pycache是什么?

    原回答: https://stackoverflow.com/questions/16869024/what-is-pycache 当你用python运行一个程序时,解释器首先将它编译成字节码(这是一 ...

  9. hdoj--1045<dfs&二分图最大匹配>(这里是dfs解法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目描述: 在矩阵中放车,车可四面攻击,矩阵中有墙,可以防止攻击,给出墙的位置,输出最多可以放多 ...

  10. Git的使用小结

    1. git是一种分布式版本控制工具.目前项目中比较常见的版本控制器有SVN.CVS等,这些版本控制工具属于集中式版本控制器.集中式版本控制器,有一个中央服务器,开发人员的开发机从主服务器上下载了项目 ...