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)