题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038

题意:数组第 a 个元素到第 b 个元素之间的和为sum;

求有几句话是假的,如果与前面的话有冲突就为假;

r[i]代表i的父节点到i的和;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 201000
#define INF 0xfffffff
using namespace std; int f[N],vis[N],r[N]; int Find(int x)
{
int k = f[x];
if(x != f[x])
{
f[x] = Find(f[x]);
r[x] += r[k];
}
return f[x];
}
int main()
{
int px, py, ans, i, n, m, x, y, sum;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(i=;i<=n;i++)
f[i]=i,r[i]=;
ans = ;
while(m--)
{
scanf("%d%d%d", &x, &y, &sum);
x --;
px = Find(x);
py = Find(y);
if(px != py)
{
f[px] = py;
r[px] = r[y] -r[x] - sum;
}
else if(r[y]-r[x] != sum)
ans++;
}
printf("%d\n",ans);
}
return ;
}

上面的关系都是有方向的。。。

 代码中的123处均与*处有关,大家可以画个图有助于理解;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 202100
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f int f[N], r[N]; int Find(int x)
{
int k = f[x];
if(x != f[x])
{
f[x] = Find(f[x]);
r[x] += r[k];///1;
}
return f[x];
} int main()
{
int n, m, x, y, sum; while(scanf("%d %d", &n, &m)!=EOF)
{
for(int i=; i<=n; i++)
f[i] = i, r[i] = ; int ans = ; while(m--)
{
scanf("%d %d %d", &x, &y, &sum);
x -- ; int px = Find(x);
int py = Find(y); if(px != py)
{
f[px] = py;///*
r[px] = r[y] - sum - r[x];///2;
}
else if(px == py && r[x]+sum != r[y])///
ans++;
}
printf("%d\n", ans);
}
return ;
}

How Many Answers Are Wrong----hdu3038(并查集)的更多相关文章

  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. *HDU3038 并查集

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

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

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

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

  5. hdu 3038 How Many Answers Are Wrong(并查集)

    题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...

  6. hdu3038 How Many Answers Are Wrong 种类并查集

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...

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

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

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

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

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. linux修改文件所属用户和用户组

    使用chown命令可以修改文件或目录所属的用户:       命令:chown 用户 目录或文件名       例如:chown testAdmin /home/work  (把home目录下的xua ...

  2. IIS日志清理(VBS版,JS版)

    IIS默认日志记录在C:\WINDOWS\system32\LogFiles,时间一长,特别是子站点多的服务器,一个稍微有流量的网站,其日志每天可以达到上百兆,这些文件日积月累会严重的占用服务器磁盘空 ...

  3. [Python]小百合十大爬虫

    国庆几天在家看了几篇关于使用Python来编写网络爬虫的博客,想来自己断断续续学习Python也有几个月了,但一个像样的程序都没有写过,编程能力并没有得到提高,愧对自己花费的时间.很多时候虽然知道什么 ...

  4. RESTFul basic introduction

    http://www.ruanyifeng.com/blog/2011/09/restful.html

  5. java远程连接access数据库

    本文转载自http://www.voidcn.com/article/p-tlrtkqlp-k.html 1  rmijdbc远程连接access数据库 正常情况下,常用的数据库sql server, ...

  6. JS基础---->javascript的基础(二)

    记载javascript的一些基础的知识.我们在春风秋雨中无话不说,又在春去秋来中失去了联系. js中string类型 一.字符方法:charAt() 和 charCodeAt() var strin ...

  7. String.Split()函数 多种使用实例

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...

  8. sencha touch 常见问题解答(1-25)

    欢迎留言补充,持续更新中... 1.sencha touch 是什么? 答:Sencha touch框架是世界上第一个基于HTML 5的移动应用框架.它可以让你的Web应用看起来像网络应用.美丽的用户 ...

  9. sencha touch 在线实战培训 第一期 第四节

    2014.1.4晚上8点开的课 第一节收费课程,还是有几位同学付费了,这些课程也录像了的,以后也会持续销售. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容:          ...

  10. 替换linux系统文件etc下passwd文件的字段获取真正的root权限

    sudo与root 很多人都知道使用linux可以利用sudo来执行一些root权限执行的事情,但是sudo和root还是有很大的区别的. 区别一:sudo可以执行的命令是root账户利用/etc/s ...