Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3186    Accepted Submission(s):
1226

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
题目大意:

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

 
 

教训:

又一次为多组数据所伤。输入不完整,初始化不彻底,不要再重蹈覆辙了。
 

思路:

这题真的是个加权并查集。。。
输入的x,y,z可以看做是从x指向y的一条边,这条边的边权需要修改为z
确定一个根节点,暂且取名为root。way[i]表示i点到他所对应的根节点的距离
在连接时,有一个式子比较难懂  way[y1]=way[x]+z-way[y];可以看图分析
不难看出  way[root2]+way[y]==way[x]+z  而此时的way[root2]==way[y1]  问题解决
剩下的就是朴素的加权并查集问题了
 
#include<iostream>
#include<cstdio>
using namespace std;
#define MAXN 50010
int n,m,father[MAXN],way[MAXN],ans;
void before()
{
for(int i=;i<MAXN;i++)father[i]=i,way[i]=;
ans=;
}
int find(int x)
{
if(x==father[x])return father[x];
int fa=father[x];
father[x]=find(father[x]);
way[x]+=way[fa];
return father[x];
}
void unit(int x,int y,int x1,int y1,int z)
{
father[y1]=x1;
way[y1]=way[x]+z-way[y];
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
before();
int x,y,z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
int f1=find(x),f2=find(y);
if(f1!=f2)unit(x,y,f1,f2,z);
else if(way[y]-way[x]!=z)ans++;
}
printf("%d\n",ans);
}
}

Zjnu Stadium(加权并查集)的更多相关文章

  1. HDU 3407.Zjnu Stadium 加权并查集

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

  2. hdu3047 Zjnu Stadium (并查集)

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

  3. hdu 3047 Zjnu Stadium(并查集)

    题意: 300个座位构成一个圈. 有N个人要入座. 共有M个说明 :A B X ,代表B坐在A顺时针方向第X个座位上.如果这个说明和之前的起冲突,则它是无效的. 问总共有多少个无效的. 思路: 并查集 ...

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

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

  5. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  6. A Bug's Life(加权并查集)

    Description Background  Professor Hopper is researching the sexual behavior of a rare species of bug ...

  7. A Bug's Life(加权并查集)

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  8. P1196 银河英雄传说(加权并查集)

    P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在 ...

  9. 洛谷 P2024 [NOI2001]食物链(种类并查集,加权并查集)

    传送门 解题思路 加权并查集: 什么是加权并查集? 就是记录着每个节点到它的父亲的信息(权值等). 难点:在路径压缩和合并节点时把本节点到父亲的权值转化为到根节点的权值 怎么转化呢? 每道题都不一样Q ...

随机推荐

  1. Qt & MySQL

    Qt中如何进行MySQL连接与操作步骤: 1.向工程中的.pro文件增加QT += sql; 2.写一个通用的数据库连接类(Connect),一个static方法(CreateConnection), ...

  2. BZOJ 3990 [SDOI2015]排序

    题解: 首先很容易看出各个操作是互不影响的,即对于一个合法的操作序列,我们可以任意交换两个操作的位置而不影响合法性. 因此我们可以忽略操作先后的影响,只考虑这个操作是否会出现在操作序列中. 如果用2n ...

  3. 《C prime plus (第五版)》 ---第12章 存储类.链接和内存管理

    12-1:存储类: 1.作用域: 代码块作用域,函数原型作用域和文件作用域. 2.链接:分为外部链接,内部链接和空链接.代码块作用域和函数原型作用域都是空连接,意味着是私有的.而文件作用域的变量可能是 ...

  4. BZOJ3262 陌上花开 —— 三维偏序 CDQ分治

    题目链接:https://vjudge.net/problem/HYSBZ-3262 3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit ...

  5. python列表推导式的if-else

    a=[i for i in range(10) if i%2==0]print(a)a=[i if i%2==0 else 'qi' for i in range(10)]print(a) 结果:[0 ...

  6. SpringBoot_00_资源汇总贴

    一.精选 1.Spring Boot文档 二.参考资料 1.springboot-learning-example 2.Spring boot 那些事 3.Spring Boot干货系列 4.Spri ...

  7. python打印字体颜色

        格式:\033[显示方式;前景色;背景色m 显示方式           意义-------------------------0                终端默认设置1         ...

  8. 集训Day11

    别人的题公开原题面不好 写题解吧 T1 很明显答案满足二分,二分之后算出每个人的位置,做一个LIS即可 T2 阅读体验极差 T3 根本不会 交都没交 期望得分200

  9. bzoj 3267: KC采花&&3272&&3638&&3502 线段树

    题目大意 给定一个长为n的序列,维护两种操作: 1.单点修改 2.在[l,r]这段区间中取k个互不相交的子段,使子段之和最大. \(n \leq 50000,k \leq 20\) 题解 四倍经验.( ...

  10. poj1094Sorting It All Out——拓扑排序

    题目:http://poj.org/problem?id=1094 看到此题,首先觉得这是一种层层递进的关系,所以可以想到用拓扑排序: 就像人工排序,每次需要找到一个最小的,再找到新的最小的……所以用 ...