鱼C论坛

 找回密码
 立即注册
查看: 3048|回复: 5

哪位大神提示下这道题目怎么做?并查集的应用

[复制链接]
发表于 2014-5-1 09:51:19 | 显示全部楼层 |阅读模式
1鱼币
本帖最后由 cuibaowenown2 于 2014-5-1 11:41 编辑

题目描述

Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get out of the dormitory and see the beautiful city. So he asks to Xi to know whether he can get to another bus station from a bus station. Xi is not a good man   because he doesn’t tell Bo directly. He tells to Bo about some buses’ routes. Now Bo turns to you and he hopes you to tell him whether he can get to another bus station from a bus station directly according to the Xi’s information.


输入

The first line of the input contains a single integer T (0<T<30) which is the number of test cases. For each test case, the first contains two different numbers representing the starting station and the ending station that Bo asks. The second line is the number n (0<n<=50) of buses’ routes which Xi tells. For each of the following n lines, the first number m (2<=m<= 100) which stands for the number of bus station in the bus’ route. The remaining m numbers represents the m bus station. All of the bus stations are represented by a number, which is between 0 and 100.So you can think that there are only 100 bus stations in Changsha.


输出

For each test case, output the “Yes” if Bo can get to the ending station from the starting station by taking some of buses which Xi tells. Otherwise output “No”. One line per each case. Quotes should not be included.



样例输入

3
0 3
3
3 1 2 3
3 4 5 6
3 1 5 6
0 4
2
3 0 2 3
2 2 4
3 2
1
4 2 1 0 3
样例输出
No
Yes
Yes

提示

来源

中南大学第五届大学生程序设计竞赛-热身赛


我的代码是:
  1. #include <stdio.h>

  2. int father[101];
  3. int rank[101];

  4. void Init()
  5. {
  6.         for (int i=0;i<=100;i++)
  7.         {
  8.                 father[i]=i;
  9.                 rank[i]=0;
  10.         }
  11. }

  12. int Find_Set(int index)
  13. {
  14.         int &x=father[index];
  15.         return x == index ? index : x=Find_Set(x);
  16. }

  17. int Union(int x,int y)
  18. {
  19.         x = Find_Set(x);
  20.         y = Find_Set(y);

  21.         if (x == y)        return x;

  22.         if (rank[x]<rank[y])
  23.         {
  24.                 father[x]=y;
  25.                 return y;
  26.         }
  27.         else
  28.         {
  29.                 if (rank[x]==rank[y])
  30.                 {
  31.                         rank[x]++;
  32.                 }
  33.                 father[y]=x;
  34.                 return x;
  35.         }
  36. }

  37. int main()
  38. {
  39.         int T;
  40.         scanf("%d",&T);
  41.         while (T--)
  42.         {
  43.                 Init();

  44.                 int src,des;
  45.                 scanf("%d %d",&src,&des);

  46.                 int n;
  47.                 scanf("%d",&n);

  48.                 while (n--)
  49.                 {
  50.                         int first;
  51.                         scanf("%d",&first);
  52.                         while (getchar()!='\n')
  53.                         {
  54.                                 int last;
  55.                                 scanf("%d",&last);
  56.                                 if (first != last)
  57.                                         first=Union(first,last);
  58.                         }
  59.                 }
  60.                 if (Find_Set(src) == Find_Set(des))
  61.                         printf("Yes\n");
  62.                 else
  63.                         printf("No\n");
  64.         }
  65.         return 0;
  66. }
复制代码

用的是并查集,但是会超时。用了按秩合并和路径压缩,但还是超时了





想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-1 09:57:49 | 显示全部楼层
看完应该能懂

并查集.zip

63.7 KB, 下载次数: 3

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-1 10:05:42 | 显示全部楼层

东西我看过了。。。都是我已经会了的。我用上了并查集以及按秩合并、路径压缩。。已经比你发的东西还要高效了。。不信你看我代码。。但是还是超时了。纳闷儿了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-1 11:20:40 | 显示全部楼层
http://courses.cs.washington.edu/courses/cse326/09wi/lectures/19-disjoint-union-find-part-2.pdf
看一下他的union-by-size。 应该有用。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-1 12:13:43 | 显示全部楼层
我知道为毛了。。题目读错了。。。。。。。。:huffy::huffy::huffy::huffy::huffy::huffy::huffy::huffy:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-15 10:52:07 | 显示全部楼层
我也刚学数据结构,不会,过来打个酱油,嘿嘿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 11:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表