HDU3047 Zjnu Stadium

来源:互联网 发布:淘宝如何发链接给别人 编辑:程序博客网 时间:2024/06/04 07:33

Description

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 101 2 1503 4 2001 5 2702 6 2006 5 804 7 1508 9 1004 8 501 7 1009 2 100

Sample Output

2



最近一周好好把并查集和最短路走的尽量远一点    再然后就得往RMQ  差分约束   网络流方向走了

扯回来扯回来     刚写的一道带权并查集问题  (因为其实我的并查集基础很薄

并且再之前已经好几次碰到类似的问题了    就是说   带权并查集再合并两棵树的时候     他们各个节点是如何处理的    一直以来我都想不到好方法   今天刚写完这题   我才算明白算法的强大与互通性         看完其他人的博客   我马上意识到    —— 是在线段树里的   延迟更新     也就是说我先做个标记   当我访问的时候  我需要的时候再更新

而这里也是一个道理     我只对被合并的树  的根节点进行更新   当要访问这棵树的叶子节点    我再对其逐个节点递归更新   一直到访问节点

这个思路的博客其实是有很多的   但我实在是忍不住发一个    毕竟对我来说又是一大冲击

ac code

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <cstdlib>#include <algorithm>using namespace std;const int maxn=50005;int root[maxn],value[maxn];int n,m;void init(){    for(int i=0;i<=n;i++)        root[i]=i,value[i]=0;}int fin(int x){    if(root[x]==x) return x;    int kao=root[x];    root[x]=fin(root[x]);    value[x]+=value[kao];//厉害    return root[x];}bool Union(int a,int b,int x){    int ra=fin(a);    int rb=fin(b);    if(ra==rb)    {        if(value[a]+x!=value[b]) return false;        return true;    }    root[rb]=ra;    value[rb]=value[a]+x-value[b];    return true;}int main(){    int a,b,x;    while(~scanf("%d%d",&n,&m))    {        init();        int cnt=0;        while(m--)        {            scanf("%d %d %d",&a,&b,&x);            if(!Union(a,b,x))                cnt++;        }        printf("%d\n",cnt);    }    return 0;}











0 0
原创粉丝点击