online_judge_1012
来源:互联网 发布:亚马逊运营专员知乎 编辑:程序博客网 时间:2024/05/16 06:10
#include <iostream>using namespace std;int Tree[1001];int findRoot(int x){ if(Tree[x] == -1) return x; else { int tmp = findRoot(Tree[x]); Tree[x] = tmp; return tmp; }}int main(){ int n,m; int a,b; int i; int ans; while(cin>>n) { ans = 0; if(n == 0) break; cin>>m; for(i=1; i<=n; ++i) Tree[i] = -1; for(i=0; i<m; ++i) { cin>>a>>b; a = findRoot(a); b = findRoot(b); if(b!=a) Tree[a] = b; } for(i=1; i<=n; ++i) { if(Tree[i] == -1) ans++; } cout<<ans-1<<endl; } return 0;}
这题参考着打满分帝,,并查集看似简单,但是十分好用。。前提是要掌握熟练……
0 0
- online_judge_1012
- 分面导航的详细操作方案
- insert中加入where条件判断,解决插入重复数据的问题
- 关于block使用的5点注意事项
- shell 脚本报错^M: syntax error near unexpected token
- Socket
- online_judge_1012
- 使用Objective-C的文档生成工具:appledoc
- 在公司实习用过的软件
- Java并发编程:volatile关键字解析
- android内存分配android:largeHeap
- 使用maven profile实现多环境可移植构建
- Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成
- Node.js 学习一(Node.js 安装)
- ora-12505错误解决,以及查找oracle安装目录