加权并查集 似乎就是在想这题的时候突然理解了之前看E题没看懂的标准加权解法

值得注意的技巧 为了让区间之前连成树 形式设定为为(l, r] 接受l的输入后先自减一下就可以了

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std; int father[],sum[];
int n,m,ans;
int find(int x)
{
if(father[x] != x) {
int tmp = father[x];
father[x] = find(father[x]);
sum[x] += sum[tmp];
}
return father[x];
}
void merge(int x,int y,int s)
{
int tx = find(x);
int ty = find(y);
if(tx == ty) {
if(sum[x] - sum[y] != s)
{
ans++;
//printf("tx = %d;ty = %d;\n", tx, ty);
}
return;
}
else
{
father[tx] = ty;
sum[tx] = sum[y] - sum[x] + s;
}
}
int main()
{
while(~scanf("%d%d", &n,&m))
{
for(int i = ; i <= n; ++i)
{
father[i] = i;
sum[i] = ;
}
//printf("father(10) = %d\n",father[10]);
//printf("find(10) = %d\n",find(10));
ans = ;
while(m--)
{
int x, y, s;
scanf("%d%d%d", &x, &y, &s);
x--;
merge(x, y, s);
}
printf("%d\n",ans);
}
return ;
}

kuangbin_UnionFind D (HDU 3038)的更多相关文章

  1. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  2. HDU 3038

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:[1-n]的区间,有m个询问,每个询问表示[a,b]的和是s,问一共有多少组矛盾 sum[i]表示i ...

  3. hdu 3038 How Many Answers Are Wrong

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...

  4. hdu 3038(扩展并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断 ...

  5. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  6. (并查集 添加关系)How Many Answers Are Wrong --Hdu --3038

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...

  8. hdu 3038 How Many Answers Are Wrong(并查集的思想利用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...

  9. HDU 3038 How Many Answers Are Wrong(种类并查集)

    题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...

随机推荐

  1. javascript面向对象知识点

    首先,声明何为对象:对象是键值对的集合 其次,声明:变量就是键值对 再次,声明:函数也是变量 1. JavaScript包含:ECMAScript(核心).DOM(文档对象模型)和BOM(浏览器对象模 ...

  2. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bashexit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.sy ...

  3. Tarjan算法求有向图的强连通分量

    算法描述 tarjan算法思想:从一个点开始,进行深度优先遍历,同时记录到达该点的时间(dfn记录到达i点的时间),和该点能直接或间接到达的点中的最早的时间(low[i]记录这个值,其中low的初始值 ...

  4. Node.js +Express+MongoDB+mogoose+ejs+bootstrap+jquery

    Node.js + MongoDB 项目实战(二)  创建项目 在项目实战(一)中,已经配置好了开发环境(详见:http://www.cnblogs.com/jameslong/articles/34 ...

  5. BZOJ 1486 最小圈

    二分答案是显然的,我们需要dfs版spfa判一下负环. 看起来是n^2其实很快. #include<iostream> #include<cstdio> #include< ...

  6. linux常用命令:2权限管理命令

    权限管理命令 1.权限管理命令:chmod 命令名:chmod 命令英文原意:change the permissions mode of a file 命令所在路径:/bin/chmod 执行权限: ...

  7. Net作业调度-----Quartz.Net

    一:业务需求: 项目需要在不同时刻,执行一个或很多个不同的作业. Windows执行计划这时并不能很好的满足需求了,迫切需要一个更为强大,方便管理,集群部署的作业调度框架. 二:介绍 Quartz一个 ...

  8. GET请求和POST请求简单说明

    一.概述 * HTTP/1.1协议共定义了8中请求方法:OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT. * GET方法和POST是我们使用 ...

  9. Linux--/tmp目录文件重启后自动删除

    源博客 http://blog.itpub.net/24996904/viewspace-769327/ 在/etc/default/目录下有个rcS文件,文件内容如下:## /etc/default ...

  10. 用 CSS 实现字符串截断

    [要求]:如何用css实现字符串截断,超出约定长度后用缩略符...代替?   ♪ 答: <html> <head> <meta charset="UTF-8&q ...