`
Simone_chou
  • 浏览: 182806 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

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

    博客分类:
  • HDOJ
 
阅读更多

How Many Answers Are Wrong

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


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

    题意:

    给出N(1到200000)个数,和M(1到40000)条信息。每条信息包括A,B,SUM,表示A到B[A,B]的总和为SUM。在给出所以的信息中,判断错误信息有多少条。

   

    思路:

    带权并查集。但是需要对区间处理一下,比如[2,3]和[4,5]合并,得出来的区间是[2,5]。如果不作处理的话,合并后就会有两棵树,2和3一颗,4和5一颗,事实上2到5是连续的,那么3到4之间就会存在有一节空隙。所以要做a--处理,优化后区间就变化为(1,3],(3,5],合并之后就为(1,5]了,那么数字就有连续性了。所以要对区间进行一开一闭处理。

    

     AC:

#include<stdio.h>
#define max 200000
int root[max],re[max];
int find(int a)
{
    if(root[a]==a) return a;
    int r=find(root[a]);
    re[a]+=re[root[a]];
    root[a]=r;
    return r;
}

int main()
{
    int n,m,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    ans=0;
    for(int i=0;i<=n;i++)
     root[i]=i,re[i]=0;
//因为有a--处理,所以i要从0开始初始化
    while(m--)
    {
        int a,b,sum;
        int fa,fb;
        scanf("%d%d%d",&a,&b,&sum);
        a--;
        fa=find(a);
        fb=find(b);
        if(fa!=fb)
        {
            root[fa]=fb;
            re[fa]=re[b]+sum-re[a];
        }
        else
        {
            if(re[a]-re[b]!=sum) ans++;
        }
    }
    printf("%d\n",ans);
    }
    return 0;
}

   

   总结:

   1.一开始以为是合并顺序的问题,所以WA了N遍。原来是因为a--处理后,i的初始化出现问题了,应该从0开始,而不是从1开始。如果从1开始的话,从前者合并到后者就会WA,而从后者合并到前者的话就不会。这是因为从前者合并到后者的话,如果A=1的话,A--后就会为0,那么0就会子节点结在B节点上,这时候的权值re[A]和本身的根节点root[A]都会影响到结果。而从后者合并到前者的话,A=1的情况是最小情况,无论如何B都大于1,那么无论如何都是B节点结到A节点上,这时候A一直都作为根节点,那么其权值re[A]和根节点root[A]其实都不会影响到结果。

      但是事实上,为了防止这类情况发生,初始化都应该从0开始的;

    2.a--的操作要记得。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics