题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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~n;

给出m个查询,每个查询的答案由a,b,s三个数组成,表示从第a个数加到第b个数的和为s;

但是其中有一些是有矛盾的(或者说错误的),求错误的查询答案有多少个。

题解:

经典的带权并查集问题;

我们设sum[x]是这n个数的前缀和数组,那么就可以把所有的 a+…+b = s 都变成 sum[b] - sum[a-1] = s;

那么我们就可以利用带权并查集进行进行建树,照例par[x]代表了节点x的父亲节点,而val[x]代表了sum[x] - sum[par[x]];

首先是find()函数,我们在原来最基础的并查集find函数中,是有一种类似于把x的父亲节点par[x]变成par[par[x]]这样的,将节点沿着树枝往上移动的操作,

那么相应的,我们每次做将par[x]变成par[par[x]]这样的操作时,val[x]也要修改为val[x]+val[par[x]];

其次,我们对于每次查询的结果a,b,s,令 t1 = find(a),t2 = find(b),那么对应的s,val[a],val[b],val[t2]就可以如下图的向量所示:

          

向量的方向相当重要(因为这个WA了两发= =):对于任何的从x指向y的向量都代表了sum[x] - sum[y];

那么很显然的有:

①若a,b还不是一个集合内,那么就要unite他们,即令par[t2]=t1;

 那么同时,根据向量的加减法,val[t2]也要跟随着par[t2]的变动,转变成sum[t2] - sum[t1];所以根据向量加减的规则就有val[t2] = - val[b] + s + val[a];

②若a,b已经是一个集合内的,那么就要判断是否有矛盾;

 显然此时t1=t2,那么如果s是正确的话,就应该有 - val[b] + s = val[a],否则,s就是一个与前面产生矛盾的、有错误的查询结果;

AC代码:

#include<bits/stdc++.h>
const int maxn=+; int n,m; int par[maxn],val[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,val[i]=;}
int find(int x)
{
if(par[x]==x) return x;
else
{
int root=find(par[x]);
val[x]+=val[par[x]];
return par[x]=root;
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init(,n);
int ans=;
for(int i=,a,b,s;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&s); a--; int t1=find(a),t2=find(b);
if(t1!=t2)
{
par[t2]=t1;
val[t2]=-val[b]+s+val[a];
}
else
{
if(val[a]+s!=val[b]) ans++;
}
}
printf("%d\n",ans);
}
}

HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]的更多相关文章

  1. HDU 3038 How Many Answers Are Wrong(带权并查集)

    传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...

  2. HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. HDU - 3038 How Many Answers Are Wrong (带权并查集)

    题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...

  4. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...

  5. HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...

  6. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. 【HDU 3038】 How Many Answers Are Wrong (带权并查集)

    How Many Answers Are Wrong Problem Description TT and FF are ... friends. Uh... very very good frien ...

  9. POJ 1182 食物链 (经典带权并查集)

    第三次复习了,最经典的并查集 题意:动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们 ...

随机推荐

  1. xcode 5.1打包iOS 7.1应用问题笔记

    XCODE 5.1默认情况下是要求应用都通过64位编译.但是往往有些第三方的类库还是32位.还木有更新64位类库.使得项目编译出错. 解决办法: BuildSetting 的Valid Archite ...

  2. 5 -- Hibernate的基本用法 --4 5 JNDI数据源的连接属性

    如果无须Hibernate自己管理数据源,而是直接访问容器管理数据源,Hibernate可使用JNDI(Java Naming Directory Interface,Java命名目录接口)数据源的相 ...

  3. UTF8 & GBK之间的转换

    使用lua的时候,在lua中给字符串赋值的中文,但是在C中读出来的就是乱码,是因为在lua中使用的是UTF8编码,而在C(windows下面)中使用的是GBK编码,将UTF8转成GBK就可以了,下面的 ...

  4. GSAP JS基础教程--动画的控制及事件

    好多天没有写无博文啦,今天无聊就再写一下! 今天要讲的是TweenLite的一些事件以及,TweenLite动画的控制,TweenMax类似,请自行参考官方文档:http://api.greensoc ...

  5. vsftpd下错误之:500 OOPS

    vsftpd下错误之:500 OOPS.vsftpd 是在Linux发行版中最推崇的一种FTP服务器程序,vsftpd的特点:小巧轻快.安全易用等. Linux也是为人们所常用的操作系统之一.这里主要 ...

  6. PHP 二叉树 二叉排序树实现

    <?php /** * PHP 二叉树 * @author : xiaojiang 2014-01-01 * */ class Tree { protected $k = null; prote ...

  7. Linux线程编程之信号处理

    前言 Linux多线程环境中的信号处理不同于进程的信号处理.一方面线程间信号处理函数的共享性使得信号处理更为复杂,另一方面普通异步信号又可转换为同步方式来简化处理. 本文首先介绍信号处理在进程中和线程 ...

  8. linux c++环境

    set expandtab set autoindent set smartindent

  9. 【python系列】python初识

    前言 Python是一种高层次,解释,互动性和面向对象的脚本语言,Python被设计成具有很强的可读性语言.它采用应用关键字,而其他语言一般使用标点符号,并且具有比其他语言有较少的语法结构. Pyth ...

  10. SharpGL学习笔记(五) 视口变换

    视口变换主是将视景体内投影的物体显示到二维的视口平面上. 在计算机图形学中,它的定义是将经过几何变换, 投影变换和裁剪变换后的物体显示于屏幕指定区域内. 前面我们讨论过的透视投影, 正射投影, 它们都 ...