Analysis tomorrow in writing, first to sleep. "Key"
Build Vector<int>child[n] (similar to the adjacency Matrix storage graph)
The decision of the subordinate relationship to a graph
The dictionary tree is mapped with a map container to solve
Dynamic Planning recursive relationships:
DP[U][1]+=DP[CHILD[U][I]][0];
Dp[u][0]+=max (Dp[child[u][i]][1],dp[child[u][i]][0]);
Uniqueness is a bit difficult to determine.
A two-dimensional array recursion relationship is also used to solve:
1. The uniqueness of the current node depends on the uniqueness of the child node you want to select and whether or not he is selected, essentially depending on the expression relationship of the dynamic array dp;
if (dp[child[u][i]][1]==dp[child[u][i]][0]) f[u][0]=0;
else if (dp[child[u][i]][0]>dp[child[u][i]][1]&&f[child[u][i]][0]==0) f[u][0]=0;
else if (dp[child[u][i]][0]<dp[child[u][i]][1]&&f[child[u][i]][1]==0) f[u][0]=0;
2. The uniqueness of the current node u depends on the existence of a child node is not unique, there is a child node is not unique, the node takes 1 of the state is 0 (0 means not unique, 1 unique)
if (f[child[u][i]][0]==0) f[u][1]=0;
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath> #include <climits> #include <map> #include <vector> #include <list> #include <stack
> #include <queue> #define EPS 1e-9 #define PI acos ( -1) #define CLR (k,v) memset (k,v,sizeof (k));
using namespace Std;
typedef long Long LL;
const int maxn=410;
const int mod=1000000009; const int SZ = 1<<20;/* struct fastio{char Inbuf[sz];char outbuf[sz];fastio () {//¸ßëù»º´æsetvbuf (stdin,inbuf,_i
OFBF,SZ);
Setvbuf (STDOUT,OUTBUF,_IOFBF,SZ);
}}io;*/vector<int>child[maxn]; map<string,int>mp;//do a string->int mapping, convenient to establish child[] int dp[maxn][2];//dp[i][0] does not contain the largest set of subtrees numbered I, dp[i][1]
The maximum set of subtree containing i bool f[maxn][2];//uniqueness judgment, also has tree node affinity, f[i][]==1 subtree unique, 0 has multiple void dfs (int u) {int size=child[u].size ();
if (size==0) {dp[u][0]=0;
Dp[u][1]=1;
return;//Recursive base} for (int i=0;i<size;++i) {DFS (child[u][i]);//The DP value of the node cannot be processed until the first node is traversedDP[U][1]+=DP[CHILD[U][I]][0];
Dp[u][0]+=max (Dp[child[u][i]][1],dp[child[u][i]][0]);
if (dp[child[u][i]][1]==dp[child[u][i]][0]) f[u][0]=0;
else if (dp[child[u][i]][0]>dp[child[u][i]][1]&&f[child[u][i]][0]==0) f[u][0]=0;
else if (dp[child[u][i]][0]<dp[child[u][i]][1]&&f[child[u][i]][1]==0) f[u][0]=0;
if (f[child[u][i]][0]==0) f[u][1]=0;
} dp[u][1]++;//can be initialized in advance, or it can be placed here} int main () {//Freopen ("Input.txt", "R", stdin);
int n;
while (Cin>>n&&n) {clr (dp,0);
CLR (f,1);//Initialize all unique int top=1;
String str1,str2;
cin>>str1;
mp[str1]=top++;
for (int i=1;i<n;i++) {cin>>str1>>str2;
if (!MP[STR1]) mp[str1]=top++;
if (!MP[STR2]) mp[str2]=top++;//do an MP form Child[mp[str2]].push_back (MP[STR1]);
} dfs (1);
for (int i=1;i<=n;i++) child[i].clear ();
Mp.clear ();
BOOL Ok=true;
if (dp[1][1]>dp[1][0]) ok=f[1][1]; else if (dp[1][1]<dp[1][0]) ok=f[1][0];
else Ok=false; printf ("%d", Max (Dp[1][0],dp[1][1])),//dp[i][0] does not contain the largest set of subtrees numbered I, dp[i][1] contains the maximum set of subtrees of I puts (ok? ").
Yes ":" No ");
} return 0;
}