#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int n, m;
const int maxn = ;
int fa[maxn];
int sum[maxn]; int Find(int x){
if (x == fa[x])
return x;
else{
int t = fa[x];
fa[x] = Find(fa[x]);
sum[x] += sum[t];
return fa[x];
}
} bool Set_Union(int x, int y, int s){
int fx = Find(x);
int fy = Find(y);
if (fx == fy && sum[x] != sum[y] + s)
return false;
else if(fx != fy){
if (fx > fy){
fa[fy] = fx;
sum[fy] = sum[x] - s - sum[y];
}
else{
fa[fx] = fy;
sum[fx] = s + sum[y] - sum[x];
}
}
return true;
} void init(){
memset(sum, , sizeof(sum)); //必须初始化为零
for (int i = ; i <= n; i++){
fa[i] = i;
}
} int main(){
while (scanf("%d %d", &n, &m) != EOF){
init(); //初始化
int ans = ;
while (m--){
int a, b, s;
scanf("%d%d%d", &a, &b, &s);
a--;
if (!Set_Union(a, b, s)){
ans++;
}
}
printf("%d\n", ans);
}
//system("pause");
return ;
}

hdu3038 How Many Answers Are Wrong 种类并查集的更多相关文章

  1. hdu3038 How many answers are wrong【并查集】

    TT and FF are ... friends. Uh... very very good friends -________-b  FF is a bad boy, he is always w ...

  2. 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[ ...

  3. [HDU3038]How Many Answers Are Wrong(并查集)

    传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...

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

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

  5. hdu3038(种类并查集,推荐)

    题目大意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的?? 很有意思的一道题目,要是没有做过种类并查集,我肯定会以为这种题目是线段树题目...... 思路:我 ...

  6. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  7. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  8. POJ1733 Parity game —— 种类并查集

    题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

随机推荐

  1. C++模板实现的AVL树

    1 AVL树的定义 AVL树是一种自平衡二叉排序树.它的特点是不论什么一个节点的左子树高度和右子树的高度差在-1,0,1三者之间. AVL树的不论什么一个子树都是AVL树. 2 AVL树的实现 AVL ...

  2. python调用nmap进行扫描

    #coding=utf-8 import nmap import optparse import threading import sys import re ''' 需安装python_nmap包, ...

  3. 设置GridCtrl中的Checkbox 为不可编辑

    m_Grid.SetCellType(index, 1, CGridCtrl::CellType_Check);   //设置第index行.第一列的单元格为类似CheckBox的模样    m_Gr ...

  4. ios 短音效的使用

    1.通用短音效ID的获取 #import <Foundation/Foundation.h> @interface MJAudioTool : NSObject /** * 播放音效 * ...

  5. mysql数据库引擎InnoDB和MyISAM的区别

    InnoDB支持行级锁和表级锁(默认行级锁),支持事务,外部键等:大量的insert和update更快等.只有通过索引条件检索数据,InnoDB 才使用行级锁,否则,InnoDB 将使用表锁. MyI ...

  6. Codeforces Round #326 (Div. 2)

    B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. CSS中的那点事儿(一)--- CSS中的单位2

    在上篇博客提到了%.px.em三个单位,其中最复杂的是em,因为要计算当前元素内的font-size,必须知道其父元素的font-size,层层累积,容易出错.现在CSS3中引入了新的单位rem,改变 ...

  8. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  9. chan

    第一单元:分型.笔.线段 ?1  分型 一.分型.笔和线段所属范畴 缠师在<教你炒股票72:本ID已有课程的再梳理>中对缠论做过这样的说明“本ID的理论,本质上分两部分,一是形态学,二是动 ...

  10. 阿里云短信服务发送短信验证码(JAVA开发此功能)

    开发此功能需注册阿里云账号,并开通短信服务(免费开通) 充值后,不会影响业务的正常使用!(因为发送验证类短信:1-10万范围的短信是0.045元/条).开发测试使用,充2块钱测试足够了 可参考阿里云官 ...