当前位置:网站首页>并查集模板
并查集模板
2022-08-10 07:56:00 【ThXe】
struct DSU {
int dis[N], si[N], f[N];//维护当前节点到根节点的距离,集合大小、
int n, m;
void init()
{
cin >> n;
for (int i = 1; i <= n; i++)f[i] = i,si[i]=1;
}
int find(int x)
{
if (x != f[x])
{
int last = f[x];
f[x] = find(f[x]);
dis[x] += dis[last];
}
return f[x];
}
void slove()//最小环、适用于边权为1 且每个点的出度为1
{
int ans = 0x3f3f3f3f;
for (int i = 1; i <= n; i++)
{
int t; cin >> t;
if (find(i) == find(t))
{
ans = min(ans, dis[i] + dis[t] + 1);
}
else
{
dis[i] = dis[t] + 1;
f[find(i)] = find(t);
si[find(t)] += si[find(i)];
}
}
cout << ans << endl;
}
}dsu;
边栏推荐
猜你喜欢
随机推荐
【MySQL】使用MySQL Workbench软件新建表
【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
VMware ESX Server常用命令行
模糊查询除了like+ % 还能用什么方式
Power function Exponential function Logarithmic function
WooCommerce 安装和 rest api 使用
调试ZYNQ的u-boot 2017.3 不能正常启动,记录调试过程
如何治理资源浪费?百度云原生成本优化最佳实践
Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
DGIOT supports industrial equipment rental and remote control
day16--抓包工具Charles的使用
预测股票涨跌看什么指标,如何预测明天股票走势
Add spark related dependencies and packaging plugins (sixth bullet)
2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
详解构建mock服务最方便的神器——Moco
IDLE development wordCount program (5)
34. Talk about why you want to split the database?What methods are there?
[机缘参悟-65]:《兵者,诡道也》-7-三十六计解读-败战计
神经网络样本太少怎么办,神经网络训练样本太少