加权并查集 似乎就是在想这题的时候突然理解了之前看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. 开始接触BT5-自动登录X界面

    第一种,直接登录root用户的图形界面,不用输入密码 1,安装rungetty 1 # apt-get install rungetty 2,编辑/etc/init/tty1.conf root@bt ...

  2. CSS网页布局:网站整体居中 转

    body{    margin:0 auto;    padding:0 auto; text-align:center;} 只设置body属性对ie浏览器有效果,但是对火狐等不起作用 body{   ...

  3. C#代码 利用MongoDB中Group聚合函数查询

    例子: public static void getUserRFM(DateTime beginTime, DateTime endTime)        {            MongoDat ...

  4. 转 Learning To Rank之LambdaMART的前世今生

    http://blog.csdn.net/huagong_adu/article/details/40710305

  5. linux学习笔记3:linux的网络配置,rpm包,shell以及samba服务器的使用和安装

    1.linux下的shell<linux命令.编辑器和shell编程> (1)shell种类有很多,常用的有三种,在linux可以通过ls -l /bin/*sh 来显示所有已安装的she ...

  6. iOS对象序列化

    系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了. 我在这里主要介绍自定义对象的归档. Sample.h文件 // //  Sample.h //  Serialization // //   ...

  7. Ubuntu 14.10 下MySQL无法远程连接问题

    安装好MySQL之后,如果需要远程连接,那么需要做一些配置,否则会出现一些类似的错误,如 mysql root用户ERROR (): mysql 远程登录 ERROR () mysql 远程登录200 ...

  8. GNURadio For Windows编译安装脚本v1.1.1发布

    GNURadio也能在Windows上运行了,安装GNURadio时,会自动化下载一系列powershell脚本,在源里进行build.然后它依赖为64位原生二进制文件,使用Visual Studio ...

  9. 【Leetcode】 LRU Cache实现

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  10. ASP.NET MVC之视图生成URL

    在MVC的Web应用程序中,我们经常会出现这样的操作,从一个视图跳转到另外一个视图,大部分情况下都是通过链接来实现,当然这是一种非常不错的选择,为什么不说这是最好的选择呢?因为它也有其弊端,我们看看如 ...