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. 5、Servlet的使用

    一.什么是Servlet:用于开发动态Web资源的的技术.使用Servlet可以读取来自用户端的数据,而实现了用户与服务器之间的动态数据交互.更简单的说就是连接页面和代码. 1.开发一个动态的Web资 ...

  2. 项目里面的某个.m文件无法使用

    - 检查:Build Phases -> Compile Sources

  3. JavaScript闭包(Closure)学习笔记

    闭包(closure)是JavaScript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于JavaScript初学者应该是很有用的. 一.变量的作用域 要理解 ...

  4. inux中fork()函数详解(原创!!实例讲解)

    转载自原创博客,欢迎继续转载 点击跳转到原文

  5. php中几个字符串替换函数详解

    在php中字符替换函数有几个如有:str_replace.substr_replace.preg_replace.preg_split.str_split等函数,下面我来给大家总结介绍介绍. 一.st ...

  6. php文件类

    1.需求 了解php对文件的一些操作 2.例子 写了一个类,可以操作文件,包含增,删,查 <?php class myfile{ public function write_file($stri ...

  7. jquery版小型婚礼(可动态添加祝福语)

    前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久……一直没找到满意的……无意间看到祝福语和一些卡通婚礼图片.最 ...

  8. phpcms中的RBAC权限系统

    PHPCMS中的RBAC权限系统主要用到了4张数据表:管理员表,角色表,菜单表,菜单权限表.先来看看数据库的数据表结构: admin 管理员表 ID 字段 类型 Null 默认 索引 额外 注释 1 ...

  9. python , angular js 学习记录【2】

    1.不同scope之间的通信 (1)无父子关系的scope通信: 在需要操作的scope里面定义一个事件,名称为delete_host,参数为data $rootScope.$on('delete_h ...

  10. sql server日期时间转字符串

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...