How Many Answers Are Wrong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6349    Accepted Submission(s): 2395

Problem Description
TT and FF are ... friends. Uh... very very good friends -________-b

FF
is a bad boy, he is always wooing TT to play the following game with
him. This is a very humdrum game. To begin with, TT should write down a
sequence of integers-_-!!(bored).

Then,
FF can choose a continuous subsequence from it(for example the
subsequence from the third to the fifth integer inclusively). After
that, FF will ask TT what the sum of the subsequence he chose is. The
next, TT will answer FF's question. Then, FF can redo this process. In
the end, FF must work out the entire sequence of integers.

Boring~~Boring~~a
very very boring game!!! TT doesn't want to play with FF at all. To
punish FF, she often tells FF the wrong answers on purpose.

The
bad boy is not a fool man. FF detects some answers are incompatible. Of
course, these contradictions make it difficult to calculate the
sequence.

However, TT is a nice and lovely girl. She doesn't have
the heart to be hard on FF. To save time, she guarantees that the
answers are all right if there is no logical mistakes indeed.

What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But
there will be so many questions that poor FF can't make sure whether
the current answer is right or wrong in a moment. So he decides to write
a program to help him with this matter. The program will receive a
series of questions from FF together with the answers FF has received
from TT. The aim of this program is to find how many answers are wrong.
Only by ignoring the wrong answers can FF work out the entire sequence
of integers. Poor FF has no time to do this job. And now he is asking
for your help~(Why asking trouble for himself~~Bad boy)

 
Input
Line
1: Two integers, N and M (1 <= N <= 200000, 1 <= M <=
40000). Means TT wrote N integers and FF asked her M questions.

Line
2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT
answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0
< Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

 
Output
A single line with a integer denotes how many answers are wrong.
 
Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
 
Sample Output
1
 
Source
 
题意:
共n个数每次给出a~b的区间的和 c,问在前面给出的区间和的前提下新给出的区间和是否正确,统计错误的个数。
代码:
 //这题处理时将输入的前一个数减一,就可以把他们连在一起了,如:1,2 3,4 ——>0,2 2,4 ——>0,4 ;并查集之后计算每个点到根节点的距离
//如果输入的两个点有相同的根节点就看他们的距离是否与并查集里的相矛盾。合并时将大的并到小的里面就可以看成每个点到1点的距离
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int fat[],num[];
int find(int x)
{
if(fat[x]!=x)
{
int f=fat[x];
fat[x]=find(fat[x]);
num[x]+=num[f]; // !!!!!!!!
}
return fat[x];
}
void connect(int x,int y,int c)
{
int a=find(x),b=find(y);
if(a<b) // !!!!!!!! 将大的并到小的里面。
{
fat[b]=a;
num[b]=num[x]-num[y]+c;
}
else if(b<a)
{
fat[a]=b;
num[a]=num[y]-num[x]-c;
}
}
int main()
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
fat[i]=i;
num[i]=;
}
int ans=;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--;
if(find(a)==find(b))
{
if(num[b]-num[a]!=c)
ans++;
}
else connect(a,b,c);
}
printf("%d\n",ans);
}
return ;
}

*HDU3038 并查集的更多相关文章

  1. HDU3038 How Many Answers Are Wrong[带权并查集]

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

  2. 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)

    并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构,  可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...

  3. HDU3038 How Many Answers Are Wrong 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3038 题意概括 有一个序列,共n个数,可正可负. 现在有m个结论.n<=200000,m< ...

  4. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

  5. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  6. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

  7. hdu3038 How Many Answers Are Wrong【基础种类并查集】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  8. 并查集-解决区间和纠错问题 hdu-3038

    题目:多次给出信息,告诉你[a,b]区间的和,求多少个错误信息(错误信息不考虑). 乍一看有点像线段树,但想想就发现这个并不能用线段树方便地解决.后来经提醒是并查集的一种经典题型. 把区间抽象为并查集 ...

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

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

随机推荐

  1. Android高手速成--第二部分 工具库

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  2. [Machine Learning] Learning to rank算法简介

    声明:以下内容根据潘的博客和crackcell's dustbin进行整理,尊重原著,向两位作者致谢! 1 现有的排序模型 排序(Ranking)一直是信息检索的核心研究问题,有大量的成熟的方法,主要 ...

  3. JavaScript类型判断instanceof与typeof对比

    经常有人会在JavaScript里写如下的方法: function checkType() { var s1 = 123; var s2 = "OK"; if (s1 instan ...

  4. 12个非常实用的JavaScript小技巧

    在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是 ...

  5. haproxy测试

    环境: Clients:N台linux, 使用“ab -k -c 20000...” 并发2w 请求haproxy Haproxy Server: R610 2*8核 2.4G Real Server ...

  6. AngularJS 初用总结

    一直用惯jquery,初用Angularjs的初段时间,需要先了解一下她的类MVC思想. 初学时几个比较基本的概念: 1.客户端模板 2. Model View Controller (MVC) 3. ...

  7. Gps与地图坐标转换

    内容实在是太太了 7.8MB 以至于浏览器 都奔溃 就算浏览器可以 博客园的文章也保存不了 只好保存到百度云 提供下载 地址: 链接:http://pan.baidu.com/s/16ggIq 密码: ...

  8. Redis3.0 配置文件说明

    背景: 以前有篇文章已经结果过了,现在复习一下,对Redis3.0进行说明: 参数说明: #redis.conf # Redis configuration file example. # ./red ...

  9. 【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持

    如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...

  10. cisco-log

    每个日志消息被关联一个严重级别,用来分类消息的严重等级:数字越低,消息越严重.严重级别的范围从0(最高)到7(最低).  日志消息的严重级别,使用logging命令可以用数字或者名称来指定严重性.  ...