bzoj1594 Pku3764 The xor-longest Path

题目链接

先求每个点到根的异或和

然后就要找出两个点,使dis[a]^dis[b]最大

注意异或的性质,我们可以用trie树,沿着与当前数字每位的相反方向走

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 void inin(int &ret)
27 {
28     ret=0;int f=0;char ch=getchar();
29     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=1;ch=getchar();}
30     while(ch>=‘0‘&&ch<=‘9‘)ret*=10,ret+=ch-‘0‘,ch=getchar();
31     ret=f?-ret:ret;
32 }
33 int n,head[100010],next[200020],zhi[200020],w[200020],ed,dis[100010];
34 int ch[3300030][2],eed,po[33];
35 void add(int a,int b,int c)
36 {
37     next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c;
38     next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c;
39 }
40 void dfs(int x,int fa)
41 {
42     for(int i=head[x];i;i=next[i])
43         if(zhi[i]!=fa)
44         {
45             dis[zhi[i]]=dis[x]^w[i];
46             dfs(zhi[i],x);
47         }
48 }
49 void add(int x)
50 {
51     int now=0;
52     rre(i,30,0)
53     {
54         int temp=x&po[i];
55         if(temp)temp=1;
56         if(!ch[now][temp])ch[now][temp]=++eed;
57         now=ch[now][temp];
58     }
59 }
60 int ans=0;
61 void query(int x)
62 {
63     int now=0,xx=0;
64     rre(i,30,0)
65     {
66         int temp=x&po[i];
67         if(temp)temp=1;
68         if(ch[now][temp^1])now=ch[now][temp^1],xx|=po[i];
69         else now=ch[now][temp];
70     }
71     ans=max(ans,xx);
72 }
73 int CSC()
74 {
75     inin(n);po[0]=1;re(i,1,30)po[i]=po[i-1]<<1;
76     re(i,2,n)
77     {
78         int q,w,e;
79         inin(q),inin(w),inin(e);
80         add(q,w,e);
81     }
82     dfs(1,0);
83     re(i,1,n)
84         add(dis[i]);
85     re(i,1,n)query(dis[i]);
86     printf("%d",ans);
87     return 0;
88 }
时间: 2024-10-25 20:00:07

bzoj1594 Pku3764 The xor-longest Path的相关文章

poj3764 The XOR Longest Path【dfs】【Trie树】

The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted: 2040 Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: ⊕ is the xor operator. W

SPOJ Problem 1437:Longest path in a tree

求树的最长链,BFS和DFS都可以,时间复杂度O(n) #include<cstdio> #include<cstring> int tot,vt[20005],nxt[20005],a[20005]; bool vis[10005]; int n,i,j,xx,yy,s,ma,r; void search(int x,int dep){ int p; vis[x]=1; if (dep>ma){ma=dep;r=x;} p=a[x]; while(p){ if (!vis[

SP1437 Longest path in a tree(树的直径)

应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距离即为树的直径. 证明:假设此树的最长路径是从s到t,我们选择的点为u.反证法:假设第一遍搜到的点是v. 1.v在这条最长路径上,那么dis[u,v]>dis[u,v]+dis[v,s],显然矛盾. 2.v不在这条最长路径上,我们在最长路径上选择一个点为po,则dis[u,v]>dis[u,po]

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

[LeetCode] 687. Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

LeetCode 388. Longest Absolute File Path

原题链接在这里:https://leetcode.com/problems/longest-absolute-file-path/ 题目: Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The dire

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

题目: Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of path between two nodes is represented by the number of edges between them. Exam

2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge.

[python] os.path说明

os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as either strings, or bytes.