BNUOJ 9870 Contestants Division
Contestants Division
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>0, M = <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
#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的更多相关文章
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- POJ 3104 Contestants Division
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10597 Accepted: ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- POJ-3140 Contestants Division (树)
题目大意:一棵树,带点权.将这棵树分成两部分,找出使得两部分的点权和的差最小. 题目分析:直接dfs即可.找出每棵子树u的点权和size(u),如果以u和它的父节点之间的边为界,那么两边的点权和分别为 ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
随机推荐
- [POI2008]激光发射器SZK
Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...
- 计算机视觉-SIFT特征匹配进行目标转换
Lowe将SIFT算法分解为如下四步: 1. 尺度空间极值检测:搜索所有尺度上的图像位置.通过高斯微分函数来识别潜在的对于尺度和旋转不变的兴趣点. 关键点定位:在每个候选的位置上,通过一个拟合精细的模 ...
- QT5每日一学(二)编写QT多窗口程序
一.添加主窗口 1.首先打开Qt Creator,新建Qt Widgets Application,项目名称设置为windows,在类信息界面保持基类为QMainWindow.类名为MainWindo ...
- 为HttpClient和HttpURLConnection添加中国移动代理
转自: http://www.2cto.com/kf/201111/112100.html 在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络 当我们 ...
- Python 相关疑问
1. 如果我的脚本error handling 做的好,在ctrl+c退出的时候是不是不应该有任何traceback log? 2. repr() str() eval() 之间的区别? 3. 参数传 ...
- windows deintall 12c client
1.unintall: close all oracle app C:\app\client\CICadmin\product\12.1.0\client_1\deinstall deinstall ...
- WinForm 对话框,流
private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...
- 【工具】sublime使用技巧
Ctrl+N 新建一个编辑区,Ctrl+Shift+C 或!加 Ctrl+E新建一个骨架完好的文件. Ctrl+Shift+P开启命令模式,sshtml 切换html语法. esc退出,Ctrl+`打 ...
- 短视频SDK超级简单易用
超级简单易用的短视频SDK来自RDSDK.COM.锐动天地为开发者提供短视频编辑.视频直播.特效.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨 ...
- jmeter 连接 sqlite 进行压力测试