codeforces581F. Zublicanes and Mumocrates

Shopee 虾皮API接口:商品详情采集的实践 随着电商行业的蓬勃发展,数据采集和分析在商业决策中扮演着越来越重要的角色。Shopee作为东南亚领先的电商平台,拥有大量的商品数据。通过Shopee的API接口,我们可以方便地获取商品详情,为数据分析和市场研究提供有力支持。本文将介绍如何使用Shopee API接口采集商品详情,并分享一些实践技巧。提供了丰富的数据接口,包括商品详情、店铺信息、订单数据等。通过API接口,开发者可以轻松地获取Shopee平台上的数据,进行进一步的处理和分析。Shopee API接口:商品详情采集的实践与技巧。 阅读详情

传送门:http://codeforces.com/problemset/problem/581/F

F. Zublicanes and Mumocrates
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.

Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.

The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.

To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 5000) — the number of squares in the capital of Berland.

Next n - 1 lines contain the pairs of integers x, y (1 ≤ x, y ≤ n, x ≠ y) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.

Output

Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.

Sample test(s)
input
8
1 4
2 4
3 4
6 5
7 5
8 5
4 5
output
1
input
5
1 2
1 3
1 4
1 5
output
2

思路:设f[i][cnt][col]表示i的子树中,黑节点为cnt,i的颜色为col时,最小的端点颜色不同的边的数量。

转移就是 f[x][cnt][col]=min(f[son[x]][i][col_son]+f[x][cnt-i][col]+(col!=col_son));

看起来这是O(n^3)的,实际上这是O(n^2)的

对于一个点x,对x操作的复杂度就是


观察一下就会发现,每对叶子节点(x,y)只会在lca(x,y)处对复杂度有一次贡献

于是复杂度就是O(n^2)的了。

#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn=5010,maxm=10010;
using namespace std;
int n,m,pre[maxm],now[maxn],son[maxm],val[maxm],f[maxn][maxn][2],g[maxn][2],siz[maxn],tot,in[maxn];bool isl[maxn];
void add(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b,in[b]++;}//0 white 1 black

void dfs(int x,int fa){
	
	if (isl[x]){siz[x]=1,f[x][0][0]=f[x][1][1]=0;return;}
	f[x][0][0]=f[x][0][1]=0;
	for (int y=now[x];y;y=pre[y])if (son[y]!=fa){
		dfs(son[y],x);
		//printf("fuckpp%d %d\n",son[y],siz[x]);
		memset(g,63,sizeof(g));
		for (int i=siz[x];i>=0;i--)
			for (int j=siz[son[y]];j>=0;j--){
				//if (son[y]==8&&x==5) printf("%d %d\n",i,j);
				g[i+j][0]=min(g[i+j][0],f[x][i][0]+f[son[y]][j][0]);
				g[i+j][1]=min(g[i+j][1],f[x][i][1]+f[son[y]][j][1]);
				g[i+j][0]=min(g[i+j][0],f[x][i][0]+f[son[y]][j][1]+1);
				g[i+j][1]=min(g[i+j][1],f[x][i][1]+f[son[y]][j][0]+1);
			}
		memcpy(f[x],g,sizeof(f[x]));
		siz[x]+=siz[son[y]];
	}
}

int main(){
	scanf("%d",&n);
	for (int i=1,a,b;i<n;i++) scanf("%d%d",&a,&b),add(a,b),add(b,a);
	if (n==2) return puts("1"),0;
	for (int i=1;i<=n;i++) isl[i]=(in[i]==1);
	memset(f,63,sizeof(f));
	for (int i=1;i<=n;i++) if (in[i]!=1){
		dfs(i,0),printf("%d\n",min(f[i][siz[i]>>1][0],f[i][siz[i]>>1][1]));
		break;
	}
//	for (int i=1;i<=n;i++) printf("%d %d\n",i,siz[i]);
	return 0;
}


PCL 将对象模板与点云对齐【2025最新版】 PCL模版匹配算法的使用案例,博客长期更新,本文最近更新时间为:2025年1月18日。 阅读详情

相关推荐

电赛综合测评题练习(一)-(与2013年电赛综合测评要求类似)

电赛综合测评题练习 文章目录电赛综合测评题练习前言1. 波形发生器1.1 题目说明1.1.1 可用器件1.1.2 设计要求1.1.3 设计方案论证1.2 仿真电路设计1.2.1 脉冲波发生器电路1.2.1.1 方案介绍1.2.1.2 理论分析与计算1.2.1.3 电路系统设计1.2.1.4 仿真方案与仿真测试结果1.2.2 锯齿波电路1.2.2.1 理论分析与计算1.2.2.2 电路系统设计1.2.2.3 仿真方案与仿真测试结果1.2.3 正弦波I电路1.2.3.1 理论分析与计算1.2.3.2 电路系

daydayup!的博客 3万+

codeforces581f(树形dp)

F. Zublicanes and Mumocrates time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output It's election time...

weixin_30562507的博客 130

如何在OBS中添加实时时间戳:Lua脚本配置教程(附效果展示)

本文详细介绍了如何在OBS中通过Lua脚本配置实时时间戳功能,包括环境准备、脚本获取、安装配置及高级定制。适用于直播和视频录制场景,提供精准回溯和风格自定义,支持多平台适配和性能优化,是专业制作的必备技巧。

day7的博客 1040

【树DP】[CodeForces - 581F]Zublicanes and Mumocrates

Codeforces - 581F 分析:根据题意,这是一棵树,在看看数据范围和时间,显然是树DP 用f[u][j][c]表示以u为根,当前点党派为c是,选取j个叶子节点(dead end squares)给0党派的时候,连接双方的边的最小值。 f[u][j+k][c]=min(f[u][j+k][c],f[u][j][c]+f[s][k][c’]+(c!=c’)); ...

weixin_30794491的博客 175

基于航位推算与多传感器融合的高精度避障小车设计

航位推算(Dead Reckoning)是一种在无外部定位信号下通过里程计与惯性测量单元(IMU)持续积分估计位姿的基础导航方法,其核心在于运动学建模、误差累积抑制与多源数据融合。该技术具备低依赖、高实时、强确定性等工程优势,广泛应用于服务机器人、AGV、竞赛无人平台等受限环境下的自主导航系统。结合编码器高分辨率测距与九轴IMU姿态解算,可显著抑制纯里程计漂移,提升短时定位精度至厘米级。本文以全国大学生电子设计竞赛K题为背景,深入实现了一种面向确定性场地的离线建图+航位推算双模协同架构,重点解决嵌入式资源约

weixin_36328210的博客 81

World Action Model:经典论文

郑困时,图prorpt为机量人压务时,退过驱制解弱词表(constrsinsd decoding), 强附模型只从.256个者动作token中保H, B象总注活地。始VLA之一,RT-2使用了大量的五联网VQ据与少里的机械网的AcSonDat行CoFine-Tunng, 独组sl, 共i744个episode,Mous使用三个专累模,分是vidogonmedel,actonesget,understandng。包的核定之的的观到沟血黄动作,预之后现(vorlemodel),tseing,2。

u013250861的博客 406

树形dp-CodeForces 581F

A - Contest Page Time Limit:3000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 581F Description It's election time in Berland. The fav

猿的进化 537

Winform使用TreeView控件实现带复选框的三态树

刚接触c#,项目中用到Treeview实现三态,即选中、未选中、半选中状态,并且要求父子节点选择联动,效果类似下图网上有很多例子,于是研究了一下,结合网上大佬们的例子,所以在这里记录一下。首先新建一个Winform工程拖个TreeView控件,然后运行。。。嗯。。。加点数据吧。private void CreateTree() { TreeNode anime = new TreeNode...

laoyezha的博客 2万+

CodeForces 581F Zublicanes and Mumocrates 树形DP

给出一棵树,要求将树叶染成2种颜色并使这两种颜色之间不连通,求最少删边数。 树D,令f[i][j]表示i的子树分走j个叶子的最小删边数。搞下背包。 一定要从非叶节点开始dp。。 写了一些冗余代码了。。 #include #include #include using namespace std; const int N = 5001; vector adj[N]; int sz[N]

とある黄鱼のOIブログ 640

从零到一:手把手教你用C#开发AGV调度系统(附避坑指南)

本文详细介绍了如何使用C#开发高效的AGV调度系统,涵盖从环境搭建到核心算法实现的完整流程。重点解析了动态路径规划、多机协同等关键技术,并提供了实用的避坑指南和性能优化建议,帮助开发者快速构建工业级车辆控制调度系统。

blockchain9miner的博客 741

DPM|03Injection设置

摘要:本文介绍了CFD软件Fluent中粒子注入的设置方法。粒子注入是将离散颗粒引入连续相的过程,可用于模拟灰尘、燃料液滴等。设置流程包括:选择粒子类型(质量less、惰性等)、定义注入点属性(坐标、时间、粒径等)、处理多组分粒子、使用文本文件批量定义复杂注入。文中还详细说明了要考虑的湍流影响,包括随机追踪法模拟湍流扩散,以及粒子旋转建模等注意事项。最后介绍了多种注入类型和粒子类型,并强调在湍流系统中需设置足够的尝试次数以确保结果准确性。

qq_28258217的博客 2228

Codeforces 581F Zublicanes and Mumocrates 树形dp

Zublicanes and Mumocrates dp[ i ][ j ][ k ] 表示 以 i 为根的子树, 占领 i 的 是 j 并且第一个人占了 i 子树的 k 个叶子节点的最小值。 然后随便d 1 d 就好了。 #include<bits/stdc++.h> #define LL long long #define LD long double #defin...

weixin_30666753的博客 90

第01章 Hermes Agent 是什么

如果读者所在的开发团队已经引入了 Cursor、Claude Code 这类 AI IDE,可能会有一个困惑:写代码已经有 AI 帮忙了,还需要 Hermes Agent 做什么?这一章将给出答案。Hermes Agent 不是另一个写代码的 AI,而是一个常驻在团队里的"AI 协作成员",专门负责写代码之外的协作环节——讨论需求、审查代码、查日志、监控告警、生成报告。学完本章,读者将清楚 Hermes 的定位以及它在团队里的角色。

亢少军的博客 74

Codeforces 581F Zublicanes and Mumocrates - 树形动态规划

It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations onnmain squares of ...

adx33526的博客 203

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Codeforces 581F Zublicanes and Mumocrates(树型DP)

题目链接 Round 322 Problem F 题意 给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白)   要求一半叶子结点的颜色为白,一半叶子结点的颜色为黑,求边权和的最小值。   若一条边连接的两个点颜色不一样,则该条边边权为$1$,否则为$0$。 考虑树型$DP$。 $f[x][i][j]$表示当以$x$为根的子树中有$i...

weixin_30532973的博客 155

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Codeforces 581F Zublicanes and Mumocrates(树形DP)

题目大概说有一棵树要给结点染色0或1,要求所有度为1的结点一半是0一半是1,然后问怎么染色,使两端点颜色不一样的边最少。 dp[0/1][u][x]表示以u结点为根的子树中u结点是0/1色 且其子树有x个结点染成1色 的最少边数 转移就是树上背包了。有点就是各个子树必须选,这种形式的树上背包之前做过,我的做法是用了tmp数组来更新最后再写入根的状态数组中,另外我特判了第一个子树的...

weixin_30344131的博客 82

Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates(树形dp,好题)

题目链接 F. Zublicanes and Mumocrates time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output It's election time in

fouzhe的博客 466

Codeforces Round #322 Zublicanes and Mumocrates 树形dp

题目链接: http://codeforces.com/contest/581/problem/F 题意: 给你一棵有偶数个叶子结点的树,现在要求给树上的每个结点染黑色或者白色,叶子结点必须有一半为黑色一半为白色,其余的结点的颜色可以任意染,当这一边两边的叶子结点颜色不同时,这条边对答案的贡献 +1+1+1 , 问你答案最小是多少,即最少有多少对边满足边两端的颜色不同。 做法: 一棵树并且是问到了...

人生如戏 176

DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates

树形dp,记录一下根节点的状态即可... #include using namespace std; typedef long long LL; const int maxn = 5005; const int maxm = 10005; const int INF = 0x3f3f3f3f; struct Edge { int v; Edge *next; }E[maxm], *H[

yysys 715

CF 581F 树形dp

http://codeforces.com/problemset/problem/581/F F. Zublicanes and Mumocrates time limit per test 3 seconds memory limit per test 512 megabytes input standard input output s

ACM,再见! 1055

Codeforces #322 div2. F Zublicanes and Mumocrates 树形dp

CF#322最后一道,前四道很容易,这道是第二难的。

ned_chu的博客 550

codeforces#322 div2 F Zublicanes and Mumocrates 树形dp

http://codeforces.com/contest/581/problem/F #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef lon...

weixin_30729609的博客 90

CodeForces 581F Contest Page 【树形dp】

F. Zublicanes and Mumocrates time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output It's election time in Berland.

MissZhou要努力 843

Codeforces581F

参考代码及简单注释: #pragma comment(linker, "/STACK:1024000000,1024000000") #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef lon

丿Smilē 丶晨星灬 802

Codeforce 581F(树形dp)

链接:点击打开链接 题意:给出一个树,要求最少删除多少条边使叶子节点恰好平分 代码: #include #include #include #include #include #include using namespace std; const int INF=0x3f3f3f3f; int n,p; int dp[5005][5005],num[5005],

stay_accept的专栏 557
上一篇: codeforces580E. Kefa and Watch
下一篇: codeforces585D. Lizard Era: Beginning
thy_asdf
博客等级 码龄12年 16粉丝 186原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值