"tsinsen-a1486" tree (Wang Kangning) point Division + Trie

Source: Internet
Author: User

A1486. Tree (Wang Kangning) time limit:1.0s Memory limit:512.0MB Total number of commits: 455AC Times: theAverage score: 52.62
View non-formatted question submission discussion Question Source2013 China National Training Team second assignment Problem DescriptionGive a tree of n points, each point has its own weight, small A to choose a simple path, so that the point on the path of the weight of the difference or maximum. In addition, little A has some favorite points, and he wants to go through at least one of his favorite points on this path. Input FormatThe first line consists of two integers n, K, which represent the number of points in the tree and the amount of dots on the path that contain at least small a like.
Next line n integers, each number is 0 or 1, small a like the I point when and only if the number of the first row is 1.
Next line n integers V1, V2, ..., VN, where number I indicates the weight of the first point.
The last N-1 line, two integers per line, u, V, represents an edge of the tree. output FormatIf no such simple path exists, output-1. Otherwise output the maximum XOR. Sample Input3 1
1 1 1
0 4 7
1 2
2 3 Sample Output7 Sample Input3 2
1 0 1
3 5 6
1 2
2 3 Sample Output0 Sample Input4 4
1 1 1 1
10 10 10 10
1 2
1 3
1 4 Sample Output-1 data size and conventions
Test point marking
Range of N
Range of K
Scope of VI
Other features
1
n=2
K=0


2
N≤10



3
n≤1000


This tree is a chain
4
n≤1000
K=0

This tree is a chain
5
n≤4000



6
n≤30000
K=0
Vi≤7

7
n≤40000
K=0


8
n≤40000
K=0


9
n≤50000
K=0

This tree is a chain
10
n≤50000
K=0


11
n≤60000

Vi≤7

12
n≤60000


This tree is a chain
13
n≤70000

vi≤107

14
n≤70000

vi≤107

15
n≤80000



16
n≤80000



17
n≤90000



18
n≤90000



19
n≤100000



20
n≤100000



For 100% of the data, 1≤n≤100000, 0≤k≤n, 0≤vi≤109, ensure that the input is a valid tree. Solution

This problem, first consider the weakening version.

If $k=0$ does not consider the number of critical points, it is very simple.

You can get the XOR of all the nodes directly from the root DFS and then insert all the trie, and then enumerate each point greedy on trie to run, complexity $o (NLOGN) $.

However, this problem needs to limit the number of nodes, it must be divided into +trie greedy, time complexity $o (nlog^{2}n) $.

The most common idea is to go through different key points of the path XOR and build a tree trie, and then query the time query can be, but the $k$ limit is too large.

So consider maintaining a trie tree, maintaining a critical number of points on each node, and dealing with it when the greedy statistic is answered.

Here is a problem, that is, when dealing with a subtrees tree, the top node will be repeated calculations, so you can consider not to calculate its impact, in the statistical answer to calculate back.

Code
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include < cstring>using namespace Std;inline int read () {int x=0,f=1; char Ch=getchar (); while (ch< ' 0 ' | | ch> ' 9 ') {if (ch== '- ') F=-1; Ch=getchar ();} while (ch>= ' 0 ' && ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} #define MAXN 100010int n,k,lov[maxn],c[maxn],ans=-1;struct edgenode{int next,to;} Edge[maxn<<1];int head[maxn],cnt=1;inline void Addedge (int u,int v) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; Edge[cnt].to=v;} inline void Insertedge (int u,int v) {Addedge (u,v); Addedge (v,u);} namespace Trie{int son[maxn][2],sz,d[maxn];int s[32];inline void Calc (int x) {memset (s,0,sizeof (s)); for (int t=31; t> = 0; t--) s[t]= (x>>t) &1;} inline void Clear () {son[1][0]=son[1][1]=0; sz=1;} inline void Insert (int x,int dep) {int now=1; Calc (x); for (int i=31; i>=0; i--) if (Son[now][s[i]) Now=son[now][s[i]],d[now]=max (D[NOW],DEP); else Son[now][s[i]] =++sz,now=sz,son[now][1]=SON[NOW][0]=0,D[NOW]=DEP;} inline int Query (int x,int dep) {int now=1,mx=0; Calc (x); for (int i=31; i>=0; i--) {if (son[now][s[i]^1] && d[son[now][s[i]^1]]>=dep) now=son[now][s[i]^1] , mx|= (1<<i), else if (Son[now][s[i]] && d[son[now][s[i]]]>=dep) Now=son[now][s[i]];else return-1;} return MX;}} Using namespace Trie;namespace treedivide{int size[maxn],mx[maxn],sz,root;bool visit[maxn];struct Node{int x, y; Node (int x=0,int y=0) {x=x,y=y;}} Stack[maxn];int top;inline void getroot (int now,int last) {size[now]=1,mx[now]=0;for (int i=head[now]; i; i=edge[i].next if (Edge[i].to!=last &&!visit[edge[i].to]) {getroot (edge[i].to,now); size[now]+=size[edge[i].to];mx[now]= Max (mx[now],size[edge[i].to]);} Mx[now]=max (Mx[now],sz-size[now]); if (Mx[now]<mx[root]) Root=now;} inline void DFS (int now,int last,int dep,int val,int fav) {Ans=max (Ans,query (VAL^FAV,K-DEP)); Stack[++top]=node (VAL,DEP ); for (int i=head[now]; i; i=edge[i].next) if (Edge[i].to!=last &&!visit[Edge[i].to]) {DFS (edge[i].to,now,dep+lov[edge[i].to],val^c[edge[i].to],fav);}} inline void Divide (int now) {//printf ("root=%d\n", now); visit[now]=1; Trie::clear (); K-=lov[now];if (k<=0) Ans=max (Ans,c[now]); for (int i=head[now]; i; i=edge[i].next) if (!visit[edge[i].to]) {Top=0;D FS (Edge[i].to,now,lov[edge[i].to],c[edge[i].to],c[now]); for (int j=1; j<=top; j + +) Trie::insert (stack[j].x,stack [j].y);} k+=lov[now];for (int i=head[now]; i; i=edge[i].next) if (!visit[edge[i].to]) {root=0; SZ=SIZE[EDGE[I].TO]; Getroot (Edge[i].to,now);D ivide (root);}} Using namespace Treedivide;int Main () {N=read (), K=read (); for (int. i=1; i<=n; i++) Lov[i]=read (); for (int i=1; i<=n; i + +) C[i]=read (); for (int i=1,x,y; i<=n-1; i++) X=read (), Y=read (), Insertedge (x, y), mx[root=0]=sz=n; Getroot (1,0);D ivide (Root);p rintf ("%d\n", ans); return 0;}

"tsinsen-a1486" tree (Wang Kangning) point Division + Trie

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.