Link:
http://acm.hdu.edu.cn/showproblem.php?pid=3047
Topic:
Problem Description
In 12th Zhejiang College Students Games 2007, there is 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 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, the Busoniya want to hold a large-scale theatrical performance into this stadium. There'll be N people go There numbered 1--n. Busoniya has Reserved several. To make it funny, he makes M requests for Seats:a B X, which means people numbered B must seat clockwise X distance From people numbered A. For EXAMPLE:A are in column 4th and X are 2, then B must in column 6th (6=4+2).
Now your task are to judge weather the request are 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 Incor Rect, 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 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 integers 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
Hint: (ps:the 5th and 10th requests are incorrect)
Source
2009 Multi-university Training Contest 14-host by ZJNU
The main effect of the topic:
There are n people sitting in the Zjnu stadium, and then giving m the distance between them, a B x, and the seat on behalf of B is more than a x. And then find out how many errors between this m relationship, the so-called error is the current relationship with the previous conflict.
Analysis and Summary:
The title has one sentence: we assume the number of rows were infinite. is to assume that the stadium is infinitely large, so do not consider the circle cycle.
With the right and check set, for and check the number of each tree, the root of the distance is 0, and then the root as a reference, each node of the weight of the distance between the roots.
When merging A,b, assume that a,b belong to different trees, then merge the two trees, the A tree into a B-tree, at this point to give a tree to the node root_a assignment, the key is to give root_a what value to attach. Because the weights of a and B points rank[a] and rank[b] are relative to the node distance, so the relative distance between the a,b can be rank[root_a] = rank[a]+x-rank[b]. Notice that at this point, for the original a tree, only with the new root_a and nodes of the weight, then the other node with the new in the search for the step inside the implementation.
Code:
#include <cstdio>
#include <cmath>
using namespace std;
#define N 50005
int f[n], rank[n], N, m;
void Init () {
for (int i=0; i<=n; ++i)
f[i]=i, rank[i]=0;
}
int find (int x) {
if (x==f[x]) return f[x];
int t=f[x];
F[X] = find (f[x]);
RANK[X] + = rank[t];
return f[x];
}
BOOL Union (int x,int y, int m) {
int a=find (x), B=find (y);
if (a==b) {
if (Rank[x]+m!=rank[y]) return
false;
return true;
}
F[B] = A;
RANK[B] = Rank[x]+m-rank[y];
return true;
}
int main () {
int a,b,x;
while (~SCANF ("%d%d", &n,&m)) {
init ();
int cnt=0;
for (int i=0; i<m; ++i) {
scanf ("%d%d%d", &a,&b,&x);
if (! Union (A, B, x)) {
++cnt
}}
printf ("%d\n", CNT);
}
return 0;
}