题意:一个300列的无限行的循环场地,a b d代表a,b顺时针相距d的距离,现在给你一些距离,判断是否有冲突,如果有冲突计算冲突的次数

思路:带权并查集

a,b的距离等于b到根节点的距离 - a到根节点的距离

1.当a,b在同一集合的时候就用b到根节点的距离 - a到根节点的距离和当前输入的距离进行对比,看是否满足条件

2.当a,b不在同一集合的时候合并两个节点,更新距离

向量法,方向一定要搞清楚,父亲指向儿子

如果x的父亲rootx ,y的父亲是rooty

rootx --> x, rooty --> y合并的方向是rootx的父亲是rooty 即rooty --> rootx

x -->  y的距离是d

rooty --> rootx = rootx --> x + x --> y + y --> rooty = rootx --> x +(-( y --> x ) ) + (-( rooty -->y))

合并的方向不同式子也不同

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int flag = 0;
  7. struct bian
  8. {
  9. int father;
  10. int dis;
  11. }p[50050];
  12.  
  13. void Make_set(int n)
  14. {
  15. int i;
  16. for(i = 1; i <= n; i++)
  17. {
  18. p[i].father = i;
  19. p[i].dis = 0;
  20. }
  21. }
  22.  
  23. int Find_set(int x)
  24. {
  25. if(x != p[x].father)
  26. {
  27. int temp = p[x].father;
  28. p[x].father = Find_set(p[x].father);
  29. p[x].dis = ( p[x].dis + p[temp].dis) % 300;
  30. }
  31. return p[x].father;
  32. }
  33.  
  34. void Union(int a,int b,int d)
  35. {
  36. int x = Find_set(a);
  37. int y = Find_set(b);
  38. if(x == y)
  39. {
  40. if(( (p[b].dis-p[a].dis+300) % 300 ) != d)
  41. {
  42. flag = 1;
  43. return ;
  44. }
  45. }
  46. else
  47. {
  48. p[x].father = y;
  49. p[x].dis = (p[b].dis+300-d-p[a].dis) % 300;
  50. }
  51. }
  52. int main()
  53. {
  54. int n,m;
  55. while(scanf("%d%d",&n,&m) != EOF)
  56. {
  57. int i,sum = 0;
  58. Make_set(n);
  59. for(i = 0; i < m; i++)
  60. {
  61. int a,b,d;
  62. flag = 0;
  63. scanf("%d%d%d",&a,&b,&d);
  64. Union(a,b,d);
  65. if(flag)
  66. sum++;
  67. }
  68. printf("%d\n",sum);
  69. }
  70. return 0;
  71. }

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

  1. HDU3047 Zjnu Stadium 【带权并查集】

    HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...

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

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

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

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

  4. hdu3047 Zjnu Stadium【带权并查集】

    <题目链接> <转载于 >>> > 题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这 ...

  5. hdu 3074 Zjnu Stadium (带权并查集)

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

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

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

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

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

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

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

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

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

随机推荐

  1. linux hash_map

    在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...

  2. 截取NSString字符串

    NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex: – substringWithRange: – substringToIndex: 具体的使用见下 ...

  3. C学习笔记 - 指针

    指针与数组 ,,,,}; int *p; p = a; printf("*a = %d\n",*a); printf("*p = %d\n",*p); prin ...

  4. CSS网页元素居中

    1.水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: text-align: center 适用元素:文字,链接,及其 ...

  5. css 梯形标签页

    html 代码 略 css : nav > a{ position: relative; display: inline_block; padding: .3em 1em 0; } nav &g ...

  6. 写一个Windows上的守护进程(4)日志其余

    写一个Windows上的守护进程(4)日志其余 这次把和日志相关的其他东西一并说了. 一.vaformat C++日志接口通常有两种形式:流输入形式,printf形式. 我采用printf形式,因为流 ...

  7. dropdownlist绑定和选中

    最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家: 现在是后台数据绑定 protected void BindCarID() { ...

  8. node.js 环境搭建

    一 官网下载安装包 : 1.http://www.nodejs.org/download/ 选择相应的包进行安装 2.安装express : npm install -g express -gener ...

  9. theos初探:ios越狱开发教程

    开发环境搭建回顾 现在已经在windows上安装好了theos了.在上一篇中都已经讲了,开发环境主要部件就是: 1.theos,主要包含了使用make时的makefile模板文件.包含了各种库和框架的 ...

  10. win7 Oracle 11g安装及安装中遇到的问题

    根据自己的系统从oracle官方下载安装包,官方地址:http://www.oracle.com/index.html win7的oracle 11g 安装包(2个): http://223.20.2 ...