Xor Path 树路径异或

Xor Path
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述
In graph theory, if an undirected gragh G(V, E) without cycle has n vertices, n − 1 edges and the graph is connected, then G(V, E) is called a tree. You are given a tree has n vertices, each vertice has a weight. Vertice i’s weight is wiw_iwi​ . Henry has q queries, each query has two vertices u, v. He wants to know the xor sum of the weights of all vertices on the shortest path from u to v (contains u,v).
输入描述:
The first line is an integer n(1≤n≤105)n (1 ≤ n ≤ 10^5
)n(1≤n≤105), the number of the vertices.Line 2 has n integers , the ith integer is wiw_iwi​ (1≤wi≤109)(1 ≤ w_i ≤ 10^9)(1≤wi​≤109), the weight of the vertice i.Line 3 to line n+1, each line has two integers u, v, means there has an edge between u and v(1≤u,v≤n).v (1 ≤ u, v \leq n).v(1≤u,v≤n).Line n + 1 is q, means the number of queries.Next q(1≤q≤105)q (1 ≤ q ≤ 10^5
)q(1≤q≤105) lines, each line has two integers u, v (1 ≤ u, v ≤ n) means the beginning and end ofthe shortest path henry wants to query.
输出描述:
For each query,output the xor sum of the weights of all vertices on the shortest path from u to v.
示例1
输入
6
1 1 2 3 4 10
1 2
1 3
1 4
2 5
2 6
2
3 6
2 4
输出
8
3
在图论中,如果一个无圈的无向gragh G(V,E)有n个顶点,n-1条边且图是连通的,则G(V,E)称为树。
给定一棵树有n个顶点,每个顶点有一个权重。亨利有q个查询,每个查询有两个顶点u,v。
他想知道从u到v(包含u,v)最短路径上所有顶点权重的异或和。
输入描述:
第一行是整数n、顶点个数。
第2行有n个整数,第i个整数是w i ,垂直线的权重i。
第3行到第n+1行,每行有两个整数u,v,表示u和v之间有一条边(1≤u,v≤n)。
第n+1行是q,表示查询数。
下一个q行,每行有两个整数u,v(1≤u,v≤n)
表示亨利要查询的最短路径的起点和终点。
输出描述:
对于每个查询,输出u到v之间最短路径上所有顶点权重的异或和。

//先写的这个超时 
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
vector<int>P[100009];
int N,w[100009],ct[100009];
void QH(int p,int q,int ans)
{
   
	ct[p]=1;//点p标记为已访问 
	//这样就不会往回走
	//因为是树,只要不走重复的点走到目的地 就是最短路 
	for(int i=0;i<P[p].size();i++)
	{
   
		if
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值