解题思路转自:

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. rman 使用catalog备份的演示

    介绍了如何使用catalog方式做RMAN备份,以及如何取消以catalog方式做备份. 第一步:创建RMAN CATALOG表空间及用户. [oracle@oel-01 ~]$ sqlplus / ...

  2. Bitmap recycle()

    Bitmap调用recycle? When? Bitmap有一个recycle方法,意思非常easy,回收Bitmap的空间. Q 1: Bitmap是否有调用recycle方法的必要性? A: 嵌入 ...

  3. Learn Python The Hard Way, 2nd Edition 尾声

    看完了这本书,你决定继续做编程.也许它能成为你的一个职业,也许它能成为你的一项爱好.但你需要一些指导,确保自己不会走错了道路,或帮助你从这个新业余爱好中得到最大的乐趣. 我做了很久的编程.久的你都想象 ...

  4. 基于表单的身份验证(FBA)

    https://technet.microsoft.com/zh-cn/library/ee806890(office.15).aspx http://www.tuicool.com/articles ...

  5. java concurrency: daemon线程

    daemon线程的概念 在学习操作系统概念的时候,我们就曾听说过daemon的概念.daemon本身指的是在后台运行的进程或者线程,一般用来提供某些不需要与用户直接交互的服务,有点像我们见到的一些系统 ...

  6. linux命令:rsync, 同步文件和文件夹的命令

    Usage: rsync [OPTION]... SRC [SRC]... DEST  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST  or ...

  7. LinkNode 温度报警器视频(2016-05-15)

    文档就不发了,申请的时候说要官方首发,所以半个月后,这里就只上一个视频表表心意.

  8. YII框架实现排序

    YII框架实现排序 用YII2实现批量修改排序功能,如下图 控制器: /** * Lists all CollectionAlbum models. * @return mixed */ public ...

  9. Regex阅读笔记(三)之固化分组

    符号:?> 使用?>的匹配与正常的匹配无区别,但是如果匹配进行到此结构之后,此结构体的所有备用状态都会放弃,也就是括号内的子表达式中未尝试过的备用状态都不复存在了. 例如'(\.\d\d( ...

  10. background-position 个人理解

    background-position这里先说像素  百分比比较复杂background-position:xxpx xxpx  这里第一个值指的是x轴坐标  第二个值是y轴坐标这里使用的坐标系和数学 ...