Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1744    Accepted Submission(s): 660

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

Hint:
(PS: the 5th and 10th requests are incorrect)

 
Source

提议很简单,就是问:

a和b是否在同一个集合,不在就将他们加入到集合,在就对他们进行路径比较,如果给定的路程和实际路程不一样,那就表示冲突,记录下来

输出最后冲突的数量。(注意的是,b在a的前面,也就是b到根的路程要大于a到根的路程)

---------》带权值的路径......

代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define maxn 50005
  4.  
  5. int father[maxn];
  6. int dist[maxn];
  7. int n,m,re;
  8.  
  9. void init(){
  10. re=;
  11. for(int i=;i<=n;i++){
  12. father[i]=i;
  13. dist[i]=;
  14. }
  15. }
  16.  
  17. int fin(int x){
  18. if(x==father[x]) return x;
  19. int pre=father[x];
  20. father[x]=fin(father[x]);
  21. dist[x]+=dist[pre]; //统计每一个点到根点的距离
  22. return father[x];
  23. }
  24.  
  25. void Union(int x , int y , int val ){
  26. int a=fin(x);
  27. int b=fin(y);
  28. if(a==b){
  29. //注意方向-------顺时针
  30. if(dist[y]-dist[x]!=val)
  31. re++;
  32. }
  33. else{
  34. father[b]=a;
  35. dist[b]=dist[x]-dist[y]+val;
  36. }
  37. }
  38.  
  39. int main(){
  40. int a,b,c;
  41. while(scanf("%d%d",&n,&m)!=EOF){
  42. init();
  43. while(m--){
  44. scanf("%d%d%d",&a,&b,&c);
  45. Union(a,b,c);
  46. }
  47. printf("%d\n",re);
  48. }
  49. return ;
  50. }

  

hdu 3074 Zjnu Stadium (带权并查集)的更多相关文章

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

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

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

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

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

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

  4. HDU3047 Zjnu Stadium 带权并查集

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

  5. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  6. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  7. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  8. hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14

    题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...

  9. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  10. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. Windows安装Python包下载工具pip遇到的问题

    到Python的官网下载get-pip.py文件,然后按照说明进行安装. 在安装过程中,我遇到以下问题: cmd的codepage引起的编码错误,提示65001编码错误,通过chcp 936切换到默认 ...

  2. switch语句和for循环

    switch语句: 1. switch 后面小括号中表达式的值必须是整型或字符型 2. case后面的值可以是常量数值,如:1.日:也可以是一个常量表达式,如:2+2:但 不能是变量或带有变量的表达式 ...

  3. 草珊瑚的css基础

    首先要了解如下概念: viewport,窗口大小,containing block,block formatting context,inline formatting context,dirctio ...

  4. NS3 日志(Logging)、命令行参数、Tracing系统概述(转载)

    NS-3日志子系统的提供了各种查看仿真结果的渠道: 一.使用Logging Module 1 [预备知识]日志级别及其对应的宏 NS-3 提供了若干个日志级别来满足不同的 Debug 需求,每一级的日 ...

  5. iOS - Block 代码块

    1.Block Block 是一段预先准备好的代码,可以在需要的时候执行,可以当作参数传递.Block 可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.Block 是 C 语言的, ...

  6. Redis基础知识之—— 缓存应用场景

    转载原文:http://www.cnblogs.com/jinshengzhi/articles/5225718.html 一.MySql+Memcached架构的问题 Memcached采用客户端- ...

  7. Windows_CMD_临时环境变量

    1. 以 path 为例: 1.1.查看: set path 1.2.添加: set path=%path%;"要添加的路径" 附录:下面贴上一些常用的环境变量及作用 %ALLUS ...

  8. poj1673EXOCENTER OF A TRIANGLE

    链接 据说这题是垂心..数学太弱没有看出来,写了分朴实无华的代码.. 旋转三边得到图中的外顶点,然后连接三角形顶点求交点,交上WA..觉得没什么错误就去看了下discuss,发现都在说精度问题,果断开 ...

  9. D3.js 其他选择元素方法

    在上一节中,已经讲解了 select 和 selectAll,以及选择集的概念.本节具体讲解这两个函数的用法. 假设在 body 中有三个段落元素: <p>Apple</p> ...

  10. JDK中的Timer和TimerTask详解(zhuan)

    http://www.cnblogs.com/lingiu/p/3782813.html ************************************************** 目录结构 ...