zoukankan      html  css  js  c++  java
  • hdu 三部曲 Contestants Division

    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
    ",(++st),m1);
    78     }
    79     return 0;
    80 
    81 }
    View Code

    坚持!!!!!

  • 相关阅读:
    jstack使用教程
    频繁fullgc排查
    ubuntu添加ubuntu make
    Spring属性编辑器详解
    mysql 查看触发器,删除触发器
    mongodb启动不了:提示错误信息为 child process failed, exited with error number 100
    RedHat7 防火墙设置以及端口设置
    linux 设置静态IP方法
    linux 安装mongo
    mongo 介绍
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3317931.html
Copyright © 2011-2022 走看看