NC220582. XortestPath
描述
You cook up a simple connected weighted undirected graph with this distance function, and you ask yourself questions about this graph, each asking about the shortest XOR distance between two nodes.
输入描述
The input consists of:
- One line containing three integers (), (), and (), the number of nodes, edges, and questions respectively.
- lines describing an edge. Each line consists of three integers , , (, and ), indicating that there is an undirected edge of length between nodes and .
- lines describing a question. Each line consists of two integers , () asking for the shortest distance between nodes and .
There is at most one edge between any pair of distinct nodes and every node can be reached from any other node.
输出描述
For every question, output one line containing the shortest distance between nodes and .
示例1
输入:
3 3 3 1 2 2 1 3 2 2 3 3 1 2 1 3 2 3
输出:
1 1 0
示例2
输入:
7 10 5 1 2 45 2 3 11 2 4 46 3 4 28 3 5 59 3 6 12 3 7 3 4 5 11 5 6 23 6 7 20 1 4 2 6 3 5 1 7 5 5
输出:
1 5 0 5 0
C++(clang++11) 解法, 执行用时: 141ms, 内存消耗: 13904K, 提交时间: 2021-04-08 11:37:04
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define b(x) (1ll<<((x)-1)) ll a[70],sb[100500],w; int i,j,k,n,m,x,y,t,vis[100500]; vector<pair<int,ll> >v[100500]; void upd(){ int i,j; for(i=1;i<=60;i++){ for(j=i+1;j<=60;j++){ if(a[i]&a[j]){a[j]^=a[i];} } } } void add(ll x){ int i,j; for(i=60;i>=1;i--){ if(x&b(i)){ if(a[i]){x^=a[i];} else{a[i]=x;break;} } } } void dfs(int k){ vis[k]=1; for(auto [i,j]:v[k]){ if(!vis[i]){sb[i]=sb[k]^j;dfs(i);} else{add(sb[i]^sb[k]^j);} } } ll get(ll x){ for(int i=60;i>=1;i--){ if(x&b(i)){x^=a[i];} } return x; } int main(){ scanf("%d%d%d",&n,&m,&t); for(i=1;i<=m;i++){ scanf("%d%d%lld",&x,&y,&w); v[x].push_back({y,w}); v[y].push_back({x,w}); } dfs(1);upd(); while(t--){ scanf("%d%d",&x,&y); printf("%lld\n",get(sb[x]^sb[y])); } }