当前位置:网站首页>POJ 3140 Contestants Division「建议收藏」
POJ 3140 Contestants Division「建议收藏」
2022-07-07 20:58:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
题目大意:
给出一棵树。求去掉一条边之后两棵子树节点权值和作差的最小值。
解题思路:
这不知道怎么用树形DP做,仅仅是个DFS就过了。还手残了一次。
思路详细看代码。
以下是代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
using namespace std;
int min(int a,int b)
{
if(a>b)a=b;
return a;
}
int max(int a,int b)
{
if(a<b)a=b;
return a;
}
struct node
{
int to,next;
} edge[100005*2];
int n,m,cnt,head[100005];
long long weight[100005],min1,sum;
const long long inf = 100000000000000ll;
bool vis[100005];
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].next=head[v];
head[v]=cnt++;
}
bool chack(int src)
{
int t=head[src];
while(t!=-1)
{
if(!vis[edge[t].to])return true;
t=edge[t].next;
}
return false;
}
long long llaabs(long long num)
{
if(num<0)num=-num;
return num;
}
long long dfs(int src)
{
long long ans=weight[src],temp;
vis[src]=true;
if(chack(src))
{
int t=head[src];
while(t!=-1)
{
if(!vis[edge[t].to])
{
temp=dfs(edge[t].to);
min1=min(min1,llaabs(sum-temp-temp));
ans+=temp;
}
t=edge[t].next;
}
}
return ans;
}
int main()
{
int case1=1;
while(scanf("%d%d",&n,&m),n||m)
{
cnt=0;
int u,v;
min1=inf;
sum=0;
memset(vis,false,sizeof(vis));
memset(head,-1,sizeof(head));
for(int i=1; i<=n; i++)
{
scanf("%lld",&weight[i]);
sum+=weight[i];
}
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
dfs(1);
printf("Case %d: %lld\n",case1++,min1);
}
return 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116301.html原文链接:https://javaforall.cn
边栏推荐
- 恶魔奶爸 A3阶段 近常速语流初接触
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- [paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
- 恶魔奶爸 指南帖——简易版
- Mysql子查询关键字的使用方式(exists)
- 字符串中数据排序
- 阿里云有奖体验:如何通过ECS挂载NAS文件系统
- Jetty:配置连接器[通俗易懂]
- 最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
- AADL Inspector 故障树安全分析模块
猜你喜欢
使用枚举实现英文转盲文
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
程序猿赚的那点钱算个P啊!
Lex & yacc of Pisa proxy SQL parsing
AADL Inspector 故障树安全分析模块
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Klocwork 代码静态分析工具
Measure the height of the building
随机推荐
死锁的产生条件和预防处理[通俗易懂]
Écrivez une liste de sauts
margin 等高布局
如何满足医疗设备对安全性和保密性的双重需求?
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
[matrix multiplication] [noi 2012] [cogs963] random number generator
数值法求解最优控制问题(〇)——定义
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
【C语言】指针进阶---指针你真的学懂了吗?
写一下跳表
Alibaba cloud award winning experience: how to mount NAS file system through ECS
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
Update iteration summary of target detection based on deep learning (continuous update ing)
Implement secondary index with Gaussian redis
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
What are the official stock trading apps in the country? Is it safe to use
如何满足医疗设备对安全性和保密性的双重需求?
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]