LA3027:Corporative Network(并查集) uva 1329
来源:互联网 发布:oracle sql优化书籍 编辑:程序博客网 时间:2024/05/22 14:16
题目大意: 有个很大的网络发展,现在有n个独立的结点。输入一串命令,如果是E u 就查询u到他的根结点的距离,如果是I u v,就让v做u的父节点,距离为u-v的绝对值模1000的。
题目分析:并查集问题,比较简单,在find函数里面加个counts 记录d【r】的累加。
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn = 200010;int pa[maxn],d[maxn];int counts;int find(int x){counts=0;if(pa[x] == x){return counts;}else {counts = d[x];int r=pa[x];while(r!=pa[r]){counts+=d[r];r=pa[r];}return counts+d[r];}} int main(){int n,t,k;int u,v;char sh[10],ch;scanf("%d",&t);while(t--){for(int i=0;i<=maxn;i++) pa[i]=i,d[i]=0; scanf("%d",&n); while(~(scanf("%s",sh))&&sh[0]!='O'){ if(sh[0] == 'E'){ scanf("%d",&u); printf("%d\n",find(u)); }if(sh[0] == 'I'){scanf("%d%d",&u,&v); pa[u]=v; d[u] =abs(u-v)%1000;} }}return 0;}
0 0
- LA3027:Corporative Network(并查集) uva 1329
- LA3027 - Corporative Network,并查集
- LA3027:Corporative Network(并查集)
- LA3027 Corporative Network 带权并查集入门
- UVA 1329Corporative Network【并查集】
- UVA 1329 Corporative Network(并查集)
- UVa 1329 Corporative Network (加权并查集)
- UVA 1329 Corporative Network(并查集:路径压缩)
- UVa:1329 Corporative Network(带权并查集)
- UVa 1329 Corporative Network(带权并查集)
- uva 1329 - Corporative Network(数据结构:并查集)
- UVA 1329 - Corporative Network(带权并查集)
- uva 1329 - Corporative Network(加权并查集)
- UVA - 1329 Corporative Network 合作网络(并查集)
- UVa 1329 Corporative Network(并查集)
- UVA 1329 Corporative Network——加权并查集
- 1329 - Corporative Network(并查集)
- Corporative Network LA3027
- Ext.tree如何创建树以及动态加载树
- throw er; // Unhandled 'error' event
- Vivado Hls 设计分析
- Java中Map与HashMap,Hashtable,HashSet的区别
- adb shell命令整理之pm
- LA3027:Corporative Network(并查集) uva 1329
- Spring AOP术语
- 2016年的酷开是如何花样崛起的?
- CSS+DIV
- PHP PDO MySql 用户删除userdelete.php
- 21 leetcode - Valid Parentheses
- adb shell命令整理之am
- 22 leetcode - Merge Two Sorted Lists
- 十字路口