博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TZC Intercommunication System
阅读量:4309 次
发布时间:2019-06-06

本文共 1711 字,大约阅读时间需要 5 分钟。

 

Intercommunication System  

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 23            Accepted: 12

Description

2010年是xx国一个多灾多难的一年,灾难使该国的通讯系统遭到了重创,全国共有n个通讯站点,分别从0到n-1进行编号,通讯部门对每两个站点的线路进行了检测,现在要你确定有哪些站点是彼此连通的。

Input

输入数据有多组,每组数据的第一行包含两个整数n和m,其中n为通讯站点个数,接下来有m行,每一行有2个整数a和b,表示站点a和b通讯正常。其中1<=n<=250。
输入以EOF结束。

Output

针对每组输入,将所有连通的站点进行分组,并将每组按照站点从小到大的顺序输出,如果有多组,所有的组根据每组最小的站点编号进行从小到大的排序后输出。
每组数据输出之后加一个空行

Sample Input

 

3 30 11 20 25 10 2

 

Sample Output

 

0 1 20 2134
#include 
#include
#include
#include
#include
using namespace std;const int maxn = 260;int n, m;int f[maxn];vector
res[maxn];int getfather(int x) { if(f[x] == x) return x; return f[x] = getfather(f[x]);}void Merge(int a, int b) { int v1 = getfather(a); int v2 = getfather(b); if(v1 != v2) { if(v1 > v2) { f[v1] = v2; } else { f[v2] = v1; } }}void init() { int i; for(i = 0; i < n; i++) { res[i].clear(); } for(i = 0; i < n; i++) { f[i] = i; }}void work() { int parent; int i, j; for( i = 0; i < n; i++) { parent = getfather(i); res[parent].push_back(i); } for( i = 0; i < n; i++) { if(res[i].begin() != res[i].end()) sort(res[i].begin(), res[i].end()); } for( i = 0; i < n; i++) { if(res[i].size() == 0) continue; if(res[i].size() >= 2) { for( j = 0; j < (int)(res[i].size()-1); j++) { printf("%d ", res[i][j]); } printf("%d\n", res[i][j]); } else { printf("%d\n", res[i][0]); } } cout << endl;}int main(){ int from, to; while(scanf("%d%d", &n, &m) != EOF) { init(); for(int i = 0; i < m; i++) { scanf("%d%d", &from, &to); Merge(from, to); } work(); } return 0;}

 

 

转载于:https://www.cnblogs.com/bbsno1/p/3260353.html

你可能感兴趣的文章
NSObject和反射2
查看>>
使用混合列压缩(HCC)创建表时,收集此表的统计信息可能会失败,会报ORA-03113,并且警报日志显示以下ORA-07445:...
查看>>
/dev/null详解
查看>>
发布功能完成
查看>>
mcast_set_if函数
查看>>
C-RAN 集中化、协作化、云化、绿色节能(4C)
查看>>
统计抽样
查看>>
链表(2)
查看>>
实验四
查看>>
P1341 无序字母对(欧拉回路)
查看>>
UOJ #349. 【WC2018】即时战略
查看>>
15天玩转redis —— 第十篇 对快照模式的深入分析
查看>>
理解MapReduce计算构架
查看>>
【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)
查看>>
jQuery渐隐渐出的文字提示
查看>>
异常记录处理
查看>>
如何定位到append的当前位置,不用拉滚动条scrollIntoView方法
查看>>
我的第一篇Window Live Writer日志
查看>>
MySQL编码、Spring配置中编码、Struts中文问题
查看>>
Controller中使用过滤器
查看>>