#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. oracle user locke

    1:管理员登录 sqlplus sys/pwd as sysdba sql->alter user jd account unlock; commit; SQL> password new ...

  2. iOS之UI--涂鸦画板实例

     iOS之UI--涂鸦画板实例  首先是搭建框架 其他的略过,直接展示效果: 然后接下来上传搭建好两个控制器框架的源码百度云下载链接: http://pan.baidu.com/s/1skjpDox  ...

  3. (转)gcc学习笔记

    1.gcc -Wall hello.c -o hello //编译源文件,显示警告信息 2../a.out   //运行程序 3.gcc -Wall calc.c /usr/lib/libm.a -o ...

  4. 【python】How to change the Jupyter start-up folder

    Copy the Jupyter Notebook launcher from the menu to the desktop. Right click on the new launcher and ...

  5. 项目Beta冲刺(团队6/7)

    项目Beta冲刺(团队6/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...

  6. GB2312,GBK,GB18030,UTF8四种汉字编码标准有什么差别和联系

     从GB2312.GBK 到 GB18030,这些编码方法是向下兼容的,即同一个字符在这些方案中总是有同样的编码,后面的标准支持很多其它的字符.在这些编码中,英文和中文能够统一地处理. 区分中文编 ...

  7. 小程序登录方式切换 不做url跳转

    var filegUP = require('../../utils/getUserPassword.js'); var filemd5 = require('../../utils/md5.min. ...

  8. redis09---redis 服务器端命令

    redis 服务器端命令 db0,db1,db2是数据库,外层是服务器,服务器下面有20个数据库. :>time ) "" //多少秒 ) "" //多少 ...

  9. express 中文文档

    express() 创建一个express应用程序 var express = require('express'); var app = express(); app.get('/', functi ...

  10. skynet实践(8)-接入websocket

    我从开源项目(https://github.com/lipp/lua-websockets,这里我们简称LWS)中抽出了websocket的部分处理,步骤如下: 1)首先是解决LWS的几个依赖问题.L ...