【Uva 1220】Party at Hali-Bula
2017-10-04 18:44
351 查看
【Link】:https://cn.vjudge.net/contest/170078#problem/M
【Description】
求一个树的最大独立子集;
(即树的一个点集,这个点集中任意两个点,都不相连)
然后,问你这个点集是不是唯一的;
【Solution】
设d[i][0],f[i][0],表示以i为根节点的子树在i节点不选的时候的最大独立子集,f[i][0],表示在不选i的时候,这个最大独立子集是否唯一;
d[i][1],f[i][1]的定义类似,只不过变成了i这个点不选;
对于第i个节点要选的情况;
儿子节点只能都不选
d[i][0]=∑d[j][1]
(j为i的儿子,当f[j][1]全部为true,f[i][0]才为true)
对于第i个节点不选的情况
儿子节点可选、可不选
d[i][1]=∑max(d[j][0],d[j][1])
这里,如果d[j][0]==d[j][1],或者d[j][0]和d[j][1]中那个较大值的f值为false;
则f[i][1]为false;否则为true;
最后,在根节点1上面加一个0节点
直接输出d[0][1];
->因为0节点不选的话,对应1节点可选可不选;
然后根据f[0][1]判断YES和NO;
【NumberOf WA】
1
【Reviw】
d[y][0]写成了dy[0]…
然后dy是那个模板里的四个方向数组….
【Code】
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) #define Open() freopen("F:\\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 210; int n,cnt; string boss; map <string,int> dic; vector <int> g ; int d [2]; bool f [2]; void dfs(int x){ d[x][0] = 1,f[x][0] = true; d[x][1] = 0,f[x][1] = true; int len = g[x].size(); rep1(i,0,len-1){ int y = g[x][i]; dfs(y); d[x][0]+=d[y][1],f[x][0] = f[x][0] && f[y][1]; if (d[y][0]==d[y][1]) f[x][1] = false,d[x][1] += d[y][1]; if (d[y][0] > d[y][1]){ d[x][1] += d[y][0]; if (!f[y][0]) f[x][1] = false; } if (d[y][0] < d[y][1]){ d[x][1] += d[y][1]; if (!f[y][1]) f[x][1] = false; } } } int main(){ //Open(); Close(); while (cin >> n && n){ rep1(i,0,n) g[i].clear(); cnt = 0; dic.clear(); cin >> boss; dic[boss] = ++cnt; string x,y; rep1(i,1,n-1){ cin >> x >> y; if (dic[x]==0) dic[x]=++cnt; if (dic[y]==0) dic[y]=++cnt; int xx = dic[x],yy = dic[y]; g[yy].pb(xx); } g[0].pb(1); dfs(0); cout << d[0][1] <<' '; if (f[0][1]) cout <<"Yes"<<endl; else cout <<"No"<<endl; } return 0; }
相关文章推荐
- UVA - 1220 Party at Hali-Bula 树的最大独立集
- Uva-1220-Party at Hali-Bula
- uva 1220 Party at Hali-Bula 树形DP
- UVA 1220 Party at Hali-Bula (树形dp--树的最大独立集)
- 【UVa1220】Party at Hali-Bula(树形DP)
- 100道动态规划——36 UVA 1220 Party at Hali-Bula 树形DP 最大独立集 思考欠妥
- Uva-1220 Party at Hali-Bula
- uva1220Party at Hali-Bula(树形dp)
- UVA - 1220 Party at Hali-Bula(树形dp)
- Party at Hali-Bula UVA - 1220 (简单树形dp)
- Party at Hali-Bula UVA - 1220
- UVa 1220 (树的最大独立集) Party at Hali-Bula
- UVa1220 Party at Hali_Bula
- UVA 1220(p282)----Party at Hali-Bula
- uva 1220 - Party at Hali-Bula
- UVA1220Party at Hali-Bula(树形dp)
- 【UVA】1220-Party at Hali-Bula?(动态规划,树)
- UVa1220 Party at Hali-Bula
- Party at Hali-Bula UVA - 1220
- UVa1220 - Party at Hali-Bula