zoukankan      html  css  js  c++  java
  • hdu 3038 How Many Answers Are Wrong (并查集)

    How Many Answers Are Wrong

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


    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
     
    Source
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  3035 3036 3037 3031 3034 
     

     核心思想和

    3635 Dragon Balls

    类似。都是要路径压缩,然后在路径压缩的过程中更新点的值。

     1 //62MS    1428K    1051 B    G++ 
     2 /*
     3 
     4     题意:
     5         给出m个关系,每个关系表示[a,b]的和为s。问有多少个是错误的。
     6         
     7     并查集:
     8         sum[i]表示第i个点到其根节点之间的数的和。
     9         
    10          
    11 
    12 */
    13 #include<stdio.h>
    14 #include<string.h>
    15 #define N 200005
    16 int set[N],sum[N];
    17 int find(int x)
    18 {
    19     if(x!=set[x]){
    20         int t=set[x];
    21         set[x]=find(set[x]);
    22         sum[x]+=sum[t]; //加上将改变前的根节点的值 
    23     }
    24     return set[x];
    25 }
    26 int merge(int a,int b,int s)
    27 {
    28     int x=find(a);
    29     int y=find(b);
    30     if(x==y){
    31         if(sum[a]-sum[b]!=s) return 1; //如果在同一集合(即两端都已算出),如果其S与其差值不等证明其错误 
    32     }else{
    33         if(x<y){
    34             set[x]=y;
    35             sum[x]=sum[b]-sum[a]+s;
    36             //printf("**%d %d %d
    ",sum[x],sum[b],sum[a]);
    37         }else{
    38             set[y]=x;
    39             sum[y]=sum[a]-sum[b]-s;
    40             //printf("**%d %d %d
    ",sum[y],sum[b],sum[a]);
    41         }
    42     }
    43     return 0;
    44 }
    45 int main(void)
    46 {
    47     int n,m;
    48     int a,b,s;
    49     while(scanf("%d%d",&n,&m)!=EOF)
    50     {
    51         for(int i=0;i<=n;i++){
    52             set[i]=i;
    53             sum[i]=0;
    54         }
    55         int ans=0;
    56         for(int i=0;i<m;i++){
    57             scanf("%d%d%d",&a,&b,&s);
    58             if(merge(a-1,b,s)) ans++; //注意是a-1 
    59         }
    60         printf("%d
    ",ans);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    Oracle 临时事务表 全局临时表_global temporary table
    js String Trim函数
    解决Navicat Error: Missing required libmysql_d.dll
    win8双屏敲代码
    条件注释判断浏览器
    Eclipse 中Alt+/快捷键失效的解决办法。
    解决java写入xml报错org.w3c.dom.DOMException:DOM002 Illeg
    用解释计划评估创建索引后对单表查询效率的影响
    在某文件夹查找以日期命名的目录,如果早已目标时间则删除之
    (Python)正则表达式进行匹配
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3630020.html
Copyright © 2011-2022 走看看