多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > poj 3140 Contestants Division (樹形dp)

poj 3140 Contestants Division (樹形dp)

來源:程序員人生   發布時間:2015-06-11 08:37:02 閱讀次數:2640次

Contestants Division
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8544   Accepted: 2439

Description

In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?

Input

There are multiple test cases in the input file. Each test case starts with two integers N and M, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers st, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.

N = 0, M = 0 indicates the end of input and should not be processed by your program.

Output

For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.

Sample Input

7 6 1 1 1 1 1 1 1 1 2 2 7 3 7 4 6 6 2 5 7 0 0

Sample Output

Case 1: 1

類似 poj 2378

刪邊,求刪去某條邊后兩個分支的最小差異值;注意數據范圍。

#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> using namespace std; #define ll long long #define N 110000 #define mem(a,t) memset(a,t,sizeof(a)) const int inf=1000005; int cnt,n; struct node { int v,next; }e[N*20]; int head[N]; ll num[N]; ll ans,sum; void add(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; } void dfs(int u,int len,int fa) { int i,v; for(i=head[u];i!=⑴;i=e[i].next) { v=e[i].v; if(v!=fa) { dfs(v,len+1,u); num[u]+=num[v]; } } ll t=sum⑵*num[u]; if(t<0) t*=⑴; if(t<ans) ans=t; } int main() { //freopen("in.txt","r",stdin); int i,u,v,m,cas=1; while(scanf("%d%d",&n,&m),n||m) { for(i=1,sum=0;i<=n;i++) { scanf("%lld",&num[i]); sum+=num[i]; } cnt=0; mem(head,⑴); for(i=0;i<m;i++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } ans=sum; dfs(1,0,⑴); printf("Case %d: %lld ",cas++,ans); } return 0; }




生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 99久久精品一区二区三区 | 永久免费看片 | 日本动漫免费看 | 日本高清一区二区三区不卡免费 | 视频三区 | 日韩成人免费视频播放 | 亚洲网站在线 | 国产成人啪午夜精品网站 | 国产高清在线精品二区一 | 伊人一本之道 | 日本janpanese护士 | 亚洲免费网 | www亚洲一区| 最近2019中文字幕免费看最新 | 在线观看视频亚洲 | 最好看的毛片 | 99久久中文字幕伊人 | 欧美日韩亚洲综合久久久 | 极品色影视 | 茄子成视频片在线观看 | 黑人巨大三根一起进hd | 国产精品3| 看一级特黄a大片日本片 | 求免费黄色网址 | 欧美性色欧美a在线观看 | 精品久久免费观看 | 成人a毛片久久免费播放 | 欧美乱大交黑人 | 色网色| 亚洲a成人网77777在线 | 欧美成人在线视频 | 国产综合影院 | 亚洲欧美视频一级 | 欧美日韩一区二区三区视视频 | 欧美综合精品 | 免费日本网站 | 国产女人18毛片水真多18精品 | 久久国产精品免费一区二区三区 | 日本高清2019免费视频 | 精品一区二区三区在线视频观看 | 性久久久久久 |