POJ 3140 Contestants Division (水题)
2015-07-19 12:00
204 查看
思路:记录每个节点的子节点的总和和非子节点的总和的差的绝对值,再扫一遍就完了。水题。。。和树形DP没关系。。。
代码:
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define abs(x) ((x) >= 0 ? (x) : -(x)) using namespace std; typedef __int64 LL; const LL maxn = 100005; const LL INF = 0x3f3f3f3f3f3f3f3f; struct Nod{ LL b,next; void init(LL b,LL next){ this->b=b;this->next=next; } }buf[maxn<<1]; LL len,E[maxn]; LL n,m,sum,vi[maxn],num[maxn],dp[maxn]; void init(){ len=0;sum=0; memset(E,-1,sizeof(E)); memset(dp,0,sizeof(dp)); } void add_edge(LL a,LL b){ buf[len].init(b,E[a]);E[a]=len++; buf[len].init(a,E[b]);E[b]=len++; } void dfs(LL u,LL pre){ LL i,v; num[u] = vi[u]; for(i=E[u];i!=-1;i=buf[i].next){ v=buf[i].b; if(v == pre) continue; dfs(v,u); num[u] += num[v]; } dp[u] = abs(sum - 2*num[u]); } int main(){ LL i,j,a,b,o,cas=0; while(scanf("%I64d%I64d",&n,&m),n+m){ init(); for(i=1;i<=n;i++){ scanf("%I64d",&vi[i]); sum += vi[i]; } while(m--){ scanf("%I64d%I64d",&a,&b); add_edge(a,b); } dfs(1,-1); o = INF; //cout<<sum<<endl; //for(int i=1;i<=n;i++) cout<<num[i]<<" "; cout<<endl; //for(int i=1;i<=n;i++) cout<<dp[i]<<" "; cout<<endl; for(int i=1;i<=n;i++) o = min(o,dp[i]); printf("Case %I64d: %I64d\n",++cas,o); } return 0; }
相关文章推荐
- 仅显示INPUT下边框
- 网络数据分析工具Charles的使用
- BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )
- javascript中setAttribute()函数使用方法及兼容性
- 股票学习16
- HDU 3836 Equivalent Sets(强连通分量 Tarjan缩点)
- [LeetCode][Java] Maximal Rectangle
- 有向图的强连通分量 Tarjan算法
- javaAPI(Map双列集合)
- Swift学习 --- 2.3和字符串
- C重要字符串函数
- IOS第一天
- 分别取商和余数:divmod(a, b)
- 黑马程序员——Java基础---数据类型、编码解码
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 合并同类项
- POJ 3107 Godfather (水题,树形DP)
- mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
- CSS + DIV 使用方法总结
- 重装电脑后配置cygwin
- 安装PHP