Maximum independent set of trees

Source: Internet
Author: User
Tags cmath

Select a node as root, use D (i) to indicate the maximum independent set size of the subtree with I as the root node, and node I have only two decisions: Select or not. If I is selected, the son of I is not selectable, and if I is not selected, then the problem is converted to the D value of all sons who seek I. State transition equation:

where S (i) and GS (i) are the sons of I are set up with grandchildren.
Implementation method: When a D (i) is computed, it is used to update the parent node of I and the grandfather node, so that whenever the parent node can be recorded
poj2342 Anniversary Party

#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <queue>#include <deque>#include <stack>using namespace STD;Const intmaxn=6000+Ten; vector<int>NEXT[MAXN];intS[MAXN],GS[MAXN];intW[MAXN];intNintdpintCurintFA) { for(intI=0; I<next[cur].size (); i++) {intX=next[cur][i];if(X==FA)Continue;intD=DP (x,cur); S[cur]+=d;if(fa!=-1) Gs[fa]+=d; }returnMax (S[cur],gs[cur]+w[cur]);}intMain () { while(scanf("%d%d", &n,&w[1]), n| | w[1]){ for(intI=1; i<=n;i++) next[i].clear ();intx, y; for(intI=2; i<=n;i++)scanf("%d", &w[i]); for(intI=1; i<=n-1; i++) {scanf("%d%d", &x,&y);            Next[x].push_back (y);        Next[y].push_back (x); }memset(S,0,sizeof(s));memset(GS,0,sizeof(GS));printf("%d\n", DP (1,-1)); }return 0;}

poj3342 Party at Hali-bula
Map names to numbers faster.
It's a bit of a hassle to check if the answer is unique.

#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <queue>#include <deque>#include <stack>using namespace STD;Const intmaxn= $+Ten; Map<string,int>Id vector<int>NEXT[MAXN];intS[MAXN],GS[MAXN];intD[MAXN];intNvoiddpintCurintFA) { for(intI=0; I<next[cur].size (); i++) {intX=next[cur][i];if(X==FA)Continue;        DP (X,CUR); S[CUR]+=D[X];if(fa!=-1) gs[fa]+=d[x]; } D[cur]=max (s[cur],gs[cur]+1);}BOOLDfsintCurintFA) {if(next[cur].size () = =0)return true;if(d[cur]==s[cur]&&d[cur]==gs[cur]+1)return false; for(intI=0; I<next[cur].size (); i++) {intX=next[cur][i];if(X==FA)Continue;if(s[cur]>gs[cur]+1){//If the child node is taken            if(Dfs (x,cur) = =false)return false; }Else{//If you are taking a grandson knot.             for(intj=0; J<next[x].size (); j + +) {intY=NEXT[X][J];if(y==cur)Continue;if(Dfs (y,x) = =false)return false; }        }    }return true;}intMain () {stringPeox,peoy; while(scanf("%d", &n), N) {id.clear (); for(intI=1; i<=n;i++) next[i].clear ();intCnt=0;Cin>>peox; for(intI=1; i<=n-1; i++) {Cin>>peox>>peoy;if(Id.count (peox) = =0) id[peox]=++cnt;if(Id.count (peoy) = =0) id[peoy]=++cnt;intX=id[peox],y=id[peoy];            Next[x].push_back (y);        Next[y].push_back (x); }memset(d,0,sizeof(d));memset(S,0,sizeof(s));memset(GS,0,sizeof(GS)); dp1,-1);printf("%d", d[1]);puts(Dfs (1,-1)?"Yes":"No"); }return 0;}

Maximum independent set of trees

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.