T - Contestants Division

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

      题意:n个点,每个结点有k个学生;求删除一条边之后分成的两棵子树的学生之和的差最小,求差值。 和S,Q,R的去点分树不同,这个题是去边的。这个题做起来说是树状dp倒不如说是深搜,因为没有递推公式,这里的解题思想是:不管要删哪一条边,节点是不会被删掉的,求两部分子树的差值,首先可以肯定这两个字数的和是sum(所有节点学生的和),所以求完一个为x后,另一个就是(sum-x),而差值就是(x-(sum-x));那这个x怎么求呢?想清楚一个事,每一个子树,都必定有一个根节点的,我们用dp【now】代表以now为跟的子树的学生和,直接深搜求就好了,最后找到最小值输出就ok。

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int MAX=1000010;
struct Edge
{
    int next;
    int to;
}edge[MAX];
int head[MAX];
int vis[MAX];
LL stu[MAX];
LL dp[MAX];
int N, M, K;
void addEdge(int u, int v)
{
    edge[++K].next=head[u];
    edge[K].to=v;
    head[u]=K;
    return ;
}
void dfs(int now)
{
    //cout<<"................."<<endl;
    vis[now]=1;
    dp[now]=stu[now];
    //cout<<"now   "<<now<<"   headnow   "<<head[now]<<endl;
    for(int i=head[now]; i!=0; i=edge[i].next)
    {
        //cout<<"now "<<now<<"  i "<<i<<endl;
        int next=edge[i].to;
        if(vis[next])
            continue;
        dfs(next);
        dp[now]+=dp[next];
    }
}
int main()
{
    ios::sync_with_stdio(0);
    int u, v;
    LL sum;
    int cnt=1;
    while(cin>>N>>M && (N+M))
    {
        memset(dp, 0, sizeof(dp));
        memset(vis, 0, sizeof(vis));
        memset(head, 0, sizeof(head));
        memset(edge, 0, sizeof(edge));
        sum=0;
        for(int i=1; i<=N; i++)
        {
            cin>>stu[i];
            sum+=stu[i];
        }
        K=0;
        for(int i=1; i<=M; i++)
        {
            cin>>u>>v;
            addEdge(u, v);
            addEdge(v, u);
        }
        dfs(1);
        LL temp=0;
        LL ans=1e15;
        for(int i=1; i<=N; i++)
        {
            temp=dp[i]-(sum-dp[i]);
            if(temp<0)
                temp=-temp;
            ans=min(ans, temp);
            //cout<<temp<<"  dp "<<dp[i]<<endl;
        }
        cout<<"Case "<<cnt++<<": "<<ans<<endl;
    }

    return 0;
}




内容概要:本文《2025年全球AI Coding市场洞察研究报告》由亿欧智库发布,深入分析了AI编程工具的市场现状和发展趋势。报告指出,AI编程工具在2024年进入爆发式增长阶段,成为软件开发领域的重要趋势。AI编程工具不仅简化了代码生成、调试到项目构建等环节,还推动编程方式从人工编码向“人机协同”模式转变。报告详细评估了主流AI编程工具的表现,探讨了其商业模式、市场潜力及未来发展方向。特别提到AI Agent技术的发展,使得AI编程工具从辅助型向自主型跃迁,提升了任务执行的智能化和全面性。报告还分析了AI编程工具在不同行业和用户群体中的应用,强调了其在提高开发效率、减少重复工作和错误修复方面的显著效果。最后,报告预测2025年AI编程工具将在精准化和垂直化上进一步深化,推动软件开发行业进入“人机共融”的新阶段。 适合人群:具备一定编程基础,尤其是对AI编程工具有兴趣的研发人员、企业开发团队及非技术人员。 使用场景及目标:①了解AI编程工具的市场现状和发展趋势;②评估主流AI编程工具的性能和应用场景;③探索AI编程工具在不同行业中的具体应用,如互联网、金融、游戏等;④掌握AI编程工具的商业模式和盈利空间,为企业决策提供参考。 其他说明:报告基于亿欧智库的专业研究和市场调研,提供了详尽的数据支持和前瞻性洞察。报告不仅适用于技术从业者,也适合企业管理者和政策制定者,帮助他们在技术和商业决策中更好地理解AI编程工具的价值和潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值