HDU3038 How Many Answers Are Wrong(带权并查集)

来源:互联网 发布:淘宝网拼多多 编辑:程序博客网 时间:2024/05/20 19:28

题目:

How Many Answers Are Wrong

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


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 51 10 1007 10 281 3 324 6 416 6 1
 

Sample Output
1
 

Source
2009 Multi-University Training Contest 13 - Host by HIT
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3033 3035 3036 3037 3031 
思路:

题意:有 n 个数,你不知道它们的值, 然后又有 m 行数,每行 a ,b ,c,表示 a 到 b 之间所有数的和为c(包含了第a个和第b个数)。但是这m行数里面有些是错的,就是与前面给的条件相冲突的,要求你最后输出错了几行。用r录每一个点跟根节点的关系。
首先我们可以把问题稍微转化一下,就是如果已知[3,6],[7,10]俩个区间内各自所有数的和,那么就可以[3,10]内所有数的和,可是,这俩个区间根本就不衔接,所有要稍微处理一下,将左区间值减1,就变成了[2,6],[6,10],这样就方便处理了。
既然这样的话,[2,6]区间内所有数的和就完全可以等价于点2到点7之间的距离了。
代码:

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N 200000+20#define M 200010#define MOD 1000000000+7#define inf 0x3f3f3f3fusing namespace std;int pre[N], dis[N],cnt;//dis表示这个节点到根节点的距离void init(int n){    for(int i=0; i<=n; i++)    {        pre[i]=i;        dis[i]=0;    }}int find(int x){    if(pre[x]==x)        return x;    int temp=pre[x];//找到x的父亲节点    pre[x]=find(pre[x]);//找到x的根节点    dis[x]+=dis[temp];//更新x到根节点的距离    return pre[x];}void mix(int x,int y,int s){    int fx=find(x);    int fy=find(y);    if(fx!=fy)    {        pre[fy]=fx;        dis[fy]=dis[x]+s-dis[y];//类似于向量    }    else    {        if(abs(dis[x]-dis[y])!=s)//如果y到根节点的距离减去x到根节点的距离不是s,那么就证明这一句错误            cnt++;    }}int main(){    int n,m,a,b,c;    while(~scanf("%d%d",&n,&m))    {        cnt=0;        init(n);        for(int i=1; i<=m; i++)        {            scanf("%d%d%d",&a,&b,&c);            mix(a-1,b,c);        }        printf("%d\n",cnt);    }    return 0;}

这道题和HDU3047几乎一模一样,都可以用来理解带权并查集



0 0
原创粉丝点击