1. 程式人生 > >How Many Answers Are Wrong(帶權並查集)

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;
}