Question: Ultraviolet A 1220-party at Hali-Bula
A company employee wants to hold a party and asks anyone not to be present with his direct supervisor at the same time. An employee has only one Support Supervisor. How many people can be present at most and whether the solution is unique?
Analysis: It is found that the largest independent set of a tree is required. Here we can use the tree-like DP Solution.
Define DP [x] [0]: indicates that X is the largest independent set of the subtree if I is not selected, and DP [x] [1] indicates the largest independent set in the presence of X.
Define f [x] [0]: indicates whether the subtree with X as the root and X is unique, f [x] [1] indicates whether the subtree with X as the root and X selected is unique
State transition equation: DP [x] [1] + = DP [child] [0];
DP [x] [0] + = max (DP [child] [0], DP [child] [1]);
The equation for determining uniqueness is the same.
AC code:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <vector> # include <algorithm> # include <map> using namespace STD; # define maxn 65540 using namespace STD; const int INF = 0x3f3f3f3f; const int n = 300; vector <int> child [N]; Map <string, int> V; int DP [N] [3]; bool f [N] [3]; // unique void DFS (int u) {If (child [u]. size () = 0) {DP [u] [0] = 0; DP [u] [1] = 1; return;} int size = Child [u]. size (); (INT I = 0; I <size; I ++) {DFS (child [u] [I]); if (F [child [u] [I] [0]) f [u] [1] = 1; DP [u] [1] + = DP [child [u] [I] [0]; if (DP [child [u] [I] [0]> DP [child [u] [I] [1]) {DP [u] [0] + = DP [child [u] [I] [0]; If (F [child [u] [I] [0]) f [u] [0] = 1;} else {DP [u] [0] + = DP [child [u] [I] [1]; if (DP [child [u] [I] [1] = DP [child [u] [I] [0] | f [child [u] [I] [1]) f [u] [0] = 1 ;}} DP [u] [1] ++ ;}int main () {int N; while (~ Scanf ("% d", & N) {memset (DP, 0, sizeof (DP); memset (F, 0, sizeof (f )); int Top = 1; string S1, S2; CIN> S1; V [S1] = Top ++; For (INT I = 1; I <n; I ++) {CIN> S1> S2; If (! V [S1]) V [S1] = Top ++; If (! V [s2]) V [s2] = Top ++; child [V [s2]. push_back (V [S1]);} DFS (1); If (DP [1] [1] = DP [1] [0]) printf ("% d No \ n", DP [1] [1]); else if (DP [1] [1]> DP [1] [0]) printf ("% d % s \ n", DP [1] [1], F [1] [1]? "No": "Yes"); else printf ("% d % s \ n", DP [1] [0], F [1] [0]? "No": "Yes"); For (INT I = 1; I <= N; I ++) child [I]. clear (); V. clear () ;}return 0 ;}
Ultraviolet
12186 |
Another crisis code |
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <algorithm>using namespace std;#define maxn 65540using namespace std;const int inf = 0x3f3f3f3f;const int N = 101000;int fa[N];vector<int> child[N];int dp[N];int n,k;void dfs(int x){ int tmp = child[x].size() * k; if(tmp%100==0) tmp = tmp/100; else tmp = tmp/100+1; vector<int> pps; for(int i=0;i<child[x].size();i++) { int ff = child[x][i],tt = 0; dfs(ff); tt = dp[ff] ; if(child[ff].size()==0) tt++; pps.push_back(tt); } int ans = 0; sort(pps.begin(),pps.end()); for(int i=0;i<tmp;i++) ans+=pps[i]; dp[x] = ans; return ;}int main(){ while(~scanf("%d%d",&n,&k) && n+k) { memset(dp,inf,sizeof(dp)); for(int i=1;i<=n;i++){ scanf("%d",&fa[i]); child[fa[i]].push_back(i); } dfs(0);// for(int i=0;i<=n;i++)// printf("CAS:%d %d\n",i,dp[i]); printf("%d\n",dp[0]); for(int i=0;i<=n;i++) child[i].clear(); } return 0;}
Ultraviolet A 1220-party at Hali-Bula [entry tree DP]