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?
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.
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.
7 6 1 1 1 1 1 1 1 1 2 2 7 3 7 4 6 6 2 5 7 0 0
Case 1: 1
题意:n个点,每个结点有k个学生;求删除一条边之后分成的两棵子树的学生之和的差最小,求差值。 和S,Q,R的去点分树不同,这个题是去边的。这个题做起来说是树状dp倒不如说是深搜,因为没有递推公式,这里的解题思想是:不管要删哪一条边,节点是不会被删掉的,求两部分子树的差值,首先可以肯定这两个字数的和是sum(所有节点学生的和),所以求完一个为x后,另一个就是(sum-x),而差值就是(x-(sum-x));那这个x怎么求呢?想清楚一个事,每一个子树,都必定有一个根节点的,我们用dp【now】代表以now为跟的子树的学生和,直接深搜求就好了,最后找到最小值输出就ok。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int MAX=1000010;
struct Edge
{
int next;
int to;
}edge[MAX];
int head[MAX];
int vis[MAX];
LL stu[MAX];
LL dp[MAX];
int N, M, K;
void addEdge(int u, int v)
{
edge[++K].next=head[u];
edge[K].to=v;
head[u]=K;
return ;
}
void dfs(int now)
{
//cout<<"................."<<endl;
vis[now]=1;
dp[now]=stu[now];
//cout<<"now "<<now<<" headnow "<<head[now]<<endl;
for(int i=head[now]; i!=0; i=edge[i].next)
{
//cout<<"now "<<now<<" i "<<i<<endl;
int next=edge[i].to;
if(vis[next])
continue;
dfs(next);
dp[now]+=dp[next];
}
}
int main()
{
ios::sync_with_stdio(0);
int u, v;
LL sum;
int cnt=1;
while(cin>>N>>M && (N+M))
{
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
memset(head, 0, sizeof(head));
memset(edge, 0, sizeof(edge));
sum=0;
for(int i=1; i<=N; i++)
{
cin>>stu[i];
sum+=stu[i];
}
K=0;
for(int i=1; i<=M; i++)
{
cin>>u>>v;
addEdge(u, v);
addEdge(v, u);
}
dfs(1);
LL temp=0;
LL ans=1e15;
for(int i=1; i<=N; i++)
{
temp=dp[i]-(sum-dp[i]);
if(temp<0)
temp=-temp;
ans=min(ans, temp);
//cout<<temp<<" dp "<<dp[i]<<endl;
}
cout<<"Case "<<cnt++<<": "<<ans<<endl;
}
return 0;
}