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

题意:告诉多组你A,B之间的和为S,求有没有相互冲突的答案,求出个数。

解题思路:

1,第一次搞这种类型的并查集,有点像垒方块那个并查集, 思路差不多,不完全相同。

2,因为区间是闭区间,所以或出现A = B的情况,所以搞成一闭一开的区间,哪边闭,哪边开都行。

3,设sum[i]表示的是i到i的根节点的距离,因为一开始自己就是自己的根节点,所以sum数组初始化为零。

4,(1)判断输入进来的A和B,如果A和B不在同一个集合,那么合并就可以了,我是将A的根节点作为根节点,将B的根节点连到A的根节点上。这样A,B就在统一的集合啦,完成A与B的合并。但是还要跟新sum数组的值,更新前B的根节点的sum值为零(因为他自己就是原来的根节点,自己到自己的距离为零),更新后它的值应变为它到它新根节点的距离,它到新节点的距离应该是A到A根节点的距离(也就是A新根节点的距离, 还是更新前那个没变) 减去B到它的距离(B到更新前B的根节点的距离)在加上S。因为A和B之间的距离为S。假设输入是A B 10,已经存在A到A根是50, B到B根是100,更新后应该是这样的


上图别看了不好懂,盗的这张图比较好明白。就是向量关系嘛!


上图可以看出B根到A根的距离是怎么算的,也可以看出A与B的距离是sum[b] - sum[a]

4.(2)如果在同一个集合,就判断sum[b] - sum[a]是不是等于S,若不是就说明存在冲突。

5,没了。

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


#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 200005
const int INF = 0x3f3f3f3f;

int parent[MAX_N];
int sum[MAX_N];

void init(int n)
{
    for(int i = 0 ; i < n + 5 ; i++)
    {
        parent[i] = i;
        sum[i] = 0;
    }
}

int find(int a)
{
    if(parent[a] != a)
    {
        int t = parent[a];
        parent[a] = find(parent[a]);
        sum[a] += sum[t];
    }
    return parent[a];
}

void unite(int a, int b, int s)
{
    int fa = find(a);
    int fb = find(b);
    if(fa == fb) return;
    parent[fb] = fa;
    sum[fb] = sum[a] - sum[b] + s;
}

int same(int a, int b)
{
    return find(a) == find(b);
}

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        init(n);
        int ans = 0;
        for(int i = 0 ; i < m ; i++)
        {
            int a = read();
            int b = read();
            int s = read();
            a--;
            if(!same(a, b))
            {
                unite(a, b, s);
            }
            else if(sum[a] + s != sum[b])
            {
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值