HDU 3038(并查集 带权值)
2016-08-01 20:02
357 查看
How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5651 Accepted Submission(s): 2140
[align=left]Problem Description[/align]
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)
[align=left]Input[/align]
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.
[align=left]Output[/align]
A single line with a integer denotes how many answers are wrong.
[align=left]Sample Input[/align]
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
[align=left]Sample Output[/align]
1
[align=left]Source[/align]
[align=left]
[/align]
[align=left]题意:[/align]
[align=left] 有n次询问,给出a到b区间的总和s,问这n次给出的总和中有几次是和前面已近给出的是矛盾的。[/align]
[align=left] 麻痹的,这道题我也是醉了,怎么交就是不对。[/align]
[align=left] 具体思路就是按照并查集的思想,我们把有关系的点放在一个集合,a,b是一个并查集里边的元素,那么s==sum[b]-sum[a]才能合法,这里给的是区间[a,b],就是sum[b]-sum[a-1]的和;顺便加一个每一个节点到根的距离,这样的话,任意两个点之间关系就可以通过求与根的距离求差得出,也就是说,如果输入的a,b在一个集合里,那么我们判断这两个的关系是否和已有的冲突,如果a,b不在一个集合里,那么我们就合并这两个集合,是的a,b这两个所在的两个集合之间的任意元素都有关系。[/align]
[align=left]
[/align]
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int f[200005]; int sum[200005]; ///sum[i]存的是i到他父亲这条路上的的和 int find(int x) { if(x!=f[x]) { int pre=f[x]; f[x]=find(f[x]); ///递归找出祖先 sum[x]+=sum[pre]; ///路径和压缩 } return f[x]; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { memset(sum,0,sizeof(sum)); for(int i=0;i<=n;i++) { f[i]=i; } int ans=0; for(int i=0;i<m;i++) { int a,b,s; scanf("%d%d%d",&a,&b,&s); a--; int fa=find(a); int fb=find(b); if(fa!=fb) { f[fb]=fa; sum[fb]=s+sum[a]-sum[b]; } else //在一个集合里,判断是否满足该关系 { if(sum[b]-sum[a]!=s) ans++; } } printf("%d\n",ans); } }
[align=left]
[/align]
相关文章推荐
- 查看mysql主从配置的状态及修正 slave不启动问题
- VBA中msgbox的用法小结
- cocos2dx-lua3.12版本保存图片、截屏显示
- 主流JVM的组成分析
- 数据结构实验之栈:行编辑器
- Vitamio 依赖导入 步骤
- 畅通工程(模板)(并查集)
- 【CDOJ 1339】郭大侠与线上游戏
- Activiti的表详细
- NKOI 1937 太空飞行计划
- 11.8 编写一个程序,有一个汽车类 vehicle,它具有一个需传递参数的构造函数,类 中的数据成员:车轮个数 wheels 和车重 weight 放在保护段中;小车类 car 是它的私有派生 类,
- java Calendar的使用方法
- Java集合--Set集合总结
- 10.3 设计一个类模板 store <T>用于存储某一类型的数据,并以整数和字符串进行实 例化
- EasyUI 属性网格
- 采用JLink+ADS1.2调试uboot的方法
- Microsoft.XMLHTTP基本用法
- BZOJ 2190
- SDUT2506完美网络
- Html5元素及基本语法