题意:一个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))

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int flag = 0;
struct bian
{
int father;
int dis;
}p[50050]; void Make_set(int n)
{
int i;
for(i = 1; i <= n; i++)
{
p[i].father = i;
p[i].dis = 0;
}
} int Find_set(int x)
{
if(x != p[x].father)
{
int temp = p[x].father;
p[x].father = Find_set(p[x].father);
p[x].dis = ( p[x].dis + p[temp].dis) % 300;
}
return p[x].father;
} void Union(int a,int b,int d)
{
int x = Find_set(a);
int y = Find_set(b);
if(x == y)
{
if(( (p[b].dis-p[a].dis+300) % 300 ) != d)
{
flag = 1;
return ;
}
}
else
{
p[x].father = y;
p[x].dis = (p[b].dis+300-d-p[a].dis) % 300;
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) != EOF)
{
int i,sum = 0;
Make_set(n);
for(i = 0; i < m; i++)
{
int a,b,d;
flag = 0;
scanf("%d%d%d",&a,&b,&d);
Union(a,b,d);
if(flag)
sum++;
}
printf("%d\n",sum);
}
return 0;
}

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下git使用记录1 git 提交

    linux下git使用记录1   浏览:985 发布日期:2013/08/08 分类:技术分享 在使用github的时候,不可避免的接触到了git,用他来更新项目,做版本控制.这里特别把常用的命令记录 ...

  2. 22. 使用 awk / grep / head / tail 命令进行文本 / 日志分析 (/home/D/acc.log)

    一.awk     # 统计 a-read-file 接口 中,接口耗时 超过 0.007 秒的有多少个请求     D@Demon ~]$ awk '$7>0.007' acc.log | w ...

  3. 面试前的准备---C#知识点回顾----03

    经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒 ...

  4. SQLserver 连接+开窗函数+视图+事务

    今天学习SQLserver 连接以及开窗函数..加油! 1.复习:查询(检索)->筛选列->筛选行:distinct top where 运算符与关键字:比较运算符,逻辑运算符,betwe ...

  5. PHP学习笔记二十五【类的继承】

    <?php //定义父类 class Stu{ public $name; protected $age; protected $grade; private $address;//私有变量不会 ...

  6. iOS 面试基础题

    1.UIWindow和UIView和 CALayer 的联系和区别? 答:UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应 ...

  7. 通过XSLT转换XML

    Hello,every body.又与大家见面了,哈哈.今天我与大家分享一下XSLT,XSL,XML,XPath.因为项目中有些功能用到了XSLT.XML等技术.所以今天好好研究了一下这几个方面的技术 ...

  8. (转) 学习C++ -> 向量(vector)

      vector是向量类型,它是一种对象实体,具有值,所以可以看作是变量. 它可以容纳许多其他类型的相同实体,如若干个整数,所以称其为容器.   vector类与一般的Array类的区别在于:   1 ...

  9. hdu3599 War(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud War Time Limit: 2000/1000 MS (Java/Others ...

  10. Linux 入门命令

    本文系转载:http://www.cnblogs.com/wwj9413/archive/2012/03/15/2638638.html#2929949 1.Linux进入与退出系统 进入Linux系 ...