Contestants Division
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submissions: 6947 Accepted: 1961
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 p Assing 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 shoshould 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. www.2cto.com
N = 0, M = 0 indicates the end of input and shoshould 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
Shanghai 2006
MYCode:
# Include <iostream>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
Typedef long LL;
Using namespace std;
# Deprecision MAX 100010
// # Define inf 100000000
Struct node
{
Int v;
Int next;
} E [1, 1000010];
Bool vis [MAX];
LL sum [MAX];
Int head [MAX];
LL v [MAX];
Int num;
Int n, m;
LL tot;
LL ans;
Void init ()
{
Memset (head,-1, sizeof (head ));
Memset (vis, 0, sizeof (vis ));
Num = 0;
}
Void add (int s, int t)
{
E [num]. v = t;
E [num]. next = head [s];
Head [s] = num ++;
}
Void dfs (int cur)
{
Vis [cur] = 1;
Int I;
LL res;
LL s = 0;
For (I = head [cur]; I! =-1; I = E [I]. next)
{
Int v = E [I]. v;
If (vis [v])
Continue;
Dfs (v );
S + = sum [v];
Res = abs (tot-sum [v])-sum [v] * 1.0 );
// Cout <"res =" <res <endl;
If (res <ans)
Ans = res;
}
Sum [cur] = s + v [cur];
LL add = tot-sum [cur];
Res = abs (tot-add)-add * 1.0 );
If (res <ans)
{
Ans = res;
// Cout <"res =" <res <endl;
}
}
Int main ()
{
Int ct = 0;
While (scanf ("% d", & n, & m )! = EOF)
{
If (n = 0 & m = 0)
Break;
Init ();
Int I;
Tot = 0;
For (I = 1; I <= n; I ++)
{
// Scanf ("% lld", & v [I]);
Cin> v [I];
Tot + = v [I];
}
Int s, t;
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & s, & t );
Add (s, t );
Add (t, s );
}
Ans = tot;
Dfs (1 );
/* For (I = 1; I <= n; I ++)
Cout <sum [I] <"";
Cout <endl ;*/
// Printf ("% d \ n", ans );
Cout <"Case" <+ + ct <":" <ans <endl;
}
}
// 750 MS
After dividing the original tree, it will inevitably become two trees. One of them is a subtree with a certain vertex in the original tree as the root. Therefore, we can enumerate all vertices in the DFS process to find the minimum value.
There should be better practices for this question, and the idea of dynamic planning cannot be imagined.
The efficiency of MS is not a satisfactory answer.