POJ 3107 Godfather (tree-shaped DP)

Source: Internet
Author: User

Title Address: POJ 3107

Tree-shaped DP water problem. Records the maximum number of nodes for each sub-tree of each point, as well as the summary points for all subtrees. And then traverse through to find the line.

The code is as follows:

#include <iostream> #include <string.h> #include <math.h> #include <queue> #include < algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>using namespace std; #define LL __int64#define Pi ACOs ( -1.0) const int Mod=100000000;const int Inf=0x3f3f3f3f;const double eqs=1e -8;int dp[60000], head[60000], CNT, tot[60000], c[60000], num, min1;struct node {int u, V, next;} edge[120000];voi        d Add (int u, int v) {edge[cnt].v=v;        Edge[cnt].next=head[u]; head[u]=cnt++;}        void Dfs (int u, int fa) {tot[u]=1;                for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;                if (V==FA) continue;                DFS (V,U);                Dp[u]=max (Dp[u],tot[v]);        TOT[U]+=TOT[V];        }}void init () {memset (head,-1,sizeof (head));        Memset (Dp,0,sizeof (DP));        cnt=0;        num=0; Min1=inf;}        int main () {int n, I, J, u, V; while (sCANF ("%d", &n)!=eof) {init ();                        for (I=1; i<n; i++) {scanf ("%d%d", &u,&v);                        Add (U,V);                Add (V,u);                } dfs (1,-1);                        for (I=1; i<=n; i++) {Dp[i]=max (dp[i],n-tot[i]);                        if (Min1>dp[i]) {min1=dp[i];                                }} for (I=1; i<=n; i++) {if (dp[i]==min1) {                        C[num++]=i;                        }} for (i=0; i<num; i++) {printf ("%d", c[i]);                if (i!=num-1) printf ("");        } puts (""); } return 0;}


POJ 3107 Godfather (tree-shaped DP)

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.