当前位置:网站首页>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
边栏推荐
- npm uninstall和rm直接删除的区别
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
- Small guide for rapid formation of manipulator (12): inverse kinematics analysis
- 如何满足医疗设备对安全性和保密性的双重需求?
- Klocwork 代码静态分析工具
- Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
- 现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
- I have to use my ID card to open an account. Is the bank card safe? I don't understand it
- Phoenix JDBC
猜你喜欢

Nebula Importer 数据导入实践
![嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]

H3C s7000/s7500e/10500 series post stack BFD detection configuration method

Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city

Implement secondary index with Gaussian redis

Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance

Codesonar Webinar

Cantata9.0 | 全 新 功 能

Static analysis of software defects codesonar 5.2 release

【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
随机推荐
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
如何满足医疗设备对安全性和保密性的双重需求?
Do you have to make money in the account to open an account? Is the fund safe?
OneSpin | 解决IC设计中的硬件木马和安全信任问题
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
恶魔奶爸 C
margin 等高布局
机械臂速成小指南(十一):坐标系的标准命名
Mysql子查询关键字的使用方式(exists)
权限不足
Referrer和Referrer-Policy简介
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
Codeforces 474 F. Ant colony
目前股票开户安全吗?可以直接网上开户吗。
使用高斯Redis实现二级索引
Implement secondary index with Gaussian redis
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展