HDU 2412Party at Hali-Bula(树状DP)

来源:互联网 发布:马丁靴 知乎 编辑:程序博客网 时间:2024/06/06 02:51

如何得到最大人数还是比较简单的。。。难的是如何判断唯一。。搞不出来


#include<iostream>#include<cstdio>#include<algorithm>#include<cstdlib>#include<cmath>#include<string.h>#include<cstring>#include<string>#include<map>#include<set>#include<vector>#include<ctime>#define hash hashhusing namespace std;typedef long long ll;#define sp  system("pause");int dp[450][2];int dup[450][2];int isvis[220];vector<int>G[400];int maxx = 0, ok = 0;void dfs(int root){int j, u;dp[root][0] = 0;dp[root][1] = 1;dup[root][0] = 1;dup[root][1] = 1;for (j = 0; j<G[root].size(); j++){u = G[root][j];dfs(u);dp[root][0] += max(dp[u][0], dp[u][1]);dp[root][1] += dp[u][0];if (dp[u][0]>dp[u][1] && dp[u][0] == 0) dup[root][0] = 0;else if (dp[u][1]>dp[u][0] && dp[u][1] == 0) dup[root][0] = 0;else if (dp[u][0] == dp[u][1]) dup[root][0] = 0;if (dup[u][0] == 0) dup[root][1] = 0;}}int main(){int n;while (cin >> n, n){memset(dup, 0, sizeof dup);memset(isvis, 0, sizeof isvis);maxx = 0; ok = 0;for (int i = 0; i<400; i++)G[i].clear();memset(dp, 0, sizeof dp);string name;cin >> name;int cot = 1;map<string, int>ma;ma[name] = cot++;for (int i = 0; i<n - 1; i++){cin >> name;if (!ma[name])ma[name] = cot++;int x = ma[name];cin >> name;if (!ma[name])ma[name] = cot++;int y = ma[name];G[y].push_back(x);}dfs(1);if (dp[1][0]>dp[1][1] && dup[1][0] == 1) printf("%d Yes\n", dp[1][0]);else if (dp[1][1]>dp[1][0] && dup[1][1] == 1) printf("%d Yes\n", dp[1][1]);else printf("%d No\n", max(dp[1][0], dp[1][1]));}return 0;}


0 0