HDU 3038 How Many Answers Are Wrong 很有意思的一道并查集问题

题目大意:TT 和 FF玩游戏(名字就值五毛),有一个数列,数列有N个元素,现在给出一系列个区间和该区间内各个元素的和,如果后出现的一行数据和前面一出现的数据有矛盾,则记录下来。求有矛盾数据的数量。

题目思路:刚刚拿到手时一脸懵逼,这是并查集?后来发现还真是并查集 - -!!

如果数据有错那么会是什么情况?

1-10 10

1-5   5

6-10  4

很明显第三行的数据和已知的数据产生了矛盾,我们分析一下矛盾是如何产生的。

我们用v[i]来统计最右端为i的区间和,那么:

第一行数据得知v[10]=10;

第二行数据得知v[5]=5;

第三行数据在与第二行数据合并时发现 V[10]=9.

这样矛盾便产生了。

由于存在集合划分的问题,所以我们理应想到并查集去解决问题。具体问题看代码吧,代码我觉得解释的比较详细了

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1000005

using namespace std;

int father[MAX],v[MAX],n,m;

int Find(int x)
{
    if(father[x]==-1)
        return x;
    int k=Find(father[x]);

    v[x]+=v[father[x]];//对v[x]进行更新

    return father[x]=k;
}

int main()
{
    int i,j,a,b,c;

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int ans=0;
        for(i=0;i<MAX;i++)
        {
            father[i]=-1;
            v[i]=0;
        }

        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);

            a--;//为什么要减一?你知道了区间1-5,和6-10的和,如果你不把6减1,你如何经行下面的更新呢?

            int x=Find(a);
            int y=Find(b);

            if(x!=y)//如果发现两个根节点不相同,则经行更新
            {
                father[y]=x;

                v[y]=v[a]-v[b]+c;//由式子:v[y]=v[a]-(v[b]-c),化简而得,自己可以画个图推一下,并不难。
            }

            else//如果根节点相同了,我们就可以经行判断了
            {
                if(v[b]-v[a] !=c)//v[b]-v[a]根据之前的条件得到的区间a-b的和,如果这个值并不等于C证明与前面矛盾。
                    ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-08-29 16:03:08

HDU 3038 How Many Answers Are Wrong 很有意思的一道并查集问题的相关文章

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

题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用,[a, b]和为s,所以a-1与b就可以确定一次关系,通过计算与根的距离可以判断出询问的正确性 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int f[MAXN],a

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri

HDU 3038 How Many Answers Are Wrong (带权并查集+区间判断)

题意:给你长度为n的区间,m个询问:a,b,c,问这m个问题有多少个是错误的(矛盾). 10 5 1 10 100 7 10 28 1 3 32 4 6 41 6 6 1 由6->6=1,  4->6=41 知4->5=40; 同理 由1->10=100,7->10=28 知1->7=72; 又由1->3=32,4-6=41 知1->7=73,与上面矛盾: 所以答案为1: #include<cstdio> #include<stdlib.h

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路.统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求.还有别忘了判断是否连通,此时用到并查集,图中所有的边

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出两个数N和M代表有N个数个M组数据 N个数是未知的,然后M组数据每组数据形如:l r x 代表位置l和位置r之间的数的和为x,最后求出M组数据中有 几组是与上面冲突的. 分析:这道题我直接就知道是带权并查集的题,原因是我直接按带权并查 集找的题,所以在这不能为诸君讲述有此类题如何联系到并查集的过程. 直接说方法吧,首先定义数据level[maxn]存相权值初始化为0,level[x]

HDU 3038 - How Many Answers Are Wrong

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 intege

hdu 3038 How Many Answers Are Wrong(并查集)

题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum(ai...bi)=si.假设下一个是sum(ai,ci)=sj.则sum(ai,ci)肯定也知道了.这很符合并查集的结构. *:画个图. 代码: int n,m; int fa[200005]; int sum[200005]; int findFa(int x){ if(fa[x]==x){ re

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

传送门 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-_

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): 2961    Accepted Submission(s): 1149 Problem Description TT and FF are ... friends. Uh... very very good friends -_____