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. python基础(十)--函数进阶

    嵌套函数 >>> graphic = '三角形' >>> def chang(): graphic = '正方形' def chang1(): #内部嵌套的函数命名 ...

  2. S4VM解析

    S4VM解析 2018年08月03日 15:20:59 stringlife 阅读数 1233   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...

  3. 怎样查看或修改网页的标题title

    网页的标题一般指的是 <title>标签之间的文本节点值, 它会显示在浏览器的标签页上, 我们可以通过 document.title 来查看或修改它: document.title; // ...

  4. mybatis的BLOB存储与读取

    http://blog.csdn.net/luyinchangdejiqing/article/details/45096689 简单介绍一下背景环境,web开发避免不了照片附件之类的东东,原先是存到 ...

  5. chartjs显示数值标签插件:chartjs-plugin-datalabels

    Getting Started #Installation #npm   npm install chartjs-plugin-datalabels --save This plugin can al ...

  6. 事件处理程序EventUtil

    /**********事件处理程序***********EventUtil.js*浏览器兼容,<高三>13章 P354*2014-12-8************************* ...

  7. linux 下使用 VirtualBox 搭建集群环境

    参考文章: https://www.nakivo.com/blog/virtualbox-network-setting-guide/ https://help.ubuntu.com/lts/serv ...

  8. springboot集成websocket的两种实现方式

    WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...

  9. .net工作流引擎ccflow集成并增加自定义功能

    一.为什么需要自定义扩展 1.第三方类库已满足大部分需求,剩下的根据具体业务需求抽象成公共功能进行扩展 2.第三方呈现的web页面与原类库耦合度较高,希望在原页面上扩展而不影响原来的功能 3.在完全不 ...

  10. js页面内容只读,不可复制

    // document.oncontextmenu=new Function("event.returnValue=false"); // document.onselectsta ...