本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
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
 
 
 
 
正解:带权并查集
解题报告:

  带权并查集裸题。

  大概就是比并查集多维护了一个dis数组,表示的含义就是一个到根的距离。

  每次我路径压缩的时候顺便把父亲节点的距离加到儿子节点上就可以了,有一定像延迟标记。

  合并的时候,画个图看看就可以发现,如果合并的是x、y,权值为z,集合的代表元素分别为r1,r2,则dis[r2]=dis[x]+z-dis[y]。直接做就可以了。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 50011;
int n,m,ans,father[MAXN],dis[MAXN]; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline int find(int x){
if(father[x]==x) return x;
int t=father[x]; father[x]=find(father[x]);
dis[x]+=dis[t]; return father[x];
} inline bool check(int x,int y,int z){
int r1=find(x),r2=find(y);
if(r1==r2) { if(dis[y]!=dis[x]+z) return false; return true; }
dis[r2]=dis[x]+z-dis[y];//可以画图看看距离的计算式
father[r2]=r1;
return true;
} inline void work(){
while(scanf("%d%d",&n,&m)!=EOF) {
for(int i=1;i<=n;i++) father[i]=i,dis[i]=0;
int x,y,z; ans=0;
for(int i=1;i<=m;i++) {
x=getint(); y=getint(); z=getint();
if(!check(x,y,z)) ans++;
}
printf("%d\n",ans);
}
} int main()
{
work();
return 0;
}

  

HDU3047 Zjnu Stadium的更多相关文章

  1. HDU3047 Zjnu Stadium 【带权并查集】

    HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...

  2. hdu3047 Zjnu Stadium (并查集)

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

  3. hdu3047 Zjnu Stadium【带权并查集】

    <题目链接> <转载于 >>> > 题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这 ...

  4. HDU3047 Zjnu Stadium 带权并查集

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

  5. 带权并查集--hdu3047 ZJnu stadium

    题意:给出一个n,m,n表示的是有n 个人,m表示的是 有m 对关系: 接下来输入的就是这m对关系,a,b,x:表示的是a,b相距x个距离:然后判断输入的是否与这个数的上面的数信息一致, 输出不一致的 ...

  6. hdu 3074 Zjnu Stadium (带权并查集)

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

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

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

  8. HDU 3047 Zjnu Stadium(带权并查集,难想到)

    M - Zjnu Stadium Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  9. hdu 3047 Zjnu Stadium 并查集高级应用

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

随机推荐

  1. tomcat访问日志分析

    常使用web服务器的朋友大都了解,一般的web server有两部分日志: 一是运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息 二是访问日志信息,它记录的访问的时间,IP,访问的资 ...

  2. 微信小程序开发遇见的问题之一

    在开发的时候,因为要定位到当前的城市,而小程序提供的API只是能够获取当前的经纬度,所以我们需要通过接入百度地图的API.调接口的时候发现报错,说域名不合法,此时千万不要着急,只要你登录你的微信开发公 ...

  3. Linux中的日志分析及管理

    日志文件对于诊断和解决系统中的问题很有帮助,因为在Linux系统中运行的程序通常会把系统消息和错误消息写入相应的日志文件,这样系统一旦出现问题就会“有据可查”.此外,当主机遭受攻击时,日志文件还可以帮 ...

  4. Service Name Port Number Transport Protocol tcp udp 端口号16bit

    https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol The DHCP employs a connectionless  ...

  5. MySQL二进制包安装简略过程

    l  软件目录 [root@MASTER_03 ~]# mkdir -pv /data/software [root@MASTER_03 ~]# cd /data/software/ [root@MA ...

  6. PAT 1072. 开学寄语(20) JAVA

    下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其QQ,封其电脑,夺其手机,收其ipad,断其wifi,使其百无聊赖,然后,净面.理发.整衣,然后思过.读书.锻炼.明智.开悟.精进 ...

  7. Python学习笔记1_初识Python

    一.Python的发展 1.CNRI时期:CNRI是自助Python发展初期的重要单位,Python1.5版之前的成果大部分都在此时期内完成 2.BeOpen时期:Guido van Rossum与B ...

  8. 序列化+protobuff+redis

    背景: 当redis里面需要存储 “key-字符串,value-对象” 时,是不能直接存对象,而是需要将序列化后的对象存进redis. redis没有实现内部序列化对象的功能,所以需要自己提前序列化对 ...

  9. LDAP注入

    理解LDAP与LDAP注入 0x01 LDAP简介 查阅了一些网上的资料,读了好久还是觉得理解起来很困难,感觉还是不够干,之后看到的一个博客http://www.chinaunix.net/old_j ...

  10. 019_Map Task数目的确定和Reduce Task数目的指定

    注意标题:Map Task数目的确定和Reduce Task数目的指定————自然得到结论,前者是后者决定的,后者是人为指定的.查看源码可以很容易看懂 1.MapReduce作业中Map Task数目 ...