BNUOJ 9870 Contestants Division

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>, (1$ \le$N$ \le$100000, 1$ \le$M$ \le$1000000) <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,去掉一条边,求两棵树点权和的最小的差值
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define INF 0x3f3f3f3f3f3f3f3f

15 using namespace std;

16 const int maxn = 100010;

17 vector<int>g[maxn];

18 LL ans,cnt[maxn],sum;

19 int n,m,w[maxn];

20 bool vis[maxn];

21 void dfs(int u){

22     vis[u] = true;

23     cnt[u] = w[u];

24     LL temp;

25     for(int i = 0,sz = g[u].size(); i < sz; i++){

26         if(vis[g[u][i]]) continue;

27         dfs(g[u][i]);

28         cnt[u] += cnt[g[u][i]];

29         if(sum - cnt[g[u][i]] >= cnt[g[u][i]])

30             temp = sum - 2*cnt[g[u][i]];

31         else temp = 2*cnt[g[u][i]] - sum;

32         if(temp < ans) ans = temp;

33     }

34 }

35 int main() {

36     int i,u,v,k = 1;

37     while(~scanf("%d %d",&n,&m),n||m){

38         sum = 0;

39         for(i = 1; i <= n; i++){

40             scanf("%d",w+i);

41             sum += w[i];

42             g[i].clear();

43             vis[i] = false;

44             cnt[i] = 0;

45         }

46         ans = INF;

47         for(i = 0; i < m; i++){

48             scanf("%d %d",&u,&v);

49             g[u].push_back(v);

50             g[v].push_back(u);

51         }

52         dfs(1);

53         printf("Case %d: %I64d\n",k++,ans);

54     }

55     return 0;

56 }
View Code

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

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define INF 0x3f3f3f3f3f3f3f3f

15 using namespace std;

16 const int maxn = 100010;

17 struct arc{

18     int to,next;

19 };

20 LL ans,cnt[maxn],sum;

21 int n,m,w[maxn],head[maxn],tot;

22 bool vis[maxn];

23 arc g[maxn*10];

24 void add(int u,int v){

25     g[tot].to = v;

26     g[tot].next = head[u];

27     head[u] = tot++;

28 }

29 void dfs(int u){

30     vis[u] = true;

31     cnt[u] = w[u];

32     LL temp;

33     for(int i = head[u]; i != -1; i = g[i].next){

34         if(vis[g[i].to]) continue;

35         dfs(g[i].to);

36         cnt[u] += cnt[g[i].to];

37         if(sum - cnt[g[i].to] >= cnt[g[i].to])

38             temp = sum - 2*cnt[g[i].to];

39         else temp = 2*cnt[g[i].to] - sum;

40         if(temp < ans) ans = temp;

41     }

42 }

43 int main() {

44     int i,u,v,k = 1;

45     while(~scanf("%d %d",&n,&m),n||m){

46         sum = 0;

47         tot = 0;

48         for(i = 1; i <= n; i++){

49             scanf("%d",w+i);

50             sum += w[i];

51             head[i] = -1;

52             vis[i] = false;

53             cnt[i] = 0;

54         }

55         ans = INF;

56         for(i = 0; i < m; i++){

57             scanf("%d %d",&u,&v);

58             add(u,v);

59             add(v,u);

60         }

61         dfs(1);

62         printf("Case %d: %I64d\n",k++,ans);

63     }

64     return 0;

65 }
View Code

 

你可能感兴趣的:(visio)