Zjnu Stadium(hdu3047带权并查集)
题意:一个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带权并查集)的更多相关文章
- HDU3047 Zjnu Stadium 【带权并查集】
HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- 【HDOJ3047】Zjnu Stadium(带权并查集)
题意:浙江省第十二届大学生运动会在浙江师范大学举行,为此在浙师大建造了一座能容纳近万人的新体育场. 观众席每一行构成一个圆形,每个圆形由300个座位组成,对300个座位按照顺时针编号1到300,且可以 ...
- hdu3047 Zjnu Stadium【带权并查集】
<题目链接> <转载于 >>> > 题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这 ...
- hdu 3074 Zjnu Stadium (带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3047 Zjnu Stadium(带权并查集,难想到)
M - Zjnu Stadium Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
随机推荐
- 常调用的Webservice接口 集合
1. 查询手机:http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 2. 查询IP:http://www.yoda ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- go中string和slice no-copy转换
在go里面,string和slice的互换是需要进行内存拷贝的,虽然在底层,它们都只是用 pointer + len来表示的一段内存. 通常,我们不会在意string和slice的转换带来的内存拷贝性 ...
- linux 系统监控系列之vmstat
vmstat的官方定义是:vmstat - Report virtual memory statistics,即虚拟内存的统计. 先来追根溯源: 什么是虚拟内存? 答:虚拟内存就是磁盘上虚拟出来可以当 ...
- htm跨域锚点定位
a.html <div class="title" id="aaaa">保险服务</div> b.html <div class= ...
- C# 让textbox 只能输入数字的方法
使用textBox控件的KeyPress事件 private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.Key ...
- JS滚轮事件(mousewheel/DOMMouseScroll)了解
已经没有了小学生时代过目不忘的记忆力了,很多自己折腾的东西.接触的东西,短短1年之后就全然不记得了.比方说,完全记不得获取元素与页面距离的方法(getBoundingClientRect),或者是不记 ...
- linux环境下编码的问题
查看linux支持的编码格式: locale -a查看文件的编码格式: :set fileencodinglinux下文本编码转换: iconv -f gbk -t utf8 main.cpp > ...
- SPOJ QTREE 系列解题报告
题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...
- debian安装缺少网卡驱动rtl_nic/rtl8168e-2.fw和bcm43xx-0.fw
最近拿了公司一台旧笔记本来装debian玩,刻好盘之后安装,发现网卡驱动都无法装上. 这是因为这些网卡驱动不是开源的,顽固的debian便将他们从驱动列表中移除了... 我从debian官方找到了这两 ...