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.
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
由已加入的两点的距离计算已经加入的点之间的距离,若新加入的两点间距离已经被算出来了,判断与已知的是不是相同的
#include<stdio.h>
#include<memory.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m;
int a,b;
ll len;
int ans;
int pre[200005];
ll sum[200005];//各个结点到根节点的距离
int find(int x){
if(pre[x]==x)return x;
else{
int r=find(pre[x]);
sum[x]+=sum[pre[x]];
pre[x]=r;
return pre[x];
}
}
void join(int x,int y,ll len){
int rx=find(x);
int ry=find(y);
if(rx!=ry){
pre[ry]=rx;
sum[ry]=len+sum[x]-sum[y];
}
else{
if(sum[y]-sum[x]!=len)ans++;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
ans=0;
for(int i=0;i<=n;i++){
pre[i]=i;
}
memset(sum,0,sizeof(sum));
while(m--){
scanf("%d%d%lld",&a,&b,&len);
join(a-1,b,len);
}
printf("%d\n",ans);
}
return 0;
}
刚开始没用
while(~scanf("%d%d",&n,&m)),只能输入一组测试样例一直过不了,贼坑啊!!!