Zjnu Stadium

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

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

  1.  
  2. 10 10
  3. 1 2 150
  4. 3 4 200
  5. 1 5 270
  6. 2 6 200
  7. 6 5 80
  8. 4 7 150
  9. 8 9 100
  10. 4 8 50
  11. 1 7 100
  12. 9 2 100

Sample Output

  1.  
  2. 2
  3.  
  4.  
  5.  
  6. Hint
  7.  
  8. Hint:
  9. PS the 5th and 10th requests are incorrect

Source

2009 Multi-University Training Contest 14 - Host by ZJNU

 

 

(1)弄清题意,找出出现冲突的位置,判断冲突很简单: 就是当两个人在同一行坐同时, 他们到根节点的距离差值正好是他们之间的距离差值。如果和测试数据不同,此时就出现了冲突了。

(2)关键有两个地方,这也是并查集题目的难点,1、路径压缩,2、合并时候求被合并根节点到新根节点的距离。

路径压缩在递归过程中计算每个节点到根的距离: dist[x] += dist[fx];

合并过程 fy合并到fx

p[fy]=fx;

dist[fy]=-dist[y]+d+dist[x];

使用的是数学中向量计算的原理如图

 

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<string>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=50000+5;
  8. int p[maxn], dist[maxn];//dist存储的是相对于父节点的距离
  9.  
  10. void make_set()
  11. {
  12. memset(p, -1, sizeof(p));
  13. memset(dist, 0, sizeof(dist));
  14. }
  15.  
  16. int find_set(int x)
  17. {
  18. if(p[x]==-1) return x;
  19. int fx=p[x];
  20. p[x]=find_set(p[x]);
  21. dist[x]+=dist[fx];
  22. return p[x];
  23. }
  24.  
  25. void union_set(int x, int y, int d)
  26. {
  27. int fx=find_set(x), fy=find_set(y);
  28. if(fx==fy) return;
  29. p[fy]=fx;
  30. dist[fy]=-dist[y]+d+dist[x];
  31. }
  32.  
  33. int main()
  34. {
  35. int n, m;
  36. int a, b, x;
  37. int ans;
  38. while(scanf("%d%d", &n, &m)!=EOF) {
  39. ans=0;
  40. make_set();
  41. while(m--)
  42. {
  43. scanf("%d%d%d", &a, &b, &x);
  44. if(find_set(a)==find_set(b))
  45. {
  46. if(dist[b]-dist[a]!=x)
  47. {
  48. //printf("a=%d b=%d x=%d, dist[b]-dist[a]=%d\n", a, b, x, dist[b]-dist[a]);
  49. ans++;
  50. }
  51. }
  52. else union_set(a, b,x);
  53. }
  54. printf("%d\n", ans);
  55. }
  56.  
  57. return 0;
  58. }

hdu 3047 Zjnu Stadium 并查集高级应用的更多相关文章

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

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

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

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

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

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

  4. HDU 3047 Zjnu Stadium(带权并查集,难想到)

    M - Zjnu Stadium Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. hdu 3047 Zjnu Stadium(并查集)

    题意: 300个座位构成一个圈. 有N个人要入座. 共有M个说明 :A B X ,代表B坐在A顺时针方向第X个座位上.如果这个说明和之前的起冲突,则它是无效的. 问总共有多少个无效的. 思路: 并查集 ...

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

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

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

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

  8. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  9. hdu 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 带权并差集 #include <cstdio> #include <cstring> ...

随机推荐

  1. 参数数组(params)的用法

    使用参数数组的注意事项: 1. 只能在一维数组上使用params关键字. 2. 不能重载一个只基于params关键字的方法.params关键字不构成方法的签名的一部分. 如: //编译时错误:重复访问 ...

  2. myeclipse中配置schemaLocation路径,实现xml文件自动提示

    在开发中,XML的xsi:schemaLocation路径都是指向网络,但是这个网络地址有时候很不给力导致工程检验XML格式缓慢.所以有必要再myeclipse中配置本地xsd文件路径,以免每次校验都 ...

  3. C# Winform 实现自定义半透明遮罩层介绍

    在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法: 效果图如下,正常时: 显示遮罩层时: 自定义遮罩层控件的源码如下: View Row Code 1 usi ...

  4. QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout

    http://blog.csdn.net/zhuyingqingfen/article/details/6562246 如题,出现这个的原因是,如果你的窗口继承的是QMainwindow,需要设置 s ...

  5. ArrayList 和 HashMap 的默认大小是多数?

    ArrayList 和 HashMap 的默认大小是多数? 在 Java 7 中,ArrayList 的默认大小是 10 个元素,HashMap 的默认大小是16个元素(必须是2的幂).这就是 Jav ...

  6. oracle 创建表空间及oracle 11g表空间之最大最小

    /*分为四步 *//*第1步:创建临时表空间 */create temporary tablespace emaoyi_temp tempfile 'D:\app\Administrator\prod ...

  7. ITDB系统搭建及实时备份

    ITDB系统搭建及实时备份 ITDB简介 ITDB一款来自希腊的开源IT资产管理系统,它是基于Web的IT资产信息管理系统.对于那些IT设备较多而又缺少管理IT资产信息工具的公司,ITDB是一个不错的 ...

  8. jquery远程引用地址大全

    jquery官方的引用地址,如图: <script typet="text/javascript" src="http://code.jquery.com/jque ...

  9. "/usr/local/openresty/nginx/html/index.html" is forbidden (13: Permission denied), client: 10.0.4.118, server: localhost, request: "GET / HTTP/1.1"

    openrestry 安装之后 报"/usr/local/openresty/nginx/html/index.html" is forbidden (13: Permission ...

  10. YARN机制

    YARN是资源管理调度的机制,之前一直以来和MapReduce机制合在一起,之后才分开.正是因为YARN机制单独独立出来,才使得Hadoop框架更加具有普适性.MapReduce可以处理海量离线数据, ...