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(P[p][i]==q)
{
ans=ans^w[q];
cout<<ans<<endl;
return;
}
}
for(int i=0;i<P[p].size();i++)
if(ct[P[p][i]]==0)
QH(P[p][i],q,ans^w[P[p][i]]);
}
int main(){
cin>>N;
for(int i=1;i<=N;i++)
cin>>w[i];
int a1,a2;
for(int i=1;i<N;i++){
cin>>a1>>a2;
P[a1].push_back(a2);
P[a2].push_back(a1);
}
int n,p,q;
cin>>n;
while(n--)//n个查询
{
cin>>p>>q;
memset(ct,0,sizeof(ct));
QH(p,q,w[p]);//w是当前的权重
}
return 0;
}
//a异或b再异或b 等于啥也没干还是a
//树上两个点 要么在一条线上 要么会通过一个拐弯的
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
vector<int>P[100009];
int N,w[100009];
int Fa[100009],dp[100009];//next的父亲,深度
int Qz[100009];//Qz[i]从树根到节点i 一路上的异或结果
void dfs(int a)
{
for(int i=0;i<P[a].size();i++)
{
int next=P[a][i];
if(Fa[a]==next)continue;//这个邻节点是a的父亲 跳过
Fa[next]=a;
Qz[next]=w[next]^Qz[a];
dp[next]=dp[a]+1;
/*cout<<next<<"父亲: "<<Fa[next]<<endl;
cout<<next<<"从根异或到next: "<<Qz[next]<<endl;
cout<<next<<"深度: "<<dp[next]<<endl;
cout<<endl;*/
dfs(next);
}
}
int find(int a,int b)//找a和b的 是同一个的 长辈 就是路上拐弯的点
{
if(dp[a]!=dp[b])// 深度弄成相同的先
{
if(dp[a]<dp[b])
swap(a,b);
do
{
a=Fa[a];
}while(dp[a]!=dp[b]);
}
for(;a!=b;)//如果起初的a b是一条线的 那上边弄成同深度就找到了 就不会进 这个循环
{
a=Fa[a];
b=Fa[b];
}
//cout<<"公共父节点: "<<a<<endl;//啊,可能往上好几辈了
return a;
}
int main()
{
cin>>N;
for(int i=1;i<=N;i++)
cin>>w[i];
int a1,a2;
for(int i=1;i<N;i++)
{
cin>>a1>>a2;
P[a1].push_back(a2);
P[a2].push_back(a1);
}
//树根
Qz[1]=w[1];//权重是自己
dp[1]=1;//深度是1
Fa[1]=0;//父节点0
dfs(1);
int n,p,q;
cin>>n;
while(n--)
{
cin>>p>>q;
int ans=Qz[p]^Qz[q]^w[find(p,q)];
//cout<<Qz[p]<<" "<<Qz[q]<<" "<<Qz[find(p,q)]<<endl;
cout<<ans<<endl;
}
return 0;
}
来源:CSDN
作者:hjnnhjknbgjnnbc
链接:https://blog.csdn.net/weixin_44009226/article/details/104155236