*HDU3038 并查集
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
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)
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.
//这题处理时将输入的前一个数减一,就可以把他们连在一起了,如: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 并查集的更多相关文章
- HDU3038 How Many Answers Are Wrong[带权并查集]
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)
并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构, 可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...
- HDU3038 How Many Answers Are Wrong 并查集
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3038 题意概括 有一个序列,共n个数,可正可负. 现在有m个结论.n<=200000,m< ...
- 种类并查集——带权并查集——POJ1182;HDU3038
POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- hdu3038 How Many Answers Are Wrong【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...
- 并查集-解决区间和纠错问题 hdu-3038
题目:多次给出信息,告诉你[a,b]区间的和,求多少个错误信息(错误信息不考虑). 乍一看有点像线段树,但想想就发现这个并不能用线段树方便地解决.后来经提醒是并查集的一种经典题型. 把区间抽象为并查集 ...
- hdu3038(种类并查集,推荐)
题目大意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的?? 很有意思的一道题目,要是没有做过种类并查集,我肯定会以为这种题目是线段树题目...... 思路:我 ...
随机推荐
- 用Javascript(js)进行HTML转义工具(处理特殊字符显示)
转自:http://blog.csdn.net/hj7jay/article/details/51280405 众所周知页面上的字符内容通常都需要进行HTML转义才能正确显示,尤其对于Input,T ...
- codeigniter钩子的使用
CodeIgniter 的钩子功能,使得我们可以在不修改系统核心文件的基础上,来改变或增加系统的核心运行功能.可是钩子究竟该怎么用呢?虽然不是很难,不过很多刚用ci的朋友可能还是不明白怎么用. 通过本 ...
- espcms特殊标签
内页banner图从后台调用分类图片 <div style="background:url({%$rootdir%}{%find:type class=$type.topid out= ...
- 我常用的grep命令
查找包含某个字符的行并保存在文件 grep -rn 'test' ./*.sql >test.sql -r 是递归查找 -n 是显示行号 在当前目录下的.sql结尾的文件中查找包含 test 字 ...
- 提额 APP
提额 APP 开始参与这个APP也是巧合,一个月之前,我还在忙于电信运营商的工作,上级就过来问我在之前公司有没有用过 html css js这些.在维库的时候,可是从前台到后台都是要全包的呀,因为项目 ...
- .NET LINQ基本查询操作
获取数据源 在 LINQ 查询中,第一步是指定数据源.像在大多数编程语言中一样,在 C# 中,必须先声明变量,才能使用它.在 LINQ 查询中,最先使用 from 子句的目的是引入数据源 ( ...
- 各种浏览器的Hack写法(chrome firefox ie等)
Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack. 然后将Hac ...
- PHP中删除数组空值的方法
array_filter函数的功能是利用回调函数来对数组进行过滤,如果没有回调函数,那么默认就是删除数组中值为false的项目. 例如 $entry = array( 0 ...
- Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)
1.文件搜索命令:locate [文件名] 在后台数据库中按文件名搜索,搜索速度比find快,耗费资源更少 例子:locate test.txt,就会显示文件名包含 test.txt的所 ...
- RAM和ROM总结
RAM和ROM总结 一.在解释之前先备注一些缩写的全称便于记忆: 1.EPROM:(Electrically Programmable Read-Only-Memory)电可编程序只读存储器 2.EE ...