1954: Pku3764 The xor-longest Path 字典树 贪心
显然, (a,b)两点间的路径异或和等于 (1,a) 和 (1,b) 两条路径的异或和。 所以一遍dfs就可得出所有的点的异或和。
然后…………字典树上贪心乱搞了。
1 #include<cstdio> 2 #include<iostream> 3 #define rep(i,j,k) for(register int i = j; i <= k; i++) 4 #define dow(i,j,k) for(register int i = j; i >= k; i--) 5 #define maxn 100005 6 using namespace std; 7 8 inline int read() { 9 int s = 0, t = 1; char c = getchar(); 10 while( !isdigit(c) ) { if( c == '-' ) t = -1; c = getchar(); } 11 while( isdigit(c) ) s = s * 10 + c - 48, c = getchar(); 12 return s * t; 13 } 14 15 struct edge{ int to, v; edge*next; } e[maxn<<1], *pt = e, *head[maxn]; 16 inline void add(int x,int y,int v) { 17 pt->to = y, pt->next = head[x], pt->v = v, head[x] = pt++; 18 pt->to = x, pt->next = head[y], pt->v = v, head[y] = pt++; 19 } 20 21 #define ez(i,j) for(edge*i = head[j]; i; i=i->next) 22 #define to i->to 23 int val[maxn]; 24 inline void dfs(int x,int fa) { 25 ez(i,x) if( to != fa ) val[to] = val[x] ^ i->v, dfs(to,x); 26 } 27 28 int ans = 0, bin[32], all[32]; 29 int tot = 0, ch[3000000][2]; 30 inline void insert(int v) { 31 int p = 0; 32 dow(i,30,0) 33 if( v & bin[i] ) { 34 if( ch[p][1] ) p = ch[p][1]; else p = ch[p][1] = ++tot; 35 } else { 36 if( ch[p][0] ) p = ch[p][0]; else p = ch[p][0] = ++tot; 37 } 38 } 39 40 inline void query(int v) { 41 int p = 0, now = 0; 42 dow(i,30,0) { 43 if( v & bin[i] ) { 44 if( ch[p][0] ) now += bin[i], p = ch[p][0]; else p = ch[p][1]; 45 } else { 46 if( ch[p][1] ) now += bin[i], p = ch[p][1]; else p = ch[p][0]; 47 } 48 if( now + all[i-1] <= ans ) return; 49 } ans = max(ans,now); 50 } 51 52 int main() { 53 bin[0] = 1; rep(i,1,30) bin[i] = bin[i-1] << 1; 54 all[0] = 1; rep(i,1,30) all[i] = all[i-1] | bin[i]; 55 int x, y, n = read(); 56 rep(i,1,n-1) x = read(), y = read(), add(x,y,read()); 57 dfs(1,0); 58 rep(i,1,n) insert(val[i]); 59 rep(i,1,n) query(val[i]); 60 cout<<ans<<endl; 61 return 0; 62 }
————————————————