【算法】trie树+xor路径
【题解】
套路1:统计从根到每个点的xor路径和,由于xor的自反性,两个点到根的xor路径和异或起来就得到两点间路径和。
然后问题就是找到n个值中异或值最大的两个值,考虑枚举每个数字,对于一个数找到与其异或和最大的数。
套路2:对所有数值二进制建01-trie,对于一个已知数字在trie上每一层尽量往另一端走,O(log n)得到与其异或和最大的数。
复杂度O(n log n)。
另一种做法,用两个指针从根往下,尽量分叉走,查询总复杂度O(log n),但是建树仍然需要O(n log n)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include#include #include using namespace std;const int maxn=100010;struct edge{ int v,w,from;}e[maxn*2];int t[maxn*33][2],dfsnum=0,cnt=0,tot=0,p[maxn*2],first[maxn],n;bool val[maxn*33];void insert(int u,int v,int w){tot++;e[tot].v=v;e[tot].w=w;e[tot].from=first[u];first[u]=tot;}void push(int x){ int u=0; for(int i=30;i>=0;i--){ bool c=x&(1< =0;i--){ bool c=x&(1<