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. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  2. js 宿主对象的属性和方法总结

    (1)属性:       //height,width;           a=document.documentElement.clientHeight;           //文档可视高度,由 ...

  3. LN : leetcode 646 Maximum Length of Pair Chain

    lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...

  4. Java用SAX解析XML

    要解析的XML文件:myClass.xml <?xml version="1.0" encoding="utf-8"?> <class> ...

  5. P1118 [USACO06FEB]数字三角形Backward Digit Su…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  6. 使用Gson解析Json数组遇到的泛型类型擦除问题解决方法

    谷歌Gson转换Json串有如下方法: public Object fromJson(String json, Type typeOfT);1可以使用它进行数组解析.如下,使用此方法解析Json串为类 ...

  7. git 控制操作

    克隆文件 git clone https://gitee.com/xxxxx/xxxxx.git 克隆分支文件 git clone -b 分支名 https://gitee.com/xxxxx/xxx ...

  8. 火狐加载用户配置文件 "C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk

    "C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk $("#clic ...

  9. lua之链表的实现

    -- lua链表的实现 node = {} list = node --初始化,构建一个空表 function init() list.data = --我将头结点的数据域存放链表的长度,以免浪费空间 ...

  10. 如何优化LIMIT

    首先我们先创建个数据表做测试 表名 test (id(int) , name(var char) , content(text) , pid(int) ) 往里面倒几百万条数据进去做测试. 我们都知道 ...