解题思路转自:

http://blog.csdn.net/azheng51714/article/details/8500459

http://blog.csdn.net/acresume/article/details/7461238

有一个体育馆,座位呈环状,想象下,貌似体育馆都是这样的,每一列有300个座位,按逆时钟方向编号为1~300,假设行数无穷大。

某一天,有N个人(编号为1~N)来到这个体育馆看一场赛事,主办方提出了M个要求,要求的格式是"A B X",表示的是,假设A坐在编号为i的列,则B必须坐在编号为(i+x)%300的列上,模300是因为座位呈环状。。这些要求里有一些是错误的,只有和前面的要求产生冲突时才算错误,其它都是正确的。程序要输出错误的要求个数。

读入一个要求,看看A和B的差是否确定或是否可以根据之前的一些要求推出,即是否在同一个集合里。如果不是,则确定A和B的差,即把A所在集合和B所在集合合并为同一个集合并更新集合中每个结点相对于新根的差(查找时在路径压缩过程中分别更新两个集合的非根结点相对于根结点的差,合并时更新旧根相对于新根的差);如果A和B已经在同一个集合里,那么判断(B-A+300)%300是否等于X,如果不相等,则该要求是错误的。另外,要注意取模操作的一些细节。

分析:这是一道比较简单地并查集题目。

(1)弄清题意,找出出现冲突的位置,判断冲突很简单就是当两个人在同一行坐,同时他们到根节点的距离差值正好是他们之间的差值,此时就出现了冲突了。

(2)关键有两个地方,这也是并查集题目的难点,就是压缩集合,和求节点到根的距离。这里压缩集合就很简单了,一个通用的递归。求到跟的距离

dist[a]+= dist[tem];
dist[rb]=dist[a]+x-dist[b];

注意这两行代码,这是核心代码,首先第一行是求出节点a到根的距离。第二行代码使用的是数学中向量计算的原理如图

AC代码:


 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
int n, m, root[maxn], dis[maxn];
void init(){
for(int i = ; i <= n; i++)
root[i] = i, dis[i] = ;
}
int find(int x){
if(root[x] == x) return x;
int t = root[x];
root[x] = find(root[x]);
dis[x] += dis[t];//求出节点a到根的距离
return root[x];
}
void Union(int u,int v,int x){
int ru = find(u);
int rv = find(v);
root[rv] = ru;
dis[rv] = dis[u] + x - dis[v];//使用的是数学中向量计算
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
int i, j, u, v, dist, ans = , cnt = ;
while(m--){
scanf("%d%d%d",&u,&v,&dist);
if(find(u) != find(v)) Union(u,v,dist);
else if(dis[u] + dist != dis[v]) ans++;
}
printf("%d\n",ans);
}
return ;
}

Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1221    Accepted Submission(s): 472
Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want
to hold a large-scale theatrical performance in this stadium. There will
be N people go there numbered 1--N. Busoniya has Reserved several
seats. To make it funny, he makes M requests for these seats: A B X,
which means people numbered B must seat clockwise X distance from
people numbered A. For example: A is in column 4th and X is 2, then B
must in column 6th (6=4+2).
Now your task is to judge weather the
request is correct or not. The rule of your judgement is easy: when a
new request has conflicts against the foregoing ones then we define it
as incorrect, otherwise it is correct. Please find out all the
incorrect requests and count them as R.
 

Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

 

Output
For every case:
Output R, represents the number of incorrect request.
 

Sample Input
10 10 1 2 150 3 4 200 1 5 270 2 6 200 6 5 80 4 7 150 8 9 100 4 8 50 1 7 100 9 2 100
 

Sample Output
2

Hint

Hint: (PS: the 5th and 10th requests are incorrect)

HDOJ 3047 带权并查集的更多相关文章

  1. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  2. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  3. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  4. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  5. 带权并查集 HDU - 3047

    题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...

  6. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  7. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  8. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  9. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

随机推荐

  1. 在mysql中创建存储过程出现1307错误,解决方法

    需要删除mysql数据库下proc表 在重新创建 CREATE TABLE `proc` ( `db` char(64) character set utf8 collate utf8_bin NOT ...

  2. 带参数的存储过程和标量Function

    在SQL Server中,我们通常会使用NEWID(),GETDATE(),等一些数据库函数,这些函数是很有帮助的,然后数据库也能够让我们自己写函数,即Function,下面简单说说Function的 ...

  3. 基于maven插件的缓存控制插件

    asset-cache-control github源码及下载地址: https://github.com/StruggleBird/asset-cache-control 基于maven插件的缓存控 ...

  4. ansible笔记

    ansible 资料 ansible 配置 ansible inventory配置文件 ansible模块 http://www.cnblogs.com/iois/p/6216936.html ans ...

  5. sql学习之基础(MySql)

    --#创建一个数据库 create database excise01; --#查看所有数据库 show databases; --#查看刚建的数据库 show create database exc ...

  6. eclipse 查看快捷键

    无意中发现,ctrl+shift+L 能打开快捷键窗口

  7. UnixShell编程(第三版) 二章

    这本书写的真的很好,让人欲罢不能的读下去. 1,可以简单的将命令看做类似函数的东西,而后面跟的是所传的参数. echo Hello Word > Hi   将字符串hello Wrod 写入文件 ...

  8. C陷阱与缺陷(一)

    第一章 词法陷阱 术语“符号”指的是程序的一个基本组成单元,其作用相当于一个句子中的单词.编译器中负责将程序分解为一个一个符号的部分,一般称为“词法分析器”. 1.1 =不同于== 一般容易将比较运算 ...

  9. position relative和absolute区别

    看这个博客 说的很详细http://blog.sina.com.cn/s/blog_647a022e0101b2gn.html 总的来说 这两个属性都是通过增加left和right偏离原来的位置  但 ...

  10. C++基础-位运算

    昨天笔试遇到一道题,让实现乘法的计算方法,设计方案并优化,后来总结位运算相关知识如下: 在计算机中,数据是以1010的二进制形式存储的,1bytes = 8 bits,bit就是位,所以位运算就是对每 ...