Zxa and Leaf accepts:25 submissions:249Time limit:5000/2500 MS (java/others) Memory limit:65536/65536 K (java/others)Problem description
Zxa has a tree containingNN-node tree without root, containing(n-1)(N−1) strip without the edge, point from11 toNn numbering, defining the number of degrees for each point to be connected to this point, in degrees11 of nodes are called leaf nodes of this tree. Zxa want to set its attractiveness for each node, the attractiveness must be a positive integer. His tree has no roots.M (1\leq m\leq N)m(1≤m≤n) leaf nodes, of which K (1\leq k\leq m)k(1≤k≤ m) The attractiveness of the leaf nodes has been determined, ZXA only need to set the attractiveness of the other nodes. Zxa is curious, if each side of the unsightly degree is the edge of the two nodes connected to the absolute value of the difference in the attractiveness of the whole tree is the ugliest of all sides of the maximum value, then the tree is the smallest degree of unsightly, you can help him?
Enter a description
The first line has a positive integerTT, indicating that there isTT Group data. For each set of data: The first row has two positive integersNN andKK, indicating that this tree hasNn Nodes, whereKThe attractiveness of K-leaf nodes has been determined. Next(n-1)(N−1) lines, each line has two mutually dissimilar positive integersUU andVV, which represents the nodeUU and NodeVThere is a non-forward edge between v. NextKK-line with two positive integers per lineUU andWW, which represents the nodeUU is a leaf node, and its attractiveness isWW. There is only one space between adjacent numbers in each row. Ensure that the input edges form a tree.1\leq t\leq 10,2\leq n\leq 5\cdot10^4,1\leq k\leq n,1\leq u,v\leq n,1\leq w\leq 10^91≤T≤10,2≤N≤5⋅10?4?? ,1≤k≤< Span class= "Mord mathit" >n,1≤ u,v≤n,1≤w≤10? 9
Output description
For each set of data, the output line contains a non-negative integer that represents the minimum value of the tree's unsightly size.
Input sample
23 21 21 32 43 96 21 21 31 42 52 63 65 9
Output sample
31
#include <iostream>#include<algorithm>#include<cstdio>#include<vector>#include<map>#include<queue>#include<cstring>using namespacestd;Const intmax=5e4+Ten; typedefLong LongLl;vector<int>Book[max];Const intinf=1e9+Ten;intVis1[max],a[max],vis2[max];intpnt[max*2],head[max],nxt[max*2],e;//if the number of sides is wrong, the report time-out what ghost ... InlinevoidAddedge (intUintv) {Pnt[e]=v;nxt[e]=head[u];head[u]=e++;}intMaxx[max],minx[max],limit;BOOLDfsintu) { intv; Maxx[u]=inf;minx[u]=0;//Each virtual node range is set to an infinite size if(Vis1[u]) maxx[u]=minx[u]=A[u]; for(inti=head[u];i!=-1; i=Nxt[i]) {v=Pnt[i]; if(!Vis2[v]) {Vis2[v]=1; if(!dfs (v))return 0;//Dfs First, so that all nodes have scopevis2[v]=0; Maxx[u]=min (maxx[u],maxx[v]+limit); Minx[u]=max (Minx[u],minx[v]-limit);//the scope of each node needs to meet the requirements of neighboring nodes } } returnmaxx[u]>=minx[u];//Each node must meet the criteria}BOOLCheckintLen) {Limit=Len; memset (Vis2,0,sizeof(VIS2)); if(!dfs (1))return 0; return 1;}intMain () {intT; for(SCANF ("%d", &t); T t--) {memset (head,-1,sizeof(head)); memset (NXT,-1,sizeof(NXT)); memset (PNT,-1,sizeof(PNT)); memset (Vis1,0,sizeof(VIS1)); intn,k,u,v; scanf ("%d%d",&n,&k); E=0; for(intI=1; i<=n-1; i++) {scanf ("%d%d",&u,&v); Addedge (U,V); Addedge (V,u); } for(intI=1; i<=k;i++) {scanf ("%d%d",&u,&v); A[u]=v; Vis1[u]=1; } intL=0, R=inf,ans,mid;//two points for the answer while(l<=r) {Mid= (l+r) >>1; if(Check (mid)) r=mid-1, ans=mid; ElseL=mid+1; } printf ("%d\n", ans); } return 0;}
View Code
Hdu 5682 Zxa and Leaf