当前位置:网站首页>POJ 3140 contents division "suggestions collection"
POJ 3140 contents division "suggestions collection"
2022-07-07 20:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
The main idea of the topic :
Give me a tree . Find the minimum value of the sum of the node weights of the two subtrees after removing an edge .
Their thinking :
I don't know how to use tree DP do , It's just a DFS It's over . I hit back once .
Think about the code in detail .
Here's the code :
#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;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116301.html Link to the original text :https://javaforall.cn
边栏推荐
- guava多线程,futurecallback线程调用不平均
- [concept of network principle]
- Is private equity legal in China? Is it safe?
- 使用枚举实现英文转盲文
- MySQL storage expression error
- 【网络原理的概念】
- [paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
- 嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
- 恶魔奶爸 指南帖——简易版
- Referrer和Referrer-Policy简介
猜你喜欢
Codesonar Webinar
如何满足医疗设备对安全性和保密性的双重需求?
Klocwork code static analysis tool
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
SQL注入报错注入函数图文详解
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
Static analysis of software defects codesonar 5.2 release
AADL inspector fault tree safety analysis module
机械臂速成小指南(十一):坐标系的标准命名
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
随机推荐
Phoenix JDBC
程序猿赚的那点钱算个P啊!
CodeSonar网络研讨会
恶魔奶爸 B2 突破语法,完成正统口语练习
Flask1.1.4 Werkzeug1.0.1 源码分析:路由
恶魔奶爸 指南帖——简易版
恶魔奶爸 A0 英文零基础的自我提升路
神兵利器——敏感文件发现工具
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
死锁的产生条件和预防处理[通俗易懂]
Écrivez une liste de sauts
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Klocwork 代码静态分析工具
Data sorting in string
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
【C语言】指针进阶---指针你真的学懂了吗?
Lex & yacc of Pisa proxy SQL parsing
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system