ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

hdu 三部曲 Contestants Division

2019-06-26 12:49:53  阅读:187  来源: 互联网

标签:Contestants hdu Division int LL head two include university


Problem 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 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 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.

  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 ********************************************************************************************* 一棵树,去掉一条边,形成两棵子树,求两棵子树的节点数量差最小。 *********************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<queue>
 5 #include<cmath>
 6 #include<cstdio>
 7 #define LL  long long
 8 using namespace std;
 9 bool vis[100011];
10 int next[1000111];
11 int head[100011],li[1000011];
12 int I,i,j;
13 LL sum,m1;
14 LL p[100011],d[100011];
15 int n,m,k;
16 LL  ff_abs(LL x)//要重新定义

17 {
18     if(x>=0)
19     return x;
20     return -x;
21 }
22 LL min1(LL x,LL y)
23 {
24     if(x>y)
25      return y;
26     return x;
27 }
28 void add(int a,int b)//链式前向星(新形式)
29 {
30     li[I]=b;next[I]=head[a];
31       head[a]=I++;
32     li[I]=a;next[I]=head[b];
33       head[b]=I++;
34 }
35 void dfs(int x)
36 {
37     int i;
38     vis[x]=true;
39     d[x]=p[x];
40     for(i=head[x];i!=-1;i=next[i])
41     {
42         int cur=li[i];
43         if(!vis[cur])
44          {
45              dfs(cur);//更加深刻理解了dfs();
46              d[x]+=d[cur];
47          }
48     }
49     m1=min1(m1,ff_abs(sum-2*d[x]));
50 }
51 int main()
52 {
53     int st=0,t,s;
54     while(scanf("%d%d",&n,&m)!=EOF)
55     {
56        if(m==0&&n==0)
57         break;
58        sum=0;
59     for(i=1;i<=n;i++)
60     {
61         scanf("%lld",&p[i]);
62         sum+=p[i];
63     }
64      I=0;
65      m1=sum;
66     memset(head,-1,sizeof(head));
67     memset(li,0,sizeof(li));
68     memset(next,-1,sizeof(next));
69     memset(vis,false,sizeof(vis));
70     memset(d,0,sizeof(d));
71      for(i=1;i<=m;i++)
72      {
73          scanf("%d%d",&s,&t);
74          add(s,t);
75      }
76     dfs(1);
77     printf("Case %d: %lld\n",(++st),m1);
78     }
79     return 0;
80 
81 }
View Code

坚持!!!!!

转载于:https://www.cnblogs.com/sdau--codeants/p/3317931.html

标签:Contestants,hdu,Division,int,LL,head,two,include,university
来源: https://blog.csdn.net/weixin_34061482/article/details/93727627

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有