poj2342.Anniversary party(树形dp)
2015-07-24 20:55
232 查看
[code]#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; const int maxn=1510; int a[maxn]; vector<int>G[maxn]; int vis[maxn]; int dp[maxn][2]; void init(int n){ for(int i=0;i<n;i++) G[i].clear(); mem0(vis); mem0(dp); } void dfs(int s){ vis[s]=1; dp[s][1]=1; for(int i=0;i<G[s].size();i++){ int x=G[s][i]; if(vis[x]){ continue; } dfs(x); dp[s][0]+=dp[x][1]; dp[s][1]+=min(dp[x][1],dp[x][0]); } } int main(){ int n; while(scanf("%d",&n)!=EOF){ int u,v,x; init(n); for(int i=0;i<n;i++){ scanf("%d:(%d)",&u,&x); for(int i=0;i<x;i++){ scanf("%d",&v); G[u].push_back(v); G[v].push_back(u); } } dfs(0); printf("%d\n",min(dp[0][0],dp[0][1])); } return 0; }
相关文章推荐
- Hdu5303 Delicious Apples 贪心
- struts2 模型分配问题和延迟加载问题
- cocos2d-x 3.4 VS2013无法打开包含文件extensions/ExtensionExport.h
- 首篇技术博客感想
- UVa 11361 Investigating Div-Sum Property 数字和与倍数
- 共享一个以前写的俄罗斯方块
- 数据库事物隔离级别通俗理解
- Universal-Image-Loader分析 第一篇
- 士兵队列
- AngularJs编写指令
- ubuntu android studio 安装
- 了解NoSQL
- poj 3126 Prime Path (bfs)
- 食物链
- HDU 3681 Prison Break(状压DP + 二分)
- hdu 4266 The Worm in the Apple (线段树)
- 树的重心
- treap 模板
- 【C++系列】之 运算符重载
- 狡猾的百度安全组件、百度安全防护服务