当前位置: 首页 > news >正文

免费做彩页网站江北seo综合优化外包

免费做彩页网站,江北seo综合优化外包,郑州网页制作案例,国家新闻大事💯 博客内容:并查集 😀 作  者:陈大大陈 🚀 个人简介:一个正在努力学技术的准C后端工程师,专注基础和实战分享 ,欢迎私信! 💖 欢迎大家:这里是C…

💯 博客内容:并查集

😀 作  者:陈大大陈

🚀 个人简介:一个正在努力学技术的准C++后端工程师,专注基础和实战分享 ,欢迎私信!

💖 欢迎大家:这里是CSDN,我总结知识和写笔记的地方,喜欢的话请三连,有问题请私信 😘 😘 😘

 

目录

初版

路径压缩版 

两个例题 

几张桌子

是不是亲戚 


并查集是一种挺实用的数据结构,可以处理一些不相交集合的合并问题

基本操作有初始化,合并,查找,统计。

咱们先来个初版,再优化一下。

初版

#include<bits/stdc++.h>
using namespace std;
const int N = 10000;
int fa[N];
void init(int N)//初始化
{for (int i = 1; i <= N; i++){fa[i] = i;}
}
int find(int i)//查找
{return i == fa[i] ? i : find(fa[i]);
}
void Union(int i, int j)//合并
{int x = find(i);int y = find(j);fa[x] = y;
}

这种并查集查找的效率太低,最坏情况的时间复杂度能达到O(N)。

我们就针对它优化。

路径压缩版 

#include<bits/stdc++.h>
using namespace std;
const int N = 10000;
int fa[N];
void init(int N)
{for (int i = 1; i <= N; i++){fa[i] = i;}
}
int find(int i)
{if (i == fa[i]){return i;}else{fa[i] = find(fa[i]);return fa[i];}
}
void Union(int i, int j)
{int x = find(i);int y = find(j);fa[x] = y;
}

上面的时间复杂度最好能有O(1)。

实现方式是记忆化搜索,用数组存储好所需结果,需要时就不用再次递归了。

来看两个例题巩固一下。

两个例题 

几张桌子

How Many Tables(并查集)

Problem - 1213 (hdu.edu.cn)

题目:

Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input


5 3 
1 2 
2 3 
4 5

5 1 
2 5

Sample Output

4

思路:每个人是一个数组,两个数组内存的数如果相同,代表认识并成为一桌;(每个数组内刚开始都是自身编号)

给你们一组特殊测试数据:

输入:

1

5 4

1 2

1 3

4 3

3 4

输出:

2
 

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 1050;
int fa[N];
void init()
{for (int i = 1; i <= N; i++){fa[i] = i;}
}
int find(int i)
{if (i == fa[i]){return i;}else{fa[i] = find(fa[i]);return fa[i];}
}
void Union(int i, int j)
{int x = find(i);int y = find(j);fa[x] = y;
}
int main()
{int t, x, y, n, m;cin >> t;//t个测试while (t--){cin >> n >> m;init();for (int i = 1; i <= m; i++){cin >> x >> y;Union(x, y);//合并x和y}int ans = 0;for (int i = 1; i <= n; i++)//统计有多少个集{if (fa[i] == i)ans++;}cout << ans << endl;}return 0;}
是不是亲戚 

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 1050;
int fa[N];
void init()
{for (int i = 1; i <= N; i++){fa[i] = i;}
}
int find(int i)
{if (i == fa[i]){return i;}else{fa[i] = find(fa[i]);return fa[i];}
}
void Union(int i, int j)
{int x = find(i);int y = find(j);fa[x] = y;
}
int main()
{int n, m, n2;;cin >> n >> m;init();for (int i = 0; i < m; i++){int x, y;cin >> x >> y;Union(x, y);}cin >> n2;for (int i = 0; i < n2; i++){int x, y;cin >> x >> y;if (find(x) == find(y))cout << "Yes" << endl;elsecout << "No" << endl;}return 0;}

http://www.hkea.cn/news/602117/

相关文章:

  • 手机网站制作哪家好查关键词
  • 米拓企业网站管理系统电商培训机构排名前十
  • 做效果图有哪些网站seo点击排名
  • 网络营销推广网站收录seo推广排名平台有哪些
  • 产品经理如何看待网站开发广州软件系统开发seo推广
  • wordpress 忘记管理员如何做网站seo
  • app和网站哪个有优势淘宝关键词排名
  • wordpress该域名宁波网站seo公司
  • 建购物网站怎么建呀简单的网站建设
  • 江苏省建设教育协会网站首页百度知道合伙人答题兼职入口
  • 做优化的网站平台搭建
  • 做网站需要多久网络推广是什么专业
  • 厦门加盟网站建设线上推广营销
  • 定制网站案例seo搜索引擎优化薪酬
  • 网站制作成功后怎么使用浏览器观看b站视频的最佳设置
  • 一家专门做开网店的网站北京seo专员
  • 专业企业网站搭建服务头条权重查询
  • 去哪儿网站上做民宿需要材料免费的黄冈网站有哪些平台
  • 网站建设网现在推广什么app最挣钱
  • 嘉兴装修公司做网站安装百度到桌面
  • 电商网站特点外贸营销网站建站
  • 上海市住房城乡建设管理委员会网站网络营销软文范例大全800
  • 莱芜区政协网站做网络优化的公司排名
  • 太原网站建设开发公司电商运营基本知识
  • php做企业网站seo网站推广企业
  • 万网网站备案授权书免费发布推广信息的b2b
  • 乡镇可以做门户网站seo是什么意思职业
  • 建设银行网站优点做个公司网站大概多少钱
  • 网站标题的设置方法哪家建设公司网站
  • 网站空间托管电商平台的营销方式