思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系。假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和。那么给定(x, y, real)可以看作x-1到节点y的和为real

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 200000 + 5;
struct node{
	int par;
	int real;
}a[maxn];

int find(int x, int &root) {
	if(a[x].par == x) {
		root = x;
		return a[x].real;
	}
	int r = a[x].real + find(a[x].par, root);
	//路径压缩
	a[x].par = root;
	return a[x].real = r;
}

bool unionset(int x, int y, int real) {
	int r1, r2;
	int rx = find(x, r1), ry = find(y, r2);
	if(r1 == r2) {
		if(rx - ry != real) return false;
	}
	else { //合并
		a[r1].par = y;
		a[r1].real = real - rx;
	}
	return true;
}

int main() {
	int n, m;
	while(scanf("%d%d", &n, &m) == 2) {
		for(int i = 0; i <= n; ++i) {
			a[i].par = i;
			a[i].real = 0;
		}
		int x, y, r, ans = 0;
		for(int i = 0; i < m; ++i) {
			scanf("%d%d%d", &x, &y, &r);
			if(!unionset(x-1, y, r)) ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
} 

如有不当那个之处欢迎指出!

HDU - 3038 种类并查集的更多相关文章

  1. A Bug's Life HDU - 1829 种类并查集

    //有n个成员,并查集开两倍空间 //1~n为一组, n+1~2n为一组.a与b互斥,则a与b反(即b+n)为同一集合, //同时b与a反(a+n)为同一集合 //在union操作中,引入w ,w越大 ...

  2. hdu 3038(扩展并查集)

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

  3. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  4. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  5. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  6. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  7. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  8. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

  9. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

随机推荐

  1. AMS的适用场景

    AMS适用于网络音视频应用的各种场合,可以独立作为直播点播平台应用,也可以嵌入到用户的各种应用平台中,为客户提供音视频核心支撑,不同于其它提供云服务租给客户使用的产品,AMS是一套安装在企业内部服务器 ...

  2. linkin大话面向对象--包和导入

    我们现在的代码都扔在一个文件夹里面,比如以后我们做项目,是不是有可能有非常非常多的代码,那我就希望把不同功能和模块的类方便管理,放到不同的文件夹下,引出包概念. 什么是包,就一个文件目录,为了处理重名 ...

  3. android Android SDK Manager遇到的问题

    打开Android SDK Manager 1点击左上角的tools-->options:将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy Port分 ...

  4. IEEE754 处理数据变换

    public  class IEEE754 { /// <summary> /// 将二进制值转ASCII格式十六进制字符串 /// </summary> /// <pa ...

  5. IE各个版本的差异性

    1.IE6a.不支持png半透明图片,只能用filter实现b.不支持css的max-width.max-height.min-width.min-height其他不用说,一团糟,不过项目中还是得去兼 ...

  6. Nginx500错误

  7. 浅析调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException

    废话不多说,直接进入正题:如何使用JSR303的validate,进行数据校验,失败后直接抛出异常加入流转信息中,并在form页面提示出来. 首先我们为了启用验证,需要向 项目中添加Bean验证的实现 ...

  8. c#实现自然排序效果,按1,2,11而不是1,11,12,区分字母文字和数字

    排序有时候要考虑后缀.这样看起来比较自然. 参考了codeproject上一篇文章:http://www.codeproject.com/Articles/22978/Implementing-the ...

  9. ansible playbook实践(三)-yaml文件写法

    playbook基于YAML语法来编写,基本语法规则如下: 1.大小写敏感 2.使用缩进表示层级关系 3.缩进时不允许使用Tab键,只允许使用空格 4.缩进的空格数目不重要,只要相同层级的元素左侧对齐 ...

  10. 使用正则表达式和数组形式获取get方法传入的值

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...