TZC Intercommunication System
2013-08-15 18:17
225 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn = 260; int n, m; int f[maxn]; vector<int> 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; }
相关文章推荐
- TZC Intercommunication System
- Intercommunication System
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- ubuntu下连接mysql时报错:Lost connection to MySQL server at 'reading initial communication packet', system
- MQ Intercommunication
- DFHIR3780 WUI01 Unable to start interregion communication. Return code=X'000000
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- Lost connection to MySQL server at 'reading initial communication packet et system error:0
- Inter-Process Communication Affects Application Response Time
- POJ 1018 Communication System dp
- Communication Between Hosting System and Its Plugin Applications(Cross Domains)
- SharePoint Server 2007 SDK中的工作流示例:InterSystemWorkflow
- poj 1018 Communication System DP思想
- POJ 1018 Communication System DP
- Use Named Pipes and Shared Memory for inter process communication with a child process or two
- Service oriented architecture for inter-organizational IT resources sharing system
- 《A Protocol for Packet Network Intercommunication》 阅读报告
- Handle inter-process communication between PhantomJS and Golang processes via hixie-76 websockets
- TAxThread - Inter thread message based communication - Delphi
- 【贪心】Communication System POJ 1018