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 ...
随机推荐
- linux hash_map
在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...
- 截取NSString字符串
NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex: – substringWithRange: – substringToIndex: 具体的使用见下 ...
- C学习笔记 - 指针
指针与数组 ,,,,}; int *p; p = a; printf("*a = %d\n",*a); printf("*p = %d\n",*p); prin ...
- CSS网页元素居中
1.水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: text-align: center 适用元素:文字,链接,及其 ...
- css 梯形标签页
html 代码 略 css : nav > a{ position: relative; display: inline_block; padding: .3em 1em 0; } nav &g ...
- 写一个Windows上的守护进程(4)日志其余
写一个Windows上的守护进程(4)日志其余 这次把和日志相关的其他东西一并说了. 一.vaformat C++日志接口通常有两种形式:流输入形式,printf形式. 我采用printf形式,因为流 ...
- dropdownlist绑定和选中
最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家: 现在是后台数据绑定 protected void BindCarID() { ...
- node.js 环境搭建
一 官网下载安装包 : 1.http://www.nodejs.org/download/ 选择相应的包进行安装 2.安装express : npm install -g express -gener ...
- theos初探:ios越狱开发教程
开发环境搭建回顾 现在已经在windows上安装好了theos了.在上一篇中都已经讲了,开发环境主要部件就是: 1.theos,主要包含了使用make时的makefile模板文件.包含了各种库和框架的 ...
- win7 Oracle 11g安装及安装中遇到的问题
根据自己的系统从oracle官方下载安装包,官方地址:http://www.oracle.com/index.html win7的oracle 11g 安装包(2个): http://223.20.2 ...