poj 3140(树形dp)
来源:互联网 发布:兔子白网络 编辑:程序博客网 时间:2024/06/15 00:46
题目链接:http://poj.org/problem?id=3140
思路:简单树形dp题,dp[u]表示以u为根的子树的人数和。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<climits> 7 #include<algorithm> 8 #include<stack> 9 #include<map>10 #include<set>11 #include<vector>12 #include<queue>13 #include<list>14 using namespace std;15 #define MAXN 11111116 #define inf 1<<3017 #define INF 1LL<<6018 typedef long long ll;19 typedef pair<int,int>PP;20 template<class T> inline T Get_MIN(const T &a,const T &b){ return a < b ? a : b; }21 template<class T> inline T Get_MAX(const T &a,const T &b){ return a > b ? a : b; }22 template<class T> inline T ABS(const T &a){ return a < 0 ? -a : a; }23 24 struct Edge{25 int v,next;26 }edge[MAXN<<4];27 28 int n,m,NE;29 int head[MAXN];30 31 void Insert(int u,int v)32 {33 edge[NE].v=v;34 edge[NE].next=head[u];35 head[u]=NE++;36 }37 38 ll dp[MAXN],sum,MIN;39 void dfs(int u,int father)40 {41 for(int i=head[u];i!=-1;i=edge[i].next){42 int v=edge[i].v;43 if(v==father)continue;44 dfs(v,u);45 dp[u]+=dp[v];46 }47 MIN=Get_MIN(MIN,ABS((dp[u]-(sum-dp[u]))));48 // cout<<u<<"**"<<dp[u]<<"****"<<abs(sum-dp[u])<<"**"<<MIN<<endl;49 }50 51 52 int main()53 {54 int u,v,t=1;55 while(~scanf("%d%d",&n,&m)&&(n||m)){56 NE=0;57 memset(head,-1,sizeof(head));58 sum=0;59 for(int i=1;i<=n;i++)scanf("%lld",&dp[i]),sum+=dp[i];60 while(m--){61 scanf("%d%d",&u,&v);62 Insert(u,v);63 Insert(v,u);64 }65 MIN=INF;66 dfs(1,-1);67 printf("Case %d: %lld\n",t++,MIN);68 }69 return 0;70 }
0 0
- poj 3140 树形dp
- poj-3140-树形dp
- POJ 3140 树形dp
- poj 3140(树形dp)
- POJ 3140 树形DP
- POJ 3140 树形dp
- poj 3140(树形dp)
- POJ 3140(简单树形DP)
- poj 1655树形dp && poj 3107 &&poj 2378 && poj 3140
- Contestants Division - POJ 3140 树形dp
- poj 3140 Contestants Division(树形dp)
- POJ 3140 Contestants Division(树形DP)
- poj 3140 Contestants Division (树形dp)
- POJ 3140 Contestants Division(树形DP)
- [poj 3140] Contestants Division 树形DP
- POJ 3140 Contestants Division 简单树形DP
- POJ 3140 Contestants Division 树形dp
- 【POJ】3140 Contestants Division 树形dp
- loj 1046(bfs)
- loj 1055(bfs)
- loj 1165(bfs+康托展开)
- loj 1185(bfs)
- ural 1018(树形dp)
- poj 3140(树形dp)
- loj 1034(最小点基)
- poj 2486( 树形dp)
- poj 1935(树形dp)
- loj 1168(Tarjan应用)
- loj 1004(dp)
- poj 2378(树形dp)
- poj 1463(树形dp)
- loj 1011(状态压缩+记忆化搜索)