`
Simone_chou
  • 浏览: 192732 次
  • 性别: 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--的操作要记得。

分享到:
评论

相关推荐

    HDU 3038 How Many Answers Are Wrong 带权并查集

    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 ...

    Yahoo! Answers 问答数据集.7z

    Answers 数据集源于 Yahoo!Answers Comprehensive Questions and Answers 1.0 的 10 个主要分类数据,每个类别分别包含 140000 个训练样本和 5000 个测试样本。 该数据集是截至 2007 年 10 月 25 日的答案语料库,...

    Hands-On Go Programming

    With its C-like speed, simplicity, and power for a growing number ... Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.

    Plane answers to complex questions, 3 edition

    Plane answers to complex questions-The Theory of Linear Models, by Ronald Christensen. The 3 edition.

    CISSP Questions, Answers & Explanations(Preplogic出版 的经典习题集)

    CISSP Questions, Answers & Explanations(Preplogic出版 的经典习题集)

    Yahoo! Answers Topic Classification Dataset(Yahoo! Answers 问答数据集)-数据集

    Answers 数据集源于 Yahoo!Answers Comprehensive Questions and Answers 1.0 的 10 个主要分类数据,每个类别分别包含 140000 个训练样本和 5000 个测试样本。 test.csv classes.txt train.csv

    Vi and Vim: Questions and Answers

    if you have a question about Vi and Vim this is the book with the answers. Vi and Vim: Questions and Answers takes some of the best questions and answers asked on the vi.stackexchange....

    TuckmanSerrat_Answers

    Fixed Income Securities Third Edition Answers

    siemens answers

    ### Siemens Answers 关联知识点 #### 一、北京森社电子有限公司及其主要产品 - **公司简介**:北京森社电子有限公司是一家专业从事设计、生产和销售霍尔电流、电压传感器/变送器(即宇波模块)及变压器/电抗器的...

    SQL and Relational Theory: How to Write Accurate SQL Code

    Nulls in the database cause wrong answers. Why? What you can do about it?How can image relations help you formulate complex SQL queries?SQL supports "quantified comparisons," but they're better ...

    Cambridge.IELTS.7.with.Answers

    Cambridge.IELTS.7.with.Answers

    C# Exercise Answers C# Exercise Answers

    5. **异常处理**:理解try-catch-finally语句块,学会使用throw关键字抛出异常,并能妥善处理运行时错误。 6. **委托与事件**:了解委托的声明和使用,以及如何利用事件处理程序进行异步编程,理解事件发布者和订阅...

    c-language-reference-test-answers.rar_answers

    "c-language-reference-test-answers.rar_answers" 文件集合显然与C语言的学习和实践有关,提供了实验、参考答案以及一些C程序示例。这个压缩包包含两个文件:`c language reference test answers.doc` 和 `zguso....

    大学体验英语听力上机答案3级

    Hi,how are you doing? Pretty good. 2.hi, how are you Fine thanks. 3.good morning Morning what’s new? Not much. Listening Task 1 first listening Answers 4(at school )2(home) 3(at a reception) 1(in ...

    pku acm 2371 Questions and answers代码

    pku acm 2371 Questions and answers代码 采用二叉查找树排序,解题报告请访问:http://blog.csdn.net/china8848

    Cpp_Primer_Answers-master.zip

    《C++ Primer 第五版》是一本广受欢迎的C++编程教材,其课后答案及示例代码集合在"Cpp_Primer_Answers-master.zip"这个压缩包中,为学习者提供了深入理解和实践C++语言的重要资源。这个压缩包包含两个主要部分:GCC_...

    The Complete Computer Vision Course with Python

    While there are many answers to this question, the umbrella answer is computer vision. In this course, you'll use Python to build a variety of tools that reflect the broad range of computer vision ...

    the answers.zip

    在IT行业中,这种格式广泛应用于备份数据、分享文件集或在互联网上下载大文件。zip文件通过压缩技术减小了原始文件的大小,从而节省了磁盘空间并加快了传输速度。 在描述中提到的 "the answers.zip",可能是包含...

Global site tag (gtag.js) - Google Analytics