解题思路转自:

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. javaweb学习路之二--上传gitgub

    代码上传github 代码上传到github的步骤 第一步:申请github账号 https://github.com/注册账号 第二步:登录github,新建repository仓库,命名,创建 第 ...

  2. [译]SSRS 报表版本控制

    问题 如今商务智能应用广泛,对我们的商业愈加重要. 对新报表和的各种需求不断攀升. 自 SQL Server 2008 R2的 Reporting Services (SSRS) 开始,微软视图为减轻 ...

  3. UILabel显示html文本

    NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\ ...

  4. (Problem 37)Truncatable primes

    The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...

  5. (IOS)签名Demo

    思路是将每一次按下屏幕的touch move时的点存到一个数组里,即一个数组相当于一个笔画:再将该代表笔画的数组保存到一个大数组中,每组每次touch的移动都历遍大数组和笔画数组,将点于点之间连接起来 ...

  6. (IOS)数据持久化

    1.属性列表序列化 2.模型对象归档 3.嵌入式SQLite3 4.Core Data 5.应用程序设置 6.UIDocument管理文档存储 7.iCloud Demo界面: 1.属性列表序列化 即 ...

  7. qt qml 利用xmlhttprequest 调用有赞api

    最近朋友在有赞商城上面开了一个店铺,因为有实体店,一般卖商品后送货上门,但是打票时候老是人工用world文档人工复制黏贴订单打印小票, 所以就找我帮忙做一个软件专门打印小票的,就研究起来调用有赞第三方 ...

  8. html = data.decode('gbk').encode('utf-8')

    html = data.decode('gbk').encode('utf-8')此处encode编码要与html文件内charset=utf-8的格式一致,如果不一致,浏览器打开乱码,文本编辑器正常 ...

  9. 基于visual Studio2013解决C语言竞赛题之0402奇偶求和

      题目 解决代码及点评 这道题考察我们对循环和判断的综合应用 #include <stdio.h> #include <stdlib.h> #include < ...

  10. BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划

    题目 1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec  Memory Limit: 64 MB Description 奶牛们打算通过锻炼来 ...