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. 【zabbix】微信告警消息模版

    下面给出了一个zabbix微信告警消息的模版, 消息最后加上#号和短横线的设计有两个原因: 1,zabbix的微信告警消息总是被截断,比如最后一个告警时间,如果没有最后一行#号,在微信上看的时候时间不 ...

  2. Java for LeetCode 114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

  3. ZOJ - 3930 Dice Notation 【模拟】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3930 题意 给出一串字符串 如果是 '+' '-' '*' '/ ...

  4. i=i+2 与i+=2

    i=i+2 比 i+=2多了一次对变量 i 的运算.后者效率高

  5. <JAVA图像学习笔记>十字路口交通模拟--操作系统模拟课后小项目

    项目的要求很简单: 模拟出十字路口的交通控制情况: 秒. 当东西(或南北)方向红灯时,所有车辆(除了消防车.救护车.警车)均排队等待,当东西(或南北)方向绿灯时,所有车辆按序行驶(不准超车). 制作这 ...

  6. 解决ini-parser解析ini文件中文乱码问题

    rickyah/ini-parser 是一个.net 平台解析ini文件的库,当ini文件中含有中文字符时会乱码. 解决:将文件通过Editplus 等文本编辑工具保存为 utf-8 + bom 格式 ...

  7. ubuntn下 apt的用法和yum的比较(转)

    centos有yum安装软件,Ubuntu有apt工具. apt简单的来说,就是给Ubuntu安装软件的一种命令方式. 一.apt的相关文件 /etc/apt/sources.list 设置软件包的获 ...

  8. 网络编程学习笔记-MAC地址和IP地址的关系

    简单地说:ip地址是服务商给你的,mac地址是你的网卡物理地址. 一.IP地址 对于IP地址,相信大家都很熟悉,即指使用TCP/IP协议指定给主机的32位地址.IP地址由用点分隔开的4个8八位组构成, ...

  9. phpstorm 代码按列对齐

    设置方式: Preference... -> Editor -> CodeStyle -> PHP -> Other -> Align key-value pairs

  10. kettle每天自动发送邮件总结_20161128

    kettle作为java开发的工具,很多功能在目前工作中还用不到,原来它也是支持java代码的,现在用到的也就是它从服务器导数到数据库,然后再进行数据处理的功能. 如何快速学会使用kettle发送邮件 ...