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

题目:
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.

BoringBoringa 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)

题解
考虑前缀和,进行带权并查集合并;
   如果端点不在一个集合,合并;否则判断即可,同食物链那题;

代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#define exp 1e-9
#define PI acos(-1.0)
#define mod 100000
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
int pre[200500];
int val[200010];

/*int finds(int k)
{
    if(pre[k]==k)return k;
    return pre[k]=finds(pre[k]);
}
void join(int x,int y)
{

    int fx=finds(x),fy=finds(y);
    if(fx!=fy)  pre[fx ]=fy;
}*/

int finds(int k)
{
    if(pre[k]==k)return k;
    int tmp=finds(pre[k]);
        val[k]+=val[pre[k]];
        //找到端点
    return pre[k]=tmp;
}

int main()
{
    int m,n;
    while(~scanf("%d%d",&n,&m))
    {
        int u,v,w;
        for(int i=1;i<=n;i++)
        {
            pre[i]=i;
            val[i]=0;
        }//初始化
        int ans=0;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            u--; //前缀和 所以u-1
            int t1=finds(u);
            int t2=finds(v);
            if(t1!=t2) //端点不同 合并
            {
                pre[t2]=t1;
                val[t2]=val[u]-val[v]+w;
            }
            else //端点相同 判断
            {
                if(val[v]-val[u]!=w)
                    ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41243063/article/details/83065235