poj 3342Party at Hali-Bula
2015-02-25 14:12
357 查看
最大独立集问题,加上判断唯一性
入门经典例题,不多说了
入门经典例题,不多说了
/*********************************************** * Author: fisty * Created Time: 2015/2/25 13:07:12 * File Name : uva1220.cpp *********************************************** */ #include <iostream> #include <cstring> #include <deque> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <bitset> #include <algorithm> using namespace std; #define Debug(x) cout << #x << " " << x <<endl #define Memset(x, a) memset(x, a, sizeof(x)) const int INF = 0x3f3f3f3f; typedef long long LL; typedef pair<int, int> P; #define FOR(i, a, b) for(int i = a;i < b; i++) #define MAX_N 210 int n; int dp[MAX_N][2]; int f[MAX_N][2]; int tot; map<string, int> mp; vector<int> G[MAX_N]; void dfs(int u){ for(int i = 0;i < G[u].size(); i++){ int v = G[u][i]; dfs(v); // u不选 dp[u][0] += max(dp[v][1], dp[v][0]); if(dp[v][0] == dp[v][1]) f[u][0] = 0; else if(dp[v][1] > dp[v][0] && !f[v][1]) f[u][0] = 0; else{ if(dp[v][0] > dp[v][1] && !f[v][0]) f[u][0] = 0; } // u选 dp[u][1] += dp[v][0]; if(f[v][0] == 0) f[u][1] = 0; } } void init(){ tot = 0; mp.clear(); Memset(dp, 0); Memset(G, 0); FOR(i, 0, n+1){ dp[i][1] = 1; f[i][1] = f[i][0] = 1; } } int main() { //freopen("in.cpp", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); while(cin >> n){ if(!n) break; init(); string boos; cin >> boos; mp[boos] = ++tot; for(int i = 1;i < n; i++){ string employee; cin >> employee >> boos; if(!mp[employee]) mp[employee] = ++tot; if(!mp[boos]) mp[boos] = ++tot; G[mp[boos]].push_back(mp[employee]); } dfs(1); if(dp[1][0] > dp[1][1]){ cout << dp[1][0] << " "; if(f[1][0] == 1) cout << "Yes" << endl; else cout << "No" << endl; }else if(dp[1][0] == dp[1][1]){ cout << dp[1][0] << " "; cout << "No" << endl; }else{ cout << dp[1][1] << " "; if(f[1][1] == 1) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; }
相关文章推荐
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- POJ - 3342 Party at Hali-Bula
- [poj 3342]Party at Hali-Bula 树形dp
- poj 3342 Party at Hali-Bula
- POJ 3342 Party at Hali-Bula 树形DP 最大独立集
- POJ3342——Party at Hali-Bula
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
- poj 3342 Party at Hali-Bula 树状dp
- poj 3342 Party at Hali-Bula 判断二分图最大点独立集是否唯一
- POJ 3342 Party at Hali-Bula / HDU 2412 / UVAlive 3794 / UVA 1220
- poj 3342 Party at Hali-Bula(树形dp)
- POJ - 3342 - Party at Hali-Bula (树形dp + 判断唯一解)
- poj - 3342 Party at Hali-Bula
- POJ 3342 - Party at Hali-Bula 树形DP+最优解唯一性判断
- POJ 3342 Party at Hali-Bula(树形DP)
- poj&nbsp;3342&nbsp;Party&nbsp;at&nbsp;Hali-Bula&nbsp;(树…
- POJ 3342 Party at Hali-Bula
- POJ - 3342 Party at Hali-Bula 树形DP
- poj 3342 Party at Hali-Bula(树形dp)
- POJ 3342 Party at Hali-Bula