https://www.icourse163.org/learn/PKU

发布时间:2024-12-11 07:10

使用中国大学MOOC学习中国教育资源:https://www.icourse163.org #生活技巧# #学习技巧# #自学资源推荐#

最新推荐文章于 2021-10-15 09:36:00 发布

Albert_Albert_Albert 于 2018-12-20 19:34:11 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

对于以下等价类,采用“加权合并规则”(也称“重量权衡合并规则”),进行并查运算,给出最后父节点索引序列。For the following equivalence class, please use “weighted union rule” and UNION/FIND algorithm to write down the final parent node index sequence. 4-0 6-2 8-4 9-4 3-5 9-5 5-2 1-2 7-1 注意:当合并大小相同的两棵树的时候,将第二棵树的根指向第一棵树的根;根节点的索引是它本身;数字之间用空格隔开。 Notice: When we join two trees with the same size, we let the root of the second tree point to the root of the first tree. The index of the root node is itself. Separate the numbers with only one spaces.

using namespace std; int p[15], Size[15]; void init(){ for (int i = 0; i < 10; i++) p[i] = i, Size[i] = 1; } int Find(int x){ return x == p[x] ? x : Find(p[x]); } void Union(int a, int b){ int pa = Find(a), pb = Find(b); if (pa != pb){ if (Size[pa] >= Size[pb]){ p[pb] = pa; Size[pa] += Size[pb]; } else{ p[pa] = pb; Size[pb] += Size[pa]; } } } int main(){#ifdef ONLINE_JUDGE#elsefreopen("in.txt", "r", stdin);#endif init(); for (int i = 0; i < 9; i++){ int a, b; scanf("%d%d", &a, &b); Union(a, b); } for (int i = 0; i < 10; i++){ printf("%d ", p[i]); } return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940

网址:https://www.icourse163.org/learn/PKU https://www.yuejiaxmz.com/news/view/441543

相关内容

体育教研部体能教学团队《运动健身原理与方法》课程新学期上线
爬虫+自动化
全部免费!10门让你生活更有趣的网课推荐
8个高质量学习网站,逆袭必备!
现场生命急救知识与技能
学习资源网站工具盘点
https://dev.cdiscount.com/marketplace/?page
提升生活品质——个人效率工具与资讯网站推荐目前用到 Emacs 最多的功能就是 Org mode,用于平时的想法收集、
装饰图案
医学常识与救护

随便看看