当前位置:网站首页>1.17 learning summary
1.17 learning summary
2022-06-26 04:34:00 【After all, I still walk alone】
Today, I wrote a lot of questions about searching the collection . It is also useful for the union search set And some uses have been recognized . What I have used today are two kinds . One is Using the same ancestor of the element To solve some problems . There's another one , Use the number of ancestors to solve some problems . The first one is basically the template problem of joint search set .
For example, this question .

This question is a standard template . Is a merge set Then you can . Judging whether it is the same ancestor can solve this problem . The code is as follows ,
#include<stdio.h>
int i,j,k,n,m,s,ans,a[10010],b,c,d;
int find(int k){
if(a[k]==k)return k;
a[k]=find(a[k]);
return a[k];
}
int main()
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
a[i]=i;
for(i=1;i<=m;i++){
scanf("%d %d %d",&b,&c,&d);
if(b==1)
a[find(c)]=find(d);
if(b==2){
if(find(c)==find(d))
printf("Y\n");
else
printf("N\n");
}}
return 0;
}Then I said that the other way I used to solve problems was based on the number of ancestors .
The title is as follows .

What is required here is at least the disc to be recorded . In essence, it means that I have experienced the following . After union operation , There are also several ancestors . in other words , Our ancestors are our own Elements .
The code is as follows
#include<stdio.h>
int a[300];
int vis[300][300];
int main()
{
int n,t,ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
t=0;
while(t!=-1)
{
scanf("%d",&t);
if(t!=0){
vis[i][t]=1;}
if(t==0){
break;
}
}
}
for(int i=1;i<=n;++i)
a[i]=i;
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(vis[i][k]==1&&vis[k][j]==1)
vis[i][j]=1;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(vis[i][j]==1)
a[j]=a[i];
for(int i=1;i<=n;++i)
if(a[i]==i)
ans++;
printf("%d",ans);
return 0;
} But this problem can not be solved by an ordinary joint search set . Because this question is one-way . The ordinary union search set is bidirectional . So this problem just uses an idea similar to the union search set . What I chose was to use a two-dimensional array to record all the related objects . Then use a three-level nesting for loop . To put all that have a continuous relationship Elements , Make contact . For example, one and two are related , Two and three are related . Then it will be linked . After that, we will traverse this Two dimensional array . Mark the corresponding ancestral relationship . Then iterate over the one-dimensional array used as a marker . You can get that all ancestors are their own . Element this element is the number of discs to burn . This is how I think about this question . The time complexity of this algorithm is relatively high . But it's okay , The data range given by this question is relatively small , So it can also pass .
The above is the summary of today's study .
边栏推荐
- 08_SpingBoot 集成Redis
- Database design (3): database maintenance and optimization
- Swagger
- 6、 Project practice --- identifying cats and dogs
- 35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
- Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
- numpy 随机数
- Install SVN in Pagoda and build SVN version Library
- Analysis report on development trend and market demand of global and Chinese molecular diagnostics industry from 2022 to 2028
- China air compressor manufacturing market demand analysis and investment prospect research report 2022-2028
猜你喜欢
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me

Zhimeng CMS will file a lawsuit against infringing websites
![ctf [RoarCTF 2019]easy_ calc](/img/c7/16adb8e4b64a4be2129a6c53c5aaa7.jpg)
ctf [RoarCTF 2019]easy_ calc

2021-01-31
![Simple personal summary of tp6 multi application deployment -- Part I [original]](/img/7b/65fab1973423081483dacc9bed9594.jpg)
Simple personal summary of tp6 multi application deployment -- Part I [original]

Dameng database backup and restore

微软禁止俄用户下载安装Win10/11

Navicat connects the pit of shardingsphere sub table and sub library plug-ins

Redis cache data consistency solution analysis

Computer network high frequency interview questions
随机推荐
基础查询
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
List of provinces, cities and counties in China
Ubuntu installs PostgreSQL and uses omnidb to view
Clickhouse stand alone installation
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
numpy 通用函数
How to use the configured slave data source for the scheduled task configuration class scheduleconfig
微软禁止俄用户下载安装Win10/11
Svn error command revert error previous operation has not finished; run ‘ cleanup‘ if
What is the best way to store chat messages in a database? [Close] - best way to store chat messages in a database? [closed]
[Qunhui] command line acme SH automatically apply for domain name certificate
numpy 索引及切片
Guide de la pompe de données Oracle
PHP syntax summary
Upload script file (one sentence back door) WAF bypass (PHP)
win10 系统打开的软件太小,如何变大(亲测有效)
PHP has the problem of using strtotime to obtain time in months and months [original]
Daily tests
2021-01-31