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 ...
随机推荐
- Eclipse安装Vim——viPlugin插件
1.下载viPlugin: http://www.viplugin.com/files/viPlugin_2.14.0.zip 2.安装 解压后有两个文件夹: features 和 plugins 把 ...
- String和StringBuilder 的使用区别
String 类有不可变性,每次执行操作时都会创建一个新的String对像,需要对该对象分配新的空间. StringBuilder 解决了对字符串重复修改过程中创建大量对象的问题.初始化一个Strin ...
- android——使用自带录屏工具进行屏幕录像
在做开源项目的时候,想传一个gif效果图上去.但是,要有连贯的动画效果.所以,就想到先录制视频,然后视频转gif.但是,用第三录屏软件总是不完美. 那么,怎么办呢? android4.4 提供了自带录 ...
- ASP.NET中多个相同name的控件在后台正确取值
有兽, 页面上可能有多个相同name的Html表单控件, 一般在后台使用Request.Form[“name”]取值,并用‘,’分隔. 但是当值中包含逗号时, 取值就会出现异常, ...
- UML学习-时序图
时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的.顺序图中显示的是参与交互的对象及其对象之间消息交互的顺序.时序图中包括的建模元素主要有:对象(Actor) ...
- Android HttpClient框架get和post方式提交数据(非原创)
1.fragment_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- PHP学习笔记十八【构造函数】
<?php class Person{ public $name; public $age; //定义构造函数 function 空格__construct 构造方法没有返回值,对象自动调用 p ...
- 网络编程之TCP
知识补充:源IP地址和目的IP地址以及源端口号和目的端口号的组合称为套接字.其用于标识客户端请求的服务器和服务. TCP编程的实现步骤:服务器端:1.通过ServletSocket创建绑定到指定客户端 ...
- (转)Eclipse Shortcuts
原文地址: http://javapapers.com/core-java/eclipse-shortcuts/ Editors are an integral part of a programme ...
- (转)ubuntu下如何查看和设置分辨率
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5681159.html 原网址: http://www.2cto.com/os/201303/19397 ...