当前位置:网站首页>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
边栏推荐
- OneSpin | 解决IC设计中的硬件木马和安全信任问题
- 解决使用uni-app MediaError MediaError ErrorCode -5
- Codeforces Round #296 (Div. 2) A. Playing with Paper[通俗易懂]
- 如何满足医疗设备对安全性和保密性的双重需求?
- 开户必须往账户里面赚钱吗,资金安全吗?
- 死锁的产生条件和预防处理[通俗易懂]
- I Basic concepts
- What are the official stock trading apps in the country? Is it safe to use
- Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
- [concept of network principle]
猜你喜欢
MySQL约束之默认约束default与零填充约束zerofill
Klocwork 代码静态分析工具
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
Measure the height of the building
Nebula importer data import practice
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
Tensorflow2. How to run under x 1 Code of X
Cantata9.0 | 全 新 功 能
【C语言】指针进阶---指针你真的学懂了吗?
随机推荐
权限不足
Nebula Importer 数据导入实践
恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
软件缺陷静态分析 CodeSonar 5.2 新版发布
Micro service remote debug, nocalhost + rainbow micro service development second bullet
You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
Cantata9.0 | 全 新 功 能
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
CodeSonar如何帮助无人机查找软件缺陷?
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
阿里云有奖体验:如何通过ECS挂载NAS文件系统
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
私募基金在中国合法吗?安全吗?
npm uninstall和rm直接删除的区别