ural 1106 Two Teams

本文讨论了如何使用并查集和克鲁斯卡尔算法解决最小生成树问题,并通过实例展示了算法的具体实现。

http://acm.timus.ru/problem.aspx?space=1&num=1106

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 bool g[200][200];
 7 int vis[200];
 8 int main()
 9 {
10     int n;
11     scanf("%d",&n);
12     int a;
13     memset(g,false,sizeof(g));
14     for(int i=1; i<=n; i++)
15     {
16         scanf("%d",&a);
17         while(1)
18         {
19             if(a==0) break;
20             g[i][a]=true;
21             g[a][i]=true;
22             scanf("%d",&a);
23         }
24     }
25     int t=0;
26     memset(vis,0,sizeof(vis));
27     for(int i=1; i<=n; i++)
28     {
29         if(!vis[i])
30         {
31             vis[i]=1;
32             t++;
33             for(int j=1; j<=n; j++)
34             {
35                 if(g[i][j])
36                 {
37                 
38                     vis[j]=2;
39                 }
40             }
41         }
42     }
43     printf("%d\n",t);
44     int t1=0;
45     for(int i=1; i<=n; i++)
46     {
47         if(vis[i]==1)
48         {
49             t1++;
50             if(t1==t)
51                 printf("%d\n",i);
52             else printf("%d ",i);
53         }
54     }
55     return 0;
56 }
View Code

 

转载于:https://www.cnblogs.com/fanminghui/p/3612271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值