题意:一个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. Memcached完全解剖–1. memcached基金会

    翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西非常实用,希望大家喜欢. 发表日:2008/7/2  作者:长野雅广(Masahiro Nagano)  原文链接:ht ...

  2. RMAN-FORMAT-CONFIGURE及动态性能表

    一.FORMAT参数在备份过程中,可指定format参数来自定义备份片段的命令规则,比如: RMAN> BACKUP DATABASE FORMAT 'D:\BACKUP\%U'; RMAN&g ...

  3. sketchup 导出 fbx文件 单位 错误

    最近在使用sketchup导出fbx文件到unity中使用时,发生了尺度单位上的错误.按照网上给出的标准教程,选定模型的单位为十进制-米.导出时选项选择'米',但是得到的fbx文件在unity中出现了 ...

  4. ES6 let和const命令

    一.let定义变量 { let a = 1;} console.log(a);只在let所在的代码块有效,console的结果是a is not defined,报错. 不存在var的变量提升,即使用 ...

  5. C++服务器设计(二):应用层I/O缓冲

    数据完整性讨论 我们已经选择了I/O复用模型作为系统底层I/O模型.但是我们并没有具体解决读写问题,即在我们的Reactor模式中,我们怎么进行读写操作,才能保证对于每个连接的发送或接收的数据是完整的 ...

  6. tomact虚拟目录,虚拟主机,http请求头,相应头

    tomact虚拟目录,虚拟主机,http请求头,相应头 07. 五 / J2EE / 没有评论   一.服务器,容器(软件)1.服务器:提供网络访问的程序2.容器:支持什么技术的服务器就叫做什么容器. ...

  7. Glide 加载图片背景变绿

    解决方案: Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

  8. C# 操作系统回收站

    主要目的:对系统回收站的文件进行操作. 首先添加引用,引入shell32.dll. /// <summary> /// 对回收站的文件进行还原.删除.剪切等操作 /// </summ ...

  9. php常用的header头

    <?php /** * php常用的header头设置... */ header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not ...

  10. 火星A+B..(不贴代码了)

    还是A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...