HDU - 3047-Zjnu Stadium

In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1–300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1–N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

Output
For every case:
Output R, represents the number of incorrect request.
Sample Input
10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100
Sample Output
2
Hint:
(PS: the 5th and 10th requests are incorrect)
题目大意
就是在一个圆形的桌子上坐着,现在给你n,m两个数分别表示n个人m组数据,每组数据包含x,y,s,分别表示x距离y为s,然后让你判断有几个数据是不合法的
解题思路
emmm mmp呀!你吃个饭那来这么多事呀?小明附体???这个题意识属于带权并查集,构图之类的都很容易但是如何确定关系呢?我怎么确定这两个点冲突了呢?emmmm这是这个题的关键步骤,其实我们可以开一个数组表示它距离自己的祖先节点的距离,以为一个队伍的节点只有一个祖先节点,所以以祖先节点为原点,这样的话不是一对的肯定不会冲突,冲突的话肯定是一队的而且位置一样,或者这两个节点的距离和上次的不一样那么就是不合格的,确定好思路了,那么我们就看代码吧

#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;

int fa[50500];
int sum[50500];//表示到节点的距离 
const int mod=300;//圆形坐着一旦距离大于300就是又一圈开始了 

int find(int x)
{
	if(x!=fa[x])
	{
		int s=fa[x];
		fa[x]=find(s);
		sum[x]=(sum[s]+sum[x])%mod;//因为可能出现相减为负值的情况 
	}							   //每次更新到祖先节点的距离 
	return fa[x];
}

bool join(int x,int y,int s)
{
	int xx=find(x);
	int yy=find(y);
	if(xx!=yy)
	{
		fa[yy]=xx;
		sum[yy]=(sum[x]-sum[y]+s+mod)%mod;//两个祖先节点不等就更新 新的祖先节点的位置 
	} 									// 这个表示的是y的祖先节点更新后距离xx的距离 
	else{								// 比如说1-2是40;3-4是20;那么现在有了1-3为10;把这三个节点连一起后 
		if(sum[y]!=(sum[x]+s)%mod) return true;// 4-2的距离就是40-20+10=30; 
	}// 说明两个节点的公共祖先是相同的,如果现在的距离和以前的不一样了,那说明也是不行的 
	return 0;
}

int main()
{
	int n,m,x,y,s;
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;i++)
		{
			fa[i]=i;
			sum[i]=0;
		}
		int ans=0;
		while(m--)
		{
			scanf("%d%d%d",&x,&y,&s);
			if(join(x,y,s))
			ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
 }