当前位置:网站首页>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
边栏推荐
猜你喜欢
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System

How to meet the dual needs of security and confidentiality of medical devices?
MySQL storage expression error

95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
Lex & yacc of Pisa proxy SQL parsing

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

Cantata9.0 | 全 新 功 能

Apifox interface integrated management new artifact

目标:不排斥 yaml 语法。争取快速上手

CodeSonar网络研讨会
随机推荐
想杀死某个端口进程,但在服务列表中却找不到,可以之间通过命令行找到这个进程并杀死该进程,减少重启电脑和找到问题根源。
Is it safe to open a stock account at present? Can I open an account online directly.
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
Phoenix JDBC
解决使用uni-app MediaError MediaError ErrorCode -5
margin 等高布局
Guava multithreading, futurecallback thread calls are uneven
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
程序猿赚的那点钱算个P啊!
【C语言】指针进阶---指针你真的学懂了吗?
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
字符串中数据排序
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Spark judges that DF is empty
How to choose fund products? What fund is suitable to buy in July 2022?
Mysql子查询关键字的使用方式(exists)
Introduction to referer and referer policy
刚开户的能买什么股票呢?炒股账户安全吗