您的位置:首页 > 其它

HDU 3038 How Many Answers Are Wrong (带权并查集+区间判断)

2014-05-03 22:04 405 查看
题意:给你长度为n的区间,m个询问:a,b,c,问这m个问题有多少个是错误的(矛盾)。

10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1


由6->6=1, 4->6=41 知4->5=40;

同理 由1->10=100,7->10=28 知1->7=72;

又由1->3=32,4-6=41 知1->7=73,与上面矛盾;

所以答案为1;

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define maxn 100001
#define PI acos(-1.0)

int father[2*maxn];
int sum[2*maxn];
int find(int x)
{
if(x==father[x])
return x;
int t=father[x];
father[x]=find(father[x]);
sum[x]+=sum[t];
return father[x];
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int a,b,c;
for(int i=0;i<=n;i++)
{
father[i]=i;
sum[i]=0;
}
int ans=0;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
int x,y;
a--;
x=find(a);
y=find(b);
if(x==y)
{
if(sum[a]-sum[b]!=c)
ans++;
}
else if(x>y)
{
father[y]=x;
sum[y]=sum[a]-sum[b]-c;
}
else
{
father[x]=y;
sum[x]=sum[b]-sum[a]+c;
}

}
printf("%d\n",ans);
}
return 0;
}
/*
10 5 1 10 100 7 10 28 1 3 32 4 6 41 6 6 1
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: