HDU3047 Zjnu Stadium 【带权并查集】
HDU3047 Zjnu Stadium
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:
(PS: the 5th and 10th requests are incorrect)
就是有一个环,给出多组关系满足a=b+x,判断不满足的有多少组
然后带权并查集维护一下,主义在合并两个并查集的时候差量的计算
#include<bits/stdc++.h>
using namespace std;
#define N 50010
int fa[N],dis[N];
int n,m;
int Find(int x){
if(fa[x]==x)return x;
int f=Find(fa[x]);
dis[x]+=dis[fa[x]];
return fa[x]=f;
}
void init(){for(int i=;i<=n;i++)fa[i]=i,dis[i]=;}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
int res=;
while(m--){
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
int f_a=Find(a);
int f_b=Find(b);
if(f_a==f_b){
if(dis[a]+x!=dis[b])res++;
continue;
}
dis[f_b]=dis[a]-dis[b]+x;
fa[f_b]=f_a;
}
printf("%d\n",res);
}
return ;
}
HDU3047 Zjnu Stadium 【带权并查集】的更多相关文章
- HDU3047 Zjnu Stadium 带权并查集
转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 3047–Zjnu Stadium(带权并查集)
题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
- HDU 3047 Zjnu Stadium(带权并查集)
题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...
- Zjnu Stadium(hdu3047带权并查集)
题意:一个300列的无限行的循环场地,a b d代表a,b顺时针相距d的距离,现在给你一些距离,判断是否有冲突,如果有冲突计算冲突的次数 思路:带权并查集 a,b的距离等于b到根节点的距离 - a到根 ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
- 【HDOJ3047】Zjnu Stadium(带权并查集)
题意:浙江省第十二届大学生运动会在浙江师范大学举行,为此在浙师大建造了一座能容纳近万人的新体育场. 观众席每一行构成一个圆形,每个圆形由300个座位组成,对300个座位按照顺时针编号1到300,且可以 ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
随机推荐
- Linux系统用户及用户组管理
目录一.新增/删除用户和用户组二.创建/修改密码三.用户身份切换--su和sudo 一.新增/删除用户和用户组1.用户组 命令 : groupadd 语法 : groupadd [-g GID] gr ...
- cygwin下安装软件
cygwin下安装软件cygwin工具安装新的软件和常见的命令windows8.1下安装Cygwin并通过apt-cyg安装软件包Cygwin利用apt-cyg安装gcc.g++.make和gdb 首 ...
- ubuntu16.04 安装以及要做的事情
1.安装ubuntu 选择安装时更新,以及MP3.图形等:然后选择分区(ext4)(安装时需先进入虚拟系统连上网,输入清华net账号),分区情况按照下图来,swap为临时用的内存分区,可以不要: 选择 ...
- play的过滤类怎么实现继承问题
原文: Example: public class Secure extends Controller { @Before static void checkAuthenti ...
- 初学Laravel框架与ThinkPHP框架的不同
作为一个PHP菜鸟初学Laravel框架 在学习过程中我发现了其与TP框架的不同点,由于时间问题和认识还不够完善我先写出其中几点,有错误的地方希望各位大牛斧正... 1.渲染模版方式的不同:在Lara ...
- 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元
http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...
- guava的事件发布订阅功能
事件的重要性,不用说很重要,在很多时候我们做完一个操作的时候,需要告知另外一个对象让他执行相应操作,比如当用户注册成功的时候,需要抛出一个注册成功的事件,那么有监听器捕获到这个事件,完成后续用户信息初 ...
- 解决spring boot在RabbitMQ堆积消息情况下无法启动问题
最近遇到一个问题,服务站点上线之前,先去新建需要的rabbitmq并绑定关系,此时 如果发送消息方运行, 那边会造成新建的q消息部分堆积得不到及时消费 那么问题来了? 在消息堆积情况下,服务站点无法启 ...
- grub2 windows版安装
一.BIOS方式,grub2安装 查看磁盘情况 E:\grub-2.02-for-windows>wmic diskdrive list brief Caption DeviceID Model ...
- yyyy-MM-dd EEE hh:mm:ss(日期转换)
<script> /** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) ...