UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用。主要使用路径压缩。甚至不须要合并树了,由于没有反复的连线和改动单亲节点的操作。

郁闷的就是不太熟悉这个Oj系统,竟然使用库中的abs就会WA,自己写了个abs小函数就过了。

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075

#include <stdio.h>

const int MAX_N = 20005;
const int MOD = 1000;
//在UVa使用库的abs竟然WA,浪费好多时间
inline int abs(int a) { return a < 0? -a : a; } struct Subset
{
int p, w;
}; Subset subs[MAX_N]; int findParent(int x)
{
if (subs[x].p != x)
{
int p = subs[x].p;
subs[x].p = findParent(subs[x].p);
subs[x].w = (subs[x].w + subs[p].w);
}
return subs[x].p;
} void initSubs(int N)
{
for (int i = 1; i <= N; i++)
{
subs[i].p = i;
subs[i].w = 0;
}
} int main()
{
int T, N, i, j;
char cmd;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
getchar();
initSubs(N);
while ((cmd = getchar()) && cmd != 'O')
{
if (cmd == 'E')
{
scanf("%d", &i);
subs[i].p = findParent(i);
printf("%d\n", subs[i].w);
}
else
{
scanf("%d %d", &i, &j);
subs[i].w = (abs(j - i))%MOD;
subs[i].p = j;//不存在反复连线和更改parent,故此直接连就ok
}
getchar();
}
}
return 0;
}

UVa 1329 - Corporative Network Union Find题解的更多相关文章

  1. UVA 1329 Corporative Network【并查集】

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  2. UVA 1329 - Corporative Network

    带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...

  3. UVA 3027 Corporative Network 带权并查集、

    题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...

  4. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  5. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

  6. 【35.86%】【POJ 1962】Corporative Network

    Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3943 Accepted: 1414 Description A very bi ...

  7. UVALive 3027 Corporative Network

    ---恢复内容开始--- Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  8. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  9. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

随机推荐

  1. 让Linux应用更加得心应手的

    1.计算文件数和目录数  下面的语句可以帮你计算有多少个文件和多少个目录 # ls -l * |grep "^-"|wc -l ---- to count files # ls - ...

  2. 搜索关键词智能提示suggestion

    转载自:stormbjm的专栏 题目详情:百度搜索框中,输入“北京”,搜索框下面会以北京为前缀,展示“北京爱情故事”.“北京公交”.“北京医院”等等搜索词,输入“结构之”,会提示“结构之法”,“结构之 ...

  3. vue 子父组件之间的通信,及在调用组件的地方

    这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...

  4. Dockerfile 备份

    dotnet core app FROM microsoft/dotnet:1.1.0-runtime WORKDIR /mvcApp COPY ./out . ENTRYPOINT ["d ...

  5. 【C#日期系列(一)】--C#获取某月第一天0分0秒以及最后一天59分59秒

    工作中可能会遇到很多不常见的需求,比如这次需要获取某个月的第一天和最后一天 #region 取得某月的第一天0分0秒 /// <summary> /// 取得某月的第一天0分0秒 /// ...

  6. nhibernate 比较运算符

    比较运算符 HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于 <> Restrictions.not(Exprission.eq()) 不等于 > Re ...

  7. linux网络管理----网络命令

    1 Linux网络命令之网络环境查看命令  ifconfig  2 Linux网络命令之网络测试命令  telnet 基本已经被ssh替代了,telnet是明文传递,不安全

  8. 项目中jquery插件ztree使用记录

    最近公司要求做一个关于后台的管理系统.在这个mvvm模式横行的年代,虽然这里用jquery做项目可能有点不符合时代的潮流,但是管他呢,能做出来先在说呗(公司以后要改用angular或者vue来统一前端 ...

  9. Mac 命令行美化

    在 mac 中使用原生的命令行工具,竟然没有 git 命令的自动补全,在 git 仓库下也看不到当前的分支名,不能忍.于是,开始一波改造. 目标:命名 Tab 自动补全:可以显示分支名: 一番 Goo ...

  10. prim 算法和 kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...