HDU-3038How Many Answers Are Wrong权值并查集

How Many Answers Are Wrong

题意:输入一连串的区间和,问和前面的矛盾个数;

思路:我在做专题,知道是并查集,可是还是不知道怎么做,学了一下权值并查集和大佬的优秀思路,感觉回了一点;

    具体就是 在并查集的基础上,加上val【】数组用来记录区间和,而原来的fa【】数组表示的是这个数能到达的最左边的下标;

   下面是ac代码

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 200005;
int val[maxn],fa[maxn],n,m,ans=0;//fa记录下标所能到达最左端(),val表示前缀和

void init(){
    for(int i=0;i<=n;i++)
    {
        fa[i] = i;
        val[i]=0;
    }
}

int find (int x)
{
    if(fa[x]==x)return x;
    else
    {
        int fn = find(fa[x]);
        val[x] +=val[fa[x]];
        return fa[x] = fn;
    }
}
void uni(int a,int b,int s)
{
    int px = find(a);
    int py = find(b);
    if(px==py)                //只有左区间相同是才能比较
    {
        if(val[b]-val[a]!=s)ans++;
    }
    else
    {
        fa[py] = px;
        val[py] = val[a] + s - val[b];
    }
}
int main(){
    while(~scanf("%d%d",&n,&m))
    {
        init();
        ans = 0;
        for(int i=1;i<=m;i++)
        {
            int a,b,s;
            scanf("%d%d%d",&a,&b,&s);
            a--;
            uni(a,b,s);
        }
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ckxkexing/p/8461096.html

时间: 2024-10-07 21:14:20

HDU-3038How Many Answers Are Wrong权值并查集的相关文章

HDU 3038(权值并查集)

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21469    Accepted Submission(s): 7404 Problem Description TT and FF are ... friends. Uh... very very good friends -____

带权值并查集(转)

[POJ 1988] Cube Stacking 我们需要新增两种属性cnt[i]cnt[i]与s[i]s[i],分别表示ii之下的块数和ii所在堆的数量.在路径压缩时,cnt[i] += cnt[f[i]] ,另外在连接操作时,需要动态更新cnt[find(u)]和s[find(v)]的信息. 1 #include <iostream> 2 #define lson l,m,rt<<1 3 #define rson m+1,r,rt<<1|1 4 #define cl

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

hdu 1811 Rank of Tetris (拓扑排序+并查集)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4931 Accepted Submission(s): 1359 Problem Description自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他

HDU 1829 A Bug&#39;s Life (种类并查集)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19429    Accepted Submission(s): 6206 Problem Description Background Professor H

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

了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了. 现在回过头来看,又看了一遍. 题意—— 输入—— 给出多组测试数据. 每组数据第一行包含两个整数n, m.n表示共有1——n这么多个数,m表示m组提示. 接下来m行,每行包含三个整数a, b, val.表示从a到b这几个数的和为val. 这几组数有可能有冲突,问一共有多少组有冲突的数据. 输出—

hdu 3277 Marriage Match III【最大流+并查集+二分枚举】

Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1491    Accepted Submission(s): 440 Problem Description Presumably, you all have known the question of stable marriage match.