Corporative Network UVA - 1329
2017-10-25 20:29
453 查看
题目:https://vjudge.net/problem/UVA-1329
题目大意
给你n个点进行操作,I表示连接操作,A连接B就表示A指向B的中心。E 表示查询从某个点到中心的长度,点之间的长度等于它们做差的绝对值。
分析
In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B这句可以看出当一个点与另一个点连接,这个点的中心被另一个点的中心取代,这是并查集的性质。有位有距离属性,所以并查集加权。
代码
题目大意
给你n个点进行操作,I表示连接操作,A连接B就表示A指向B的中心。E 表示查询从某个点到中心的长度,点之间的长度等于它们做差的绝对值。
分析
In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B这句可以看出当一个点与另一个点连接,这个点的中心被另一个点的中心取代,这是并查集的性质。有位有距离属性,所以并查集加权。
代码
/******************************************************************** * File Name: Corporative_Network.cpp * Author: Sequin * mail: Catherine199787@outlook.com * Created Time: 三 9/ 6 16:24:07 2017 *************************************************************************/ #include <iostream> #include <algorithm> using namespace std; #define maxn 30000 int p[maxn]; int dis[maxn]; int find(int x) { if(x == p[x]) return x; else{ int root = find(p[x]); dis[x] += dis[p[x]]; p[x] = root; return root; } } int main() { int n; cin >> n; while(n--) { int N; cin >> N; for(int i = 1; i <= N; i++) { dis[i] = 0; p[i] = i; } char t; while(cin >> t && t != 'O'){ if(t == 'E') { int x; cin >> x; find(x); cout << dis[x] << endl; } if(t == 'I') { int a, b; cin >> a >> b; p[a] = b; dis[a] = abs(b - a) % 1000; } } } }
相关文章推荐
- Corporative Network UVA - 1329 加权并查集(压缩路径优化)
- UVA 1329 - Corporative Network
- C - Corporative Network UVA - 1329 (并查集)
- UVA 1329 Corporative Network
- C - Corporative Network UVA - 1329 -加权并查集
- Corporative Network UVA - 1329(并查集的修改与查询)
- Corporative Network UVA - 1329 加权并查集
- Corporative Network UVALive - 3027 并查集
- UVALive 3027---Corporative Network+并查集的应用
- Corporative Network +uvalive+并查集
- UVA - 1267 Network
- UVA1267 - Network
- uva 1267 - Network(dfs)
- 并查集 Corporative Network POJ 1962
- uva 1329
- UVALive 3270 Simplified GSM Network 二分+Floyd
- B - Network---UVA 315(无向图求割点)
- uva1267 Network
- Corporative Network UVA - 1329 加权并查集
- UVaLive(LA) 3902 Network,Seoul 2007