CCPC 2017 Harbin L. Color a Tree && HDU 6241

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=6241

Test instructions: Give you a tree with n nodes, each node initial color is white, there is a condition: node x_i the number of black nodes is not less than y_i, and there are B conditions, in addition to the node X_j and its sub-tree at least y_j nodes, the minimum to dye the number of black nodes to meet a + B Conditions.

Solving: Reference from: 78523559

#include <iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<Set>#include<bitset>using namespacestd;#definell Long Long#defineull unsigned long Long#defineMST (b) memset ((a), (b), sizeof (a))#definePII pair<int,int>#definePII pair<ll,ll>#definePi ACOs (-1)#definePB Push_backConst DoubleEPS = 1e-6;Const intINF =0x3f3f3f3f;Constll INF =0x3f3f3f3f3f3f3f3f;Const intMAXN = 1e5 +Ten;Const intMAXM = 1e6 +Ten;intN;vector<int>VEC[MAXN];intSIZ[MAXN],B[MAXN],MN[MAXN],MX[MAXN];intUP[MAXN],DOWN[MAXN];voidinit () { for(inti =0; I <= N; i++) {vec[i].clear (); B[i]= Mn[i] =0; }}voidGetSize (intUintFA) {Siz[u]=1;  for(inti =0; I < vec[u].size (); i++) {        intv =Vec[u][i]; if(v = = FA)Continue;        GetSize (V,u); Siz[u]+=Siz[v]; }}BOOLDfsintUintFA) {    intL =0, r =1;  for(inti =0; I < vec[u].size (); i++) {        intv =Vec[u][i]; if(v = = FA)Continue; if(!dfs (V,u))return false; L+ = Down[v], r + =Up[v]; } Down[u]= Max (L, Mn[u]), up[u] =min (R, Mx[u]); returnDown[u] <=up[u];}BOOLCheckintx) { for(inti =1; I <= N; i++) {Mx[i]= min (X-B[i],siz[i]); if(Mx[i] <0)return false; }    returnDfs1,0) && down[1] <= x && x <= up[1];}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif    intT; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);        Init ();  for(inti =1; I < n; i++) {            intu,v; scanf ("%d%d",&u,&v);            VEC[U].PB (v);        VEC[V].PB (U); } getsize (1,0); intm; BOOLFlag =true; scanf ("%d",&m);  while(m--) {            intx, y; scanf ("%d%d",&x,&y); MN[X]=Max (mn[x],y); if(Y > siz[x]) flag =false; } scanf ("%d",&m);  while(m--) {            intx, y; scanf ("%d%d",&x,&y); B[X]=Max (b[x],y); if(Y > n-siz[x]) flag =false; }        if(!flag) {Puts ("-1"); Continue; }        intL =0, R = N, ans =-1;  while(L <=r) {intMid = (L + r) >>1; if(Check (mid)) ans = Mid, R = mid-1; ElseL = mid +1; } printf ("%d\n", ans); }    return 0;}

CCPC 2017 Harbin L. Color a Tree && HDU 6241

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.