poj 3140 Contestants Division
来源:互联网 发布:华南农业大学网络教育 编辑:程序博客网 时间:2024/05/28 11:30
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 s, t, 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 61 1 1 1 1 1 11 22 73 74 66 25 70 0
Sample Output
Case 1: 1
Source
题目大意给你一颗树。叫你找到一条边使得去掉这条边后形成的两棵子树的结点数相差最小。开始因为m的范围纠结半天按常识都知道m顶多为n-1的看了半天。结果还是硬着头皮写了。结果根本跟m没多大关系。。。目测数据有点大直接用__int64了。
#include <iostream>#include<algorithm>#include<string.h>#include<stdio.h>#define MAX(a,b) ((a)>(b)?(a):(b))#define positive(a) ((a)>0?(a):-(a))using namespace std;struct node1//点结构{ __int64 sum;//存子树的总结点数} points[100010];struct node2//边结构{ int to;//终点 node2 *next;} edge[1000010],*head[100010];__int64 psum[100010],temp,ans;int cnt,ptr,n,m;void adde(int f,int s)//加边{ edge[cnt].next=head[f]; edge[cnt].to=s; head[f]=&edge[cnt++];}int ma(int a,int b){ return a>b?a:b; }void dfs(int fa,int son)//以s为根结点遍历树。计算每颗子树树的总结点数{ node2 *p=head[son]; while(p!=NULL) { if(p->to!=fa) { dfs(son,p->to); points[son].sum+=points[p->to].sum; } p=p->next; } psum[ptr++]=points[son].sum; //printf("%d %I64d\n",son,psum[ptr-1]); //getchar();}void solve(int s){ int i; ptr=0; dfs(0,s); temp=points[s].sum; ans=100000000000000LL; for(i=0;i<ptr-1;i++) { psum[i]=positive(temp-2*psum[i]); if(psum[i]<ans)//把满足条件的结点存在数组中 ans=psum[i]; } if(ptr==1) ans=0;}int main(){ int i,a,b,cas=1; while(scanf("%d%d",&n,&m),n||m) { memset(head,0,sizeof head); cnt=0; for(i=1;i<=n;i++) scanf("%I64d",&points[i].sum); for(i=0;i<m;i++) { scanf("%d%d",&a,&b); adde(a,b); adde(b,a); } solve(1); printf("Case %d: %I64d\n",cas++,ans); } return 0;}
- poj 3140 Contestants Division
- POJ--3140--Contestants Division
- poj 3140 Contestants Division
- poj 3140 Contestants Division
- POJ 3140 Contestants Division
- POJ 3140 Contestants Division
- Poj 3140 Contestants Division
- POJ-3140-Contestants Division
- POJ 3140 Contestants Division (DFS)
- POJ 3140 Contestants Division (水题)
- poj 3140 Contestants Division 水题
- poj 3140 Contestants Division DFS
- POJ 3140 Contestants Division 笔记
- poj 3140 Contestants Division(树dp)
- poj 3140 Contestants Division(树dp)
- Contestants Division - POJ 3140 树形dp
- poj 3140 Contestants Division(树形dp)
- poj 3140 Contestants Division 树的遍历
- 电商的一些文字
- 深度围观block:第一集&第二集
- Memcached 学习笔记(三)——多节点测试
- Foreach语句,Throw语句,Try,catch语句的使用,报告编译错误
- poj 2226 Muddy Fields(二分图匹配)
- poj 3140 Contestants Division
- DB2存储过程
- 32+激发灵感的HTML5/CSS3网页设计教程
- HTML中的转义字符
- Android 解析和生成XML文件
- Python egg学习笔记
- linux的c中signal函数
- Android中Gif图片的显示
- 删最小值(单链表)