当前位置:网站首页>(l2-026) small generation (with weight and search set)
(l2-026) small generation (with weight and search set)
2022-04-22 20:30:00 【AC__ dream】
Topic link :PTA | Program design experiment auxiliary teaching platform

The title requires the number of the member with the lowest seniority , Obviously, the maintenance between generations can be realized by weighted and search set , Distance indicates seniority , Then the rest is a basic weighted and search set template , It should be noted that after we have processed all the merging operations, we need to perform another merging on all points find operation , Ensure that each point is d The arrays are completely updated , Because of our find Operation with update distance in operation , Every time find(x) The operation will only update x And all of its ancestors d Array , Arrays outside this range will not be updated , Because of our merge The operation does not find the real d value , Just put his d The array is set to its parent node +1, But his father's node d Array values are not necessarily correct , Maybe it's just a parent node that depends on his father node , So after we deal with all the relationships, we need to deal with 1~n The number in find operation , Only in this way can we ensure the final d The array value is correct , Here is the code :
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
const int N=100003;
int fu[N],d[N],ans[N],tt;
int find(int x)
{
int t=fu[x];
if(x==fu[x]) return x;
fu[x]=find(fu[x]);
d[x]+=d[t];
return fu[x];
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy) return ;
fu[fx]=fy;
d[x]=1+d[y];
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
fu[i]=i;
int t;
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
if(t==-1) continue;
merge(i,t);
}
for(int i=1;i<=n;i++) find(i);// Update
int mx=0;
for(int i=1;i<=n;i++)
mx=max(mx,d[i]);
printf("%d\n",mx+1);
for(int i=1;i<=n;i++)
if(d[i]==mx) ans[++tt]=i;
for(int i=1;i<=tt;i++)
{
printf("%d",ans[i]);
if(i!=tt) printf(" ");
}
return 0;
}
版权声明
本文为[AC__ dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222012306674.html
边栏推荐
- UML (Unified Modeling Language) knowledge learning
- MarkDown 学习
- 第二章 数组
- Some considerations for pointers and objects
- Panyu maritime department has solidly promoted the 100 day action of safety education and training for water practitioners
- Leetcode222. Number of nodes of complete binary tree
- Conditions for judging whether plastic deformation occurs: Von Mises yield criterion
- redis如何使用命令清空所有key
- 2022 civil construction worker's question bank precision small question bank construction hall constructor
- Use of swift protocol
猜你喜欢

Chapter 2 array

A thorough explanation of the future form, development status and Prospect of Business Intelligence BI | recommended collection

时间戳转换
做了5年Android,靠着这份190页的面试资料,成功入职腾讯

年薪170W阿里P8相亲要求女方月薪1万,网友:有点高

Write a gateway service, understand more thoroughly!

显示实现接口和隐式实现接口的区别

TikTok+Shopify独立站测评,卖家新风口.

网站:fakeimg.pl(文字 --> 图片)

运维(33) CentOS7.6通过Kubeadm部署Kubernetes集群
随机推荐
Perfect forwarding implementation mechanism
SCI/SSCI期刊列表已更新,这几本期刊被剔除~
List的使用
2020 team design ladder competition (part)
Write a gateway service, understand more thoroughly!
微服务高性能高可用架构设计
重保战场的“排头兵”,“互联网宙斯盾”如何为城市高效布防?
Adobe系列错误代码解决方案汇总
IAP之boot实现
Countdown case
Character array and string: delete all spaces in the string. (10 points) write a function to delete all spaces in the string.
I neglected to prepare for this interview, which made me beat the day before yesterday
运维(33) CentOS7.6通过Kubeadm部署Kubernetes集群
- 4. 比较字符串 (10 分)C语言标准函数库中包括 strcmp 函数,用于字符串的比较。作为练习,我们自己编写一个功能与之相同的函数。
Acrobat Pro DC 教程,如何使用密码保护 PDF 文件?
做了5年Android,靠着这份190页的面试资料,成功入职腾讯
Huawei machine test questions summary
Detailed explanation of sorting methods (8 kinds) - bucket sorting
东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
CDH6.3.2 启用Kerberos 认证