Tree-shaped DP Water Quiz

Source: Internet
Author: User

BZOJ1131: [Poi2008]sta

Test instructions: Give a tree of n points, and find a point to which the root of the tree, the sum of the depths of all points is the maximum.

The puzzle: Record the depth of each point, and then derive the depth and level of the other points based on the depth of the root node and the layers.

I'm using BFS.

#include <stdio.h>#include<string.h>#include<iostream>#include<queue>using namespaceStd;typedefLong Longll;intN,cnt,ans;inthead[1000010],next[2000010],to[2000010],fa[1000010],s[1000010];ll f[1000010],g[1000010],siz[1000010];queue<int>Q;voidAddintAintb) {to[cnt]=b; NEXT[CNT]=Head[a]; Head[a]=cnt++;}intMain () {scanf ("%d",&N); memset (Head,-1,sizeof(head)); intI,a,b,u;  for(i=1; i<n;i++) {scanf ("%d%d",&a,&b);        Add (A, b);    Add (B,a); } Q.push (1);  while(!Q.empty ()) {u=Q.front ();        Q.pop (); s[++s[0]]=T; Siz[u]++;  for(i=head[u];i!=-1; i=Next[i]) {            if(to[i]!=Fa[u]) {Fa[to[i]]=u;            Q.push (To[i]); }        }    }     for(i=n;i>=2; i--) {siz[fa[s[i] ]+=Siz[s[i]]; F[fa[s[i] ]+=siz[s[i]]+f[s[i]];//F[i] Represents the sum of all sons of I to the depth of I} g[1]=f[ans=1];  for(i=2; i<=n;i++) {G[s[i]]=g[fa[s[i]]]+n-(siz[s[i]]<<1);//G[i] Represents the sum of all points to I, which can be launched by G[fa[i]]        if(g[ans]<g[s[i]]| | (g[ans]==g[s[i]]&&ans>S[i])) Ans=S[i]; } printf ("%d", ans); return 0;}
View Code

Poj3107:godfather

Test instructions: Find a point in the tree so that when this point is removed, the maximum number of all remaining subtree nodes is minimized, to satisfy all the required nodes.

Solution: Find out the number of nodes per subtree size[x], the answer is min (size[y],n-size[x]), Y is the son of X.

#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;Const intmaxn=50010;intN,cnt,minn;inthead[maxn],to[maxn<<1],next[maxn<<1],S[MAXN],F[MAXN],STA[MAXN];intReadin () {intret=0;CharGC;  while(gc<'0'|| Gc>'9') gc=GetChar ();  while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret;}voidAddintAintb) {to[cnt]=b; NEXT[CNT]=Head[a]; Head[a]=cnt++;}voidDfsintXintFA) {S[x]=1;  for(inti=head[x];i!=-1; i=Next[i]) {        if(to[i]!=FA)            {DFS (TO[I],X); S[X]+=S[to[i]]; F[X]=Max (F[x],s[to[i]]); }} F[x]=max (f[x],n-s[x]); Minn=min (minn,f[x]);}intMain () {n=Readin (); memset (Head,-1,sizeof(head)); inti,a,b;  for(i=1; i<n;i++) {a=readin (), b=Readin ();    Add (A, B), add (B,a); } Minn=1<< -; DFS (1,0);  for(i=1; i<=n;i++)if(F[i]==minn) sta[++sta[0]]=i;  for(i=1; i<sta[0];i++) printf ("%d", Sta[i]); printf ("%d", sta[sta[0]]); return 0;}
View Code

Poj1655:balancing Act

Test instructions: With POJ3107

#include <stdio.h>#include<iostream>#include<string.h>using namespacestd;Const intmaxn=20010;intn,minn,ans,cnt;ints[maxn],to[maxn<<1],next[maxn<<1],HEAD[MAXN];intReadin () {intret=0;CharGC;  while(gc<'0'|| Gc>'9') gc=GetChar ();  while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret;}voidAddintAintb) {to[cnt]=b; NEXT[CNT]=Head[a]; Head[a]=cnt++;}voidDfsintXintFA) {    intt=0; S[X]=1;  for(inti=head[x];i!=-1; i=Next[i]) {        if(to[i]!=FA)            {DFS (TO[I],X); S[X]+=S[to[i]]; T=Max (T,s[to[i]]); }} t=max (t,n-s[x]); if(t<minn| |    (T==minn&&x<ans)) ans=x,minn=t;}voidWork () {n=Readin (); memset (Head,-1,sizeof(head)); Ans=cnt=0, minn=1<< -; inti,a,b;  for(i=1; i<n;i++) {a=readin (), b=Readin ();    Add (A, B), add (B,a); } DFS (1,0); printf ("%d%d\n", Ans,minn);}intMain () {intt=Readin ();  while(t--) work (); return 0;}
View Code

bzoj1827:[usaco2010 Mar]gather Cow Big Rally

Test instructions: Similar to the previous question, except that different points have different cattle to live in, asking for the smallest total distance from all cows to the core

The puzzle: See http://www.cnblogs.com/CQzhangyu/p/6178852.html

Tree-shaped DP Water Quiz

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.