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 }
- javascript 模拟按键点击提交
- 微信小程序调用接口返回数据或提交数据
- 巧用shell脚本生成快捷脚本(r2第12天)
- asp.net动态增加服务器端控件并提交表单
- c# asp.net 实现分页(pager)功能
- 一次数据库无法登陆的"问题"及排查(r2第11天)
- popcorn-js视频Video框架简单用法
- 一次数据库响应缓慢的问题排查(r2第9天)
- 通过Ajax方式上传文件(input file),使用FormData进行Ajax请求
- C# 读取指定文件夹下所有文件
- ASP.NET 实现Base64文件流下载PDF
- MVC自定义视图引擎地址
- JS禁止鼠标右键、禁止全选、复制、粘贴的方法(所谓的防盗功能)
- impdp异常中断导致的问题(r2第8天)
- 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 数组属性和方法