题意:

观众席围成一圈。列的总数是300,编号为1–300,顺时针计数,我们假设行的数量是无限的。将有N个人去那里。他对这些座位提出了要求:这意味着编号A的顺时针X距离坐着编号B。例如:A在第4列,X是2,那么B必须在第6列(6=4+2)。现在你的任务是判断请求是否正确。

题目:

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)

思路

1.由于题中明确给出,编号A的顺时针X距离坐着编号B,故x<300,当我们用并查集找到一个祖宗节点时,我们可以将该点看作原点,将圈拉成直线看待。即可忽略成环。

2.b->a, a离”原点更近“

int f(int x)
{if(x!=dp[x]){int t=dp[x];/*因为在递归找祖先的过程中,dp[x]的值会改变,而我们我们只需要找到距离x最近的点,即可得到dp[x]到祖宗节点的距离。所以要记录t的值*/dp[x]=f(dp[x]);num[x]+=num[t];}return dp[x];
}
  1. dp[b]->dp[a]
  dp[v]=u;num[v]=num[a]+x-num[b];

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=5e4+10;
int dp[M],num[M];
int f(int x)
{if(x!=dp[x]){int t=dp[x];dp[x]=f(dp[x]);/*因为在递归找祖先的过程中,dp[x]的值会改变,而我们我们只需要找到距离x最近的点,即可得到dp[x]到祖宗节点的距离。所以要记录t的值*/num[x]+=num[t];}return dp[x];
}
bool dfs(int a,int b,int x)
{int u=f(a),v=f(b);/*a->u,b->v*/if(u==v){if(num[a]+x!=num[b])/*b->a,又起点为父节点*/return true;return false;}dp[v]=u;num[v]=num[a]+x-num[b];return false;
}
int main()
{int m,n;while(~scanf("%d%d",&m,&n)){int ans=0;for(int i=0;i<m;i++){dp[i]=i;num[i]=0;}while(n--){int a,b,x;/*设起点为祖宗节点,则都指向起点b->a*/scanf("%d%d%d",&a,&b,&x);if(dfs(a,b,x))ans++;}printf("%d\n",ans);}return 0;
}

Zjnu Stadium HDU - 304 加权并查集相关推荐

  1. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  2. Rochambeau POJ - 2912 (枚举和加权并查集+路径压缩)找唯一裁判

    题意:有n个人玩石头剪刀布,有且只有一个裁判.除了裁判每个人的出拳形式都是一样的. a<b表示b打败a,a=b表示a和b出拳一样,平手.a>b表示a打败b. 给出m个回合的游戏结果,问能否 ...

  3. A Bug‘s Life POJ 2492 加权并查集

    A Bug's Life POJ 2492 加权并查集 传送门:http://poj.org/problem?id=2492 Description Background Professor Hopp ...

  4. HDU 3234 Exclusive-OR [并查集]

    http://acm.hdu.edu.cn/showproblem.php?pid=3234 #Description 给你N个数,X0-X(N-1) 执行Q个查询 三种格式 I p v Xp= v ...

  5. hdu 1232 经典并查集应用

    http://acm.hdu.edu.cn/showproblem.php?pid=1232 完全就是并查集的应用啊... View Code 1 #include<iostream> 2 ...

  6. 畅通工程 hdu 1232 HDU - 1863 (并查集+最小生成树)

    畅通工程hdu 1232 并查集 Problem Description Input Output 参考代码 HDU - 1863 Problem Description Input Output 参 ...

  7. HDU 3234 Exclusive-OR(并查集)

    转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 题目:给出N个数,给出一些条件, ...

  8. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  9. hdu 1116 欧拉回路 并查集 一组字符串能否首尾相连成一个字符串

    主要是欧拉回路的基础知识,用并查集加工处理 注意欧拉回路和并查集的细节判断 不能粘贴复制,一定要理解之后再敲一遍代码,否则浪费更多的时间 #include <stdio.h> #inclu ...

最新文章

  1. 寻找U2OS中表达的基因及其promoter并用于后续annotation
  2. Collect proper diagnostic data is very important
  3. android.os.build修改,Android的os.BuildID对应的SDK版本号以及SDK版本号与APILevel对应关系.docx...
  4. 我们再也看不到“Win 10 破解版下载”
  5. 傻子坐飞机问题的求解
  6. 华为服务器默认什么系统,云服务器默认系统
  7. 阿里云windows 2012服务器部署java web程序教程
  8. 单例模式之懒汉式(线程安全)
  9. 基于MATLAB的数字图像处理-图像进行灰度化
  10. 如何进行航拍全景摄影(下)
  11. yii2自动更新时间
  12. 13:求圆的周长和面积
  13. U盘文件莫名丢失?这样做可轻松找回!
  14. php视频教程折蜗牛,折纸蜗牛的手工折法视频教程
  15. 题目0155-特异性双端队列
  16. 土地利用现状分类统计表生成
  17. 手摸手教学 - Docker(五) 超级爽!持久化数据库-bind mounts!
  18. 像素深度是什么?在arcgis中如何提取像素值以及经纬度(或x、y坐标)?
  19. Junit单元测试工具
  20. 利用ogg微服务版将oracle同步到kafka

热门文章

  1. Android之提示Cannot call this method while RecyclerView is computing a layout or scrolling
  2. 商丘高中计算机考试成绩查询系统,2019商丘中考招生成绩查询时间及网站公布...
  3. 这家AI公司用面具破解中国人脸识别系统!微信、支付宝、火车站无一幸免
  4. 惊呆了!竟然还有这样的操作!
  5. 写好一份数据分析报告的13个要点
  6. php 电梯程序设计,教你写出京东电梯式轮播
  7. 如何在android客户端中做到自动检查数据更新?,UpdateHelper
  8. oracle 参照完整性,Oracle中用表外键来保证系统参照完整性
  9. cs6序列号 mac版photoshop_重磅!Parallels Desktop 16 M1版发布
  10. oracle crontab e,Linux运维知识之通过crontab -e编辑生成的定时任务,写在哪个文件中...