Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3179 Accepted Submission(s): 1224

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

/*
带权并查集.
find逐层更新cnt值.
用向量搞出merge关系.
cnt[i]表示从i的祖宗到i的距离.
有cnt[l2]=z+cnt[x]-cnt[y].
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
using namespace std;
int father[MAXN],cnt[MAXN],n,m,ans;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
int find(int x)
{
if(x==father[x]) return x;
int fa=father[x];
father[x]=find(father[x]);
cnt[x]+=cnt[fa];
return father[x];
}
void merge(int x,int y,int z)
{
int l1=find(x),l2=find(y);
father[l2]=l1;
cnt[l2]=z+cnt[x]-cnt[y];
return ;
}
int main()
{
int x,y,z;
while(~scanf("%d%d",&n,&m))
{
ans=0;
for(int i=1;i<=n;i++) father[i]=i,cnt[i]=0;
while(m--)
{
x=read(),y=read(),z=read();
int l1=find(x),l2=find(y);
if(l1==l2){if(cnt[y]-cnt[x]!=z) ans++;}
else merge(x,y,z);
}
printf("%d\n",ans);
}
return 0;
}

Hdu 2047 Zjnu Stadium(带权并查集)的更多相关文章

  1. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  2. HDU 3047 Zjnu Stadium(带权并查集)

    题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...

  3. HDU3047 Zjnu Stadium 带权并查集

    转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...

  4. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  5. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  6. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  7. hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14

    题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...

  8. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  9. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. [第二季ZYNQ] [南京米联]ZYNQ第二季更新完毕课程共计16节课

    ZYNQ第二季更新完毕课程共计16节课全部免费 [第二季ZYNQ]                                                                    ...

  2. 怎样退出mysql命令行

    使用: mysql -u root -p 进入 mysql 命令号以后, 如果想退出, 可以使用: quit 命令, 如下: mysql -u root -p quit;

  3. MySQL设置自增字段

    1.MySQL每张表只能有1个自增字段,这个自增字段即可作为主键,也可用作非主键使用,但是请注意将自增字段当做非主键使用时必须为其添加唯一索引,否则系统将会报错 )将自动增长字段设置为主键 CREAT ...

  4. springboot-异步、发送邮件(二)

    @Test //发送复杂邮件 void contextLoads2() throws MessagingException { MimeMessage mimeMessage = mailSender ...

  5. h5学习之表单

    <html> <head> <title>新型input类型及表单新元素</title> <meta charset="utf-8&qu ...

  6. python小知识- webbrowser模块 + join()方法

    一.join描述 将序列中的元素以指定的字符连接生成一个新的字符串. 语法 语法: ‘sep’.join(seq) 参数说明: sep:分隔符.可以为空 seq:要连接的元素序列.字符串.元组.字典 ...

  7. WPF实战案例-在线程内同步集合数据到UI线程

    有这样一个场景,在vm中,我们为了ui的体验,会异步访问后端接口,获取数据集合,如果这个集合绑定到界面,并且在线程内,怎么处理? 有人讲:this.Dispatcher.Invoke,如果在vm内呢? ...

  8. Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.

    修改 my.ini 加上 max_allowed_packet =6710886467108864=64M默认大小4194304  也就是4M修改完成之后要重启mysql服务,如果通过命令行修改就不用 ...

  9. openresty 阶段说明

    开发中常用的7阶段 set_by_lua*: 流程分支处理判断变量初始化 rewrite_by_lua*: 转发.重定向.缓存等功能(例如特定请求代理到外网) access_by_lua*: IP 准 ...

  10. 用VNC远程无线站

    用VNC远程无线站 第一步: 远程一台我们可以vnc连接的linux电脑(IP1为实际IP地址) vim /etc/ssh/sshd_config 修改:69行为 X11Forwarding no 为 ...