HDU 3038 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.
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
Sample Output
1
题目大意:
第一行输入 n,m n表示有多少个点,m表示有多少个边
下面的m行,输入边的信息,即两点之间的距离;
但是,边的信息可能有冲突,就是说m行里,可能限制条件有冲突。
输出 有几个有冲突的限制条件。
题目分析:
1.使用并查集,不过是加权的并查集
2.对每个点,都找他的父节点,并每次更新到父节点的“距离”。在find代码里实现
3.这里是一个序列,下面有一个减一的操作,就相当于把[3,9],变为(3,9],因为他是整数,所以减一后相当于是一个空的点,可以画数轴理解一下。
4.路径合并时…emmm.那个式子举两个例子就可以看出来了。
5.这题需要多样例输入,虽然题目里没说…开始被坑了。
用向量理解,可比较方便推出上述关系式。
AC代码:
#include<stdio.h>
#include<queue>
#include<vector>
#include<iostream>
#include<string.h>
const int mod=300;
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
int t,u,v,w,n,m,fa[maxn],W[maxn];//fa[]存放父节点,W[]存放到父节点的距离
using namespace std;
int find(int x){
if(x!=fa[x])
{
int s=fa[x];
fa[x]=find(s);
W[x]=W[s]+W[x]; //更新到父节点的距离。
}
return fa[x];
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=1;i<=n;i++) {
fa[i]=i;W[i]=0; //开始,每个的父节点都是自己,然后自己到自己的距离是0;
}
int ans=0;
while(m--){
scanf("%d%d%d",&u,&v,&w);
u-=1;//这里注意,要减一,有开区间的感觉。
//就像是
int a=find(u);
int b=find(v);//找到u,v的父节点
if(a!=b)//他俩父节点不同时,要想把他俩连起来,就要把a和b连起来
{
fa[b]=a;
W[b]=W[u]-W[v]+w;
}
else//如果根相同,直接判断是否有冲突。
{
if(W[u]+w!=W[v])
ans++;
}
}
printf("%d\n",ans);
}
}