• POJ 3140 Contestants Division


    Contestants Division

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3140
    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 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 st, 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

    Source

     
    解题:树形dp
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <vector>
     5 using namespace std;
     6 typedef long long LL;
     7 const int maxn = 100010;
     8 vector<int>g[maxn];
     9 int w[maxn],n,m;
    10 LL son[maxn],sum,ret;
    11 inline LL myabs(LL x) {
    12     return x < 0?-x:x;
    13 }
    14 void dfs(int u,int fa) {
    15     son[u] = w[u];
    16     for(int i = g[u].size()-1; i >= 0; --i) {
    17         if(g[u][i] == fa) continue;
    18         dfs(g[u][i],u);
    19         ret = min(ret,myabs(son[g[u][i]] - (sum - son[g[u][i]])));
    20         son[u] += son[g[u][i]];
    21     }
    22 }
    23 int main() {
    24     int u,v,cs = 1;
    25     while(scanf("%d%d",&n,&m),n||m) {
    26         for(int i = 0; i <= n; ++i) g[i].clear();
    27         sum = 0;
    28         for(int i = 1; i <= n; ++i) {
    29             scanf("%d",w+i);
    30             sum += w[i];
    31         }
    32         for(int i = 0; i < m; ++i) {
    33             scanf("%d%d",&u,&v);
    34             g[u].push_back(v);
    35             g[v].push_back(u);
    36         }
    37         memset(&ret,0x3f,sizeof ret);
    38         dfs(1,-1);
    39         printf("Case %d: %lld
    ",cs++,ret);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    希尔排序
    基数排序
    spring8——AOP之Bean的自动代理生成器
    spring7——AOP之通知和顾问
    spring6——AOP的编程术语
    spring5——Aop的实现原理(动态代理)
    spring4——IOC之基于注解的依赖注入(DI )
    spring3——IOC之基于XML的依赖注入(DI )
    spring2——IOC之Bean的装配
    spring1——IOC之原理
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4798000.html
Copyright © 2020-2023  润新知