Contestants Division

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 3694
64-bit integer IO format: %lld      Java class name: Main

 
 

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 <tex2html_verbatim_mark>and M <tex2html_verbatim_mark>, (1N100000, 1M1000000) <tex2html_verbatim_mark>, the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 toN <tex2html_verbatim_mark>. The next line has N <tex2html_verbatim_mark>integers; the Kth <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>lines has two integers s <tex2html_verbatim_mark>, t <tex2html_verbatim_mark>, and describes a communication line connecting university s <tex2html_verbatim_mark>and university t <tex2html_verbatim_mark>. All communication lines of this new system are bidirectional.

N = <tex2html_verbatim_mark>0M = <tex2html_verbatim_mark>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

Source

 
解题:dfs,去掉一条边,求两棵树点权和的最小的差值
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = ;
vector<int>g[maxn];
LL ans,cnt[maxn],sum;
int n,m,w[maxn];
bool vis[maxn];
void dfs(int u){
vis[u] = true;
cnt[u] = w[u];
LL temp;
for(int i = ,sz = g[u].size(); i < sz; i++){
if(vis[g[u][i]]) continue;
dfs(g[u][i]);
cnt[u] += cnt[g[u][i]];
if(sum - cnt[g[u][i]] >= cnt[g[u][i]])
temp = sum - *cnt[g[u][i]];
else temp = *cnt[g[u][i]] - sum;
if(temp < ans) ans = temp;
}
}
int main() {
int i,u,v,k = ;
while(~scanf("%d %d",&n,&m),n||m){
sum = ;
for(i = ; i <= n; i++){
scanf("%d",w+i);
sum += w[i];
g[i].clear();
vis[i] = false;
cnt[i] = ;
}
ans = INF;
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs();
printf("Case %d: %I64d\n",k++,ans);
}
return ;
}

更快的邻接表,链式前向星。。。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
};
LL ans,cnt[maxn],sum;
int n,m,w[maxn],head[maxn],tot;
bool vis[maxn];
arc g[maxn*];
void add(int u,int v){
g[tot].to = v;
g[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u){
vis[u] = true;
cnt[u] = w[u];
LL temp;
for(int i = head[u]; i != -; i = g[i].next){
if(vis[g[i].to]) continue;
dfs(g[i].to);
cnt[u] += cnt[g[i].to];
if(sum - cnt[g[i].to] >= cnt[g[i].to])
temp = sum - *cnt[g[i].to];
else temp = *cnt[g[i].to] - sum;
if(temp < ans) ans = temp;
}
}
int main() {
int i,u,v,k = ;
while(~scanf("%d %d",&n,&m),n||m){
sum = ;
tot = ;
for(i = ; i <= n; i++){
scanf("%d",w+i);
sum += w[i];
head[i] = -;
vis[i] = false;
cnt[i] = ;
}
ans = INF;
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
dfs();
printf("Case %d: %I64d\n",k++,ans);
}
return ;
}

BNUOJ 9870 Contestants Division的更多相关文章

  1. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...

  2. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  3. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  4. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  5. POJ 3104 Contestants Division

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10597   Accepted:  ...

  6. POJ 3140 Contestants Division

    题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...

  7. POJ-3140 Contestants Division (树)

    题目大意:一棵树,带点权.将这棵树分成两部分,找出使得两部分的点权和的差最小. 题目分析:直接dfs即可.找出每棵子树u的点权和size(u),如果以u和它的父节点之间的边为界,那么两边的点权和分别为 ...

  8. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  9. POJ 3140 Contestants Division 【树形DP】

    <题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...

随机推荐

  1. 数学 Codeforces Round #282 (Div. 2) B. Modular Equations

    题目传送门 题意:a % x == b,求符合条件的x有几个 数学:等式转换为:a == nx + b,那么设k = nx = a - b,易得k的约数(>b)的都符合条件,比如a=25 b=1 ...

  2. 循环队列 分类: c/c++ 2014-10-10 23:28 605人阅读 评论(0) 收藏

    利用线性表实现队列,为了有效利用空间,将其设计为循环结构,防止假溢出:牺牲一个存储单元以区分队空.队满. 设front队头,rear队尾,N为顺序表大小 队空:rear==front 队满:(rear ...

  3. [转]C# 邮箱验证激活

    原文链接 /// <summary> /// 发送邮件 发送激活码 /// </summary> /// <param name="address"& ...

  4. poj2573Bridge(过桥问题)

    链接 A,B为最快和次快 有两种方式可以使c,d过桥 一是a与c一起走,a回来接d再与d一起走,一直到对岸人为0为止 而是 a与b一起走 a回来送灯 c与d一起走 b回来送灯 重复此过程. 只剩2人时 ...

  5. AJPFX总结Socket的低层次Java网络编程

    Socket的低层次Java网络编程 1 Socket通讯 网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket.Socket通常用来实现客户方和服务方的连接. ...

  6. [转]Sublime Text操作

    原文地址:http://www.madongdong.me/sublime-text3%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/ 作者:马东东 前言(Prologue) ...

  7. Java快速读取大文件

    Java快速读取大文件 最近公司服务器监控系统需要做一个东西来分析Java应用程序的日志. 第一步探索: 首先我想到的是使用RandomAccessFile,因为他可以很方便的去获取和设置文件指针,下 ...

  8. Katalon Studio(二) 进阶战の Jenkins集成 analytics.katalon 集成

    本教程只针对Katalon Studio 与CI工具之一Jenkins的集成与脚本集的测试报告可视化简单操作. 1.新建一个job 2.新建一个自由风格的job 3.构建触发器 4.构建Windows ...

  9. Node.js——Stream

    介绍 文件流:我们一般对大一点的文件实现stream的方式进行操作 http:显然http.createServer创建过程中的IncomingMessage实现了可读流的接口,ServerRespo ...

  10. 这是一条立了Flag的不归路

    时间2017年7月11日 14:48:40 首次激活博客园的博客来进行学习记录,立下了不算远大的小目标,下一步就是要一步一步的往前走. Java是目前最普遍的使用语言之一,作为一名测试,本应该去学习更 ...