当前位置:网站首页>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
边栏推荐
- Postgresql数据库character varying和character的区别说明
- Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
- 万字总结数据存储,三大知识点
- How to meet the dual needs of security and confidentiality of medical devices?
- 使用高斯Redis实现二级索引
- Introduction to referer and referer policy
- How to choose fund products? What fund is suitable to buy in July 2022?
- How to meet the dual needs of security and confidentiality of medical devices?
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- CodeSonar网络研讨会
猜你喜欢

Implement secondary index with Gaussian redis

恶魔奶爸 B3 少量泛读,完成两万词汇量+

Dachang classic pointer written test questions

H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法

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

智能软件分析平台Embold

上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization

Network principle (1) - overview of basic principles
随机推荐
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
AADL inspector fault tree safety analysis module
HDU4876ZCC loves cards(多校题)
OneSpin | 解决IC设计中的硬件木马和安全信任问题
智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
Spark 判断DF为空
Is private equity legal in China? Is it safe?
Nebula Importer 数据导入实践
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Mysql子查询关键字的使用方式(exists)
Écrivez une liste de sauts
Codesonar enhances software reliability through innovative static analysis
What are the official stock trading apps in the country? Is it safe to use
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
MySQL约束之默认约束default与零填充约束zerofill
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Object-C programming tips timer "suggestions collection"
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围