2016/12/8 1001.连通性问题
2016-12-08 21:09
183 查看
学习到的一种全新的做法,每次输入一个路径时将他们的最老祖先找到并记录,最后做出来代码短的吃惊。
// Problem#: 19852
// Submission#: 4951325
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
using namespace std;
int set[100001];
int findSet(int p);
int main() {
int p, q;
for (int i = 0; i < 100002; i++) {set[i] = i;}
while(cin >> p >> q) {
if (findSet(p) != findSet(q)) {
cout << p << ' ' << q << endl;
set[findSet(p)] = set[findSet(q)];
}
}
return 0;
}
int findSet(int p) {return p == set[p]?p:(set[p] = findSet(set[p]));}
// Problem#: 19852
// Submission#: 4951325
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
using namespace std;
int set[100001];
int findSet(int p);
int main() {
int p, q;
for (int i = 0; i < 100002; i++) {set[i] = i;}
while(cin >> p >> q) {
if (findSet(p) != findSet(q)) {
cout << p << ' ' << q << endl;
set[findSet(p)] = set[findSet(q)];
}
}
return 0;
}
int findSet(int p) {return p == set[p]?p:(set[p] = findSet(set[p]));}
相关文章推荐
- 图的连通性问题——无向图相关_求割顶
- poj-1001-1405-1503-高精度问题
- 连通性(connectivity)问题
- 连通性问题快速查找算法
- POJ 1001-Exponentiation(NYOJ 155 求高精度幂):大数问题
- 连通性问题快速并集算法
- 典型问题--连通性
- 动态连通性问题
- 哈工大 基础算法 1001 计数问题
- 如何测试无序区间队列的连通性问题
- [Algorithm]连通性问题 Connectivity Problem
- [双连通+割点+桥]无向图连通性问题专题
- 【专题】图的连通性问题---有向图的强连通性的求解及应用
- Sicily 4335. 连通性问题
- 在安装Windows 更新MS05-019,Windows2003 SP1或SP2后出现Domino邮件邮递性能下降与网络连通性问题
- 在安装Windows 更新MS05-019,Windows2003 SP1或SP2后出现Domino邮件邮递性能下降与网络连通性问题
- 在安装Windows 更新MS05-019,Windows2003 SP1或SP2后出现Domino邮件邮递性能下降与网络连通性问题
- 程序1-1 连通性问题的快速查找解决方案:
- 【专题】图的连通性问题
- POJ 1001-Exponentiation(NYOJ 155 求高精度幂):大数问题