hdu 3074 Zjnu Stadium (带权并查集)
Zjnu Stadium
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1744 Accepted Submission(s): 660
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)
Source
2009 Multi-University Training Contest 14 - Host by ZJNU
提议很简单,就是问:
a和b是否在同一个集合,不在就将他们加入到集合,在就对他们进行路径比较,如果给定的路程和实际路程不一样,那就表示冲突,记录下来
输出最后冲突的数量。(注意的是,b在a的前面,也就是b到根的路程要大于a到根的路程)
---------》带权值的路径......
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #define maxn 50005
4
5 int father[maxn];
6 int dist[maxn];
7 int n,m,re;
8
9 void init(){
10 re=0;
11 for(int i=0;i<=n;i++){
12 father[i]=i;
13 dist[i]=0;
14 }
15 }
16
17 int fin(int x){
18 if(x==father[x]) return x;
19 int pre=father[x];
20 father[x]=fin(father[x]);
21 dist[x]+=dist[pre]; //统计每一个点到根点的距离
22 return father[x];
23 }
24
25 void Union(int x , int y , int val ){
26 int a=fin(x);
27 int b=fin(y);
28 if(a==b){
29 //注意方向-------顺时针
30 if(dist[y]-dist[x]!=val)
31 re++;
32 }
33 else{
34 father[b]=a;
35 dist[b]=dist[x]-dist[y]+val;
36 }
37 }
38
39 int main(){
40 int a,b,c;
41 while(scanf("%d%d",&n,&m)!=EOF){
42 init();
43 while(m--){
44 scanf("%d%d%d",&a,&b,&c);
45 Union(a,b,c);
46 }
47 printf("%dn",re);
48 }
49 return 0;
50 }
- Linux下FTP环境部署梳理(vsftpd和proftpd)
- Silverlight如何与JS相互调用
- Docker容器学习梳理--私有仓库Registry使用
- 从插件重构看如何提升测试质量与效率
- 巧用WinRAR+Javascript解决activeX的自动安装问题
- 在网页中实现QQ的屏幕截图功能
- Activity之间传递参数
- linux下rsync和tar增量备份梳理
- 重温Delphi之:面向对象
- Android新手之旅(15) Win7下配置遇到的问题
- 重温Delphi之:如何定义一个类
- Android新手之旅(2) 新手问题
- Android新手之旅(2) 新手问题
- Android新手之旅(9) 自定义的折线图
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Redis过期策略以及淘汰机制
- 几行代码就可以轻松给你的程序加上进度条
- git禁止在master分支push和commit
- 记录一次mybatis缓存和事务传播行为导致ut挂的排查过程
- appium教程_3.启动appium-server
- appium教程_4.adb常用命令
- Python中的高阶概念属性:五个你应该搞明白的知识点
- 一次奇怪的http状态码改变
- Salesforce LWC学习(二十七) File Upload
- 让我们来谈谈python中的prettyprint和pprint
- vue 开发规范
- Markdown 编写规范
- JavaScript编码规范
- HTML编码规范
- postgres数据库不能用ip地址访问的问题