HDU3047 Zjnu Stadium


Problem 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 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)


就是有一个环,给出多组关系满足a=b+x,判断不满足的有多少组

然后带权并查集维护一下,主义在合并两个并查集的时候差量的计算


 #include<bits/stdc++.h>
using namespace std;
#define N 50010
int fa[N],dis[N];
int n,m;
int Find(int x){
if(fa[x]==x)return x;
int f=Find(fa[x]);
dis[x]+=dis[fa[x]];
return fa[x]=f;
}
void init(){for(int i=;i<=n;i++)fa[i]=i,dis[i]=;}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
int res=;
while(m--){
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
int f_a=Find(a);
int f_b=Find(b);
if(f_a==f_b){
if(dis[a]+x!=dis[b])res++;
continue;
}
dis[f_b]=dis[a]-dis[b]+x;
fa[f_b]=f_a;
}
printf("%d\n",res);
}
return ;
}

HDU3047 Zjnu Stadium 【带权并查集】的更多相关文章

  1. HDU3047 Zjnu Stadium 带权并查集

    转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...

  2. Hdu 2047 Zjnu Stadium(带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  4. HDU 3047 Zjnu Stadium(带权并查集)

    题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...

  5. Zjnu Stadium(hdu3047带权并查集)

    题意:一个300列的无限行的循环场地,a b d代表a,b顺时针相距d的距离,现在给你一些距离,判断是否有冲突,如果有冲突计算冲突的次数 思路:带权并查集 a,b的距离等于b到根节点的距离 - a到根 ...

  6. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  7. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  8. 【HDOJ3047】Zjnu Stadium(带权并查集)

    题意:浙江省第十二届大学生运动会在浙江师范大学举行,为此在浙师大建造了一座能容纳近万人的新体育场. 观众席每一行构成一个圆形,每个圆形由300个座位组成,对300个座位按照顺时针编号1到300,且可以 ...

  9. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

随机推荐

  1. java将配置信息写在数据库(利用反射)

    Demo出处: 1. package com.fpx.pcs.prealert.process.xml.service.impl; public class CainiaoPushMessageSer ...

  2. ExtJs4.2.1中的Ext.grid.GridPanel选择行回车事件

    网上大多说的是“rowdblclick” 其实是“itemdblclick” 这个东西坑了我一上午.

  3. js来监控复制粘贴

    平时我们在复制网页上面代码到控制台调试时,有时会出现复制过来的代码后面加上了一下描述信息(作者.版权等信息),每次需要删除才能运行,所以今天看看怎么能保证我们粘贴的代码不携带这些信息呢? (funct ...

  4. 基于事件的 JavaScript 编程:异步与同

    JavaScript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程.然而,对于初学者来说,书写异步代码可能会比较困难.而在这篇文章里,我将会 ...

  5. Entity Framework教程及文章传送门

    Entity Framework视频教程http://www.iqiyi.com/playlist296844502.html Model-Code First做法講解與教學 (ASP.net MVC ...

  6. 【Demo】jQuery 设置简单的分页

    功能描述: (1)设定起始页位置和每页的大小: (2)绑定自定义的分页事件,在事件中隐藏当前页不需要显示的行: (3)为表格添加页链接: (4)绑定链接的单击事件,在事件中触发表格分页事件. 效果图: ...

  7. 键盘对应数字-keycode值大全(转)

    event.keycode值大全   keycode 8 = BackSpace BackSpace    keycode 9 = Tab Tab    keycode 12 = Clear    k ...

  8. JAVA执行带参数的SQL语句

    转自 http://www.cnblogs.com/raymond19840709/archive/2008/05/12/1192948.html

  9. IOS-CocoaPods制作篇

    作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/20067595 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或关 ...

  10. MD5加密(java和c#)

    java代码 public static String md5(String str) { try { MessageDigest md = MessageDigest.getInstance(&qu ...