关闭

hdu 3038 How Many Answers Are Wrong

190人阅读 评论(0) 收藏 举报
分类:

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的序列,给你若干个[a,b]的和,问你有几个是错误的。

并查集来确定集合关系,然后用一个sum[] (表示到根节点的和)来判断是否正确。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define maxn 220000
using namespace std;
int sum[maxn];//指到根节点的和
int pre[maxn];
int findroot(int x)
{
    if(x!=pre[x])
    {
        int f=pre[x];
        pre[x]=findroot(pre[x]);
        sum[x]+=sum[f];
    }
    return pre[x];
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0; i <= n; i++)
        {
            pre[i] = i;
        }
        memset(sum,0,sizeof(sum));
        int ans = 0;
        for(int i = 0; i < m; i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            a--;
            int roota = findroot(a);
            int rootb = findroot(b);
            if(roota != rootb)
            {
                pre[roota] = rootb;
                sum[roota] = sum[b] - sum[a] + w;
            }
            else
            {
                if(sum[a] - sum[b] != w)
                {
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

矩阵十大经典题目之八-hdu-2157-How many ways??

题目大意:给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值 把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=...
  • rowanhaoa
  • rowanhaoa
  • 2014-03-13 01:00
  • 2585

HDU 2053 Switch Game(开灯问题,唯一分解定理)

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • hurmishine
  • hurmishine
  • 2016-05-08 19:23
  • 2050

"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现)解题思路

"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现)解题思路
  • queuelovestack
  • queuelovestack
  • 2016-05-28 19:15
  • 6199

hdu 3038 How Many Answers Are Wrong (带权并查集入门)

http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ...
  • F_cff
  • F_cff
  • 2017-04-20 10:19
  • 212

hdu3038 How Many Answers Are Wrong--种类并查集

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 一:原题内容 Problem Description TT an...
  • LaoJiu_
  • LaoJiu_
  • 2016-04-10 13:45
  • 314

hdu 3038:How Many Answers Are Wrong

TT和FF玩猜数游戏。一个未知数列,FF给出区间[a , b],TT给出该范围内的数字和。但有时TT会给出错误答案。给定数列长度n与区间数m,之后每组给出区间范围a , b与TT给出的和s,求错误答案...
  • u013898034
  • u013898034
  • 2014-03-06 23:05
  • 356

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

题目: How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768...
  • riba2534
  • riba2534
  • 2017-03-06 16:00
  • 270

HDU3038 How Many Answers Are Wrong 带权并查集

1.题目描述: How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3...
  • dragon60066
  • dragon60066
  • 2017-02-26 14:28
  • 107

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

点击打开链接题目链接
  • qq_16843991
  • qq_16843991
  • 2014-08-12 21:08
  • 318

[HDU 3038]How Many Answers Are Wrong[并查集]

题目链接:[HDU 3038]How Many Answers Are Wrong[并查集] 题意分析: 给出区间值的和,问这些话中有多少个是和之前矛盾的。 解题思路: 用一个dis数组记录点到根节点...
  • CatGlory
  • CatGlory
  • 2015-09-11 17:25
  • 495
    个人资料
    • 访问:2217次
    • 积分:382
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条