博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3140
阅读量:7016 次
发布时间:2019-06-28

本文共 1800 字,大约阅读时间需要 6 分钟。

#include
#include
#include
#include
#define MAXN 1000010 #define LL long long const LL INF = 0x7fffffffffffff; using namespace std; typedef struct{ int to,next; }Node; Node edge[2*MAXN]; int vis[MAXN/10],head[MAXN/10],val[MAXN/10]; LL sum[MAXN/10],all_sum,ans; LL My_abs(LL tmp){return tmp < 0 ? -tmp : tmp;} void addedge(int u,int v,int k){ edge[k].to = v; edge[k].next = head[u]; head[u] = k; } LL dfs_sum(int s){ vis[s] = 1; sum[s] = val[s]; for(int i = head[s];~i;i = edge[i].next){ int u = edge[i].to; if(!vis[u]) sum[s] += dfs_sum(u); } return sum[s]; } void dfs_minabs(int s){ vis[s]= 1; for(int i = head[s];~i;i = edge[i].next){ int u = edge[i].to; if(!vis[u]){ dfs_minabs(u); ans = min(ans,My_abs(all_sum - 2*sum[u])); } } } int main(){ int n,m,cas = 0; freopen("in.c","r",stdin); while(~scanf("%d%d",&n,&m) && (m+n) ){ int k = 1,tmp,u,v; all_sum = 0; for(int i = 1;i <= n;i ++) scanf("%d",&tmp),val[i] = tmp,all_sum += tmp; memset(head,-1,sizeof(head)); for(int i = 0;i < m;i ++){ scanf("%d%d",&u,&v); addedge(u,v,k++); addedge(v,u,k++); } memset(vis,0,sizeof(vis)); dfs_sum(1); ans = INF; memset(vis,0,sizeof(vis)); dfs_minabs(1); if(n == 1) ans = val[1]; printf("Case %d: %lld\n",++cas,ans); } return 0; }

转载于:https://www.cnblogs.com/wangzhili/p/3950265.html

你可能感兴趣的文章
聊聊flink的StateDescriptor
查看>>
git 使用教程,常用命令
查看>>
使用SVI实现Vlan间路由
查看>>
Linux学习笔记5月28日任务
查看>>
解决Td内容为空时不显示边框的问题-兼容IE、firefox、chrome
查看>>
SylixOS x86中断探测(二)
查看>>
HDFS总结
查看>>
scala 中导出excel
查看>>
http长轮询&短轮询
查看>>
Android 应用换肤功能(白天黑夜主题切换)
查看>>
Linux编程操作知识整理(continued)
查看>>
2012.8.13 onEnter与触摸事件
查看>>
基于 HTML5 WebGL 的 3D 棉花加工监控系统
查看>>
[redis] 获得 database, key, value
查看>>
swift之mutating关键字
查看>>
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的W...
查看>>
eclipse连接android设备的问题
查看>>
.pem引发的血案
查看>>
如何在rootscope 获取angular ui的tab子域 scope 也叫子域暴露
查看>>
HashMap,HashTable,HashSet区别
查看>>