当前位置:网站首页>并查集模板
并查集模板
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】SQL语句
- Day37 LeetCode
- IDLE development wordCount program (5)
- 英国国家卫生服务遭受攻击,系统出现大面积故障
- 阿里巴巴(中国)网络技术有限公司、测试开发笔试二面试题(附答案)
- ATH10传感器读取温湿度
- placeholder 1
- 2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
- 数据库公共字段自动填充
- 34. Talk about why you want to split the database?What methods are there?
猜你喜欢
随机推荐
34. 谈谈为什么要拆分数据库?有哪些方法?
快速输入当前日期与时间
模糊查询除了like+ % 还能用什么方式
Quickly enter the current date and time
【Event Preview on August 9】Prometheus Summit
delta method 介绍
Rust learning: 6.4_ enumeration of composite types
每日一题,数组字符串的匹配问题
明明加了唯一索引,为什么还是产生重复数据?
SCS【2】单细胞转录组 之 cellranger
day16--The use of the packet capture tool Charles
【MySQL】使用MySQL Workbench软件新建表
阿里巴巴(中国)网络技术有限公司、测试开发笔试二面试题(附答案)
数据库公共字段自动填充
uni 小程序腾讯地图polygon背景透明度
Rust learning: 6.1_Slices of composite types
卷积神经网络卷积层公式,卷积神经网络运算公式
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
MySQL索引事务
NPU架构与算力分析