博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 三部曲 Contestants Division
阅读量:7032 次
发布时间:2019-06-28

本文共 3321 字,大约阅读时间需要 11 分钟。

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
2 #include
3 #include
4 #include
5 #include
6 #include
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

你可能感兴趣的文章
详解前端异步编程的六种方案
查看>>
红帽论坛北京站召开 设立亚太开放创新实验室
查看>>
苏宁11.11:如何基于异步化打造会员任务平台?
查看>>
区块链和数据科学:如果同时应用这两种技术,将会实现什么?
查看>>
Oracle即将发布的全新Java垃圾收集器 ZGC
查看>>
深入浅出Tensorflow(三):训练神经网络模型的常用方法
查看>>
Blazor将.NET带回到浏览器
查看>>
利用人工智能提升团队包容性
查看>>
详解分布式系统本质:“分治”和“冗余”
查看>>
gRPC-Web发布,REST又要被干掉了?
查看>>
全站爬虫项目一阶段总结
查看>>
在项目中引入领域驱动设计的经验
查看>>
用关系型NoSQL回到未来
查看>>
Jeff Bean谈Flink与流式处理的5大新发现
查看>>
技术寡头争霸传之:控制开源工具,就控制了整个生态
查看>>
微软把UWP定位成业务线应用程序开发平台
查看>>
2018腾讯云+未来峰会互联网专场:腾讯云智能物联解决方案亮相
查看>>
Python数据可视化的10种技能
查看>>
关于有效的性能调优的一些建议
查看>>
微软发起Java on Azure调查,呼吁Java社区积极参与
查看>>