How Many Answers Are Wrong-- Weighted disjoint-set

Topic Link

Meaning of the questions:

There are n times asked to give a sum of the interval b, the sum Q n times given there couple of times contradictory and previously given.

Example:

[1,5] = 10, [6.10] = 10, [1, 10] = 30, which is obviously a third conflict with the previous two.

answer:

sum [i] represents 1 to i and, if present [i, a] and [i, b] is determined as long as [b, i] - [a-1, i] is equal to c

So with disjoint-set to maintain a common point that is the root node

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int f[maxn];
int n,m;
int sum[maxn];///根节点到 i 的距离
int Find(int x)
{
    if(x==f[x])return x;
    int root=Find(f[x]);
    sum[x]+=sum[f[x]];
    return f[x]=root;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<=n;i++)f[i]=i,sum[i]=0;
        int ans=0;
        while(m--)
        {
            int a,b,s;
            scanf("%d%d%d",&a,&b,&s);
            a--;
            int fx=Find(a);
            int fy=Find(b);
            if(fx!=fy)
            {
                f[fy]=fx;
                sum[fy]=sum[a]+s-sum[b];///   sum[b]+sum[fy]-sum[a]=s
            }
            else
            {
                if(sum[b]-sum[a]!=s)ans++;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
} 
View Code

 

Guess you like

Origin www.cnblogs.com/j666/p/11610131.html