HDU3047 Zjnu Stadium
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
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.
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 R, represents the number of
incorrect request.
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
带权并查集裸题。
大概就是比并查集多维护了一个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的更多相关文章
- HDU3047 Zjnu Stadium 【带权并查集】
HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...
- hdu3047 Zjnu Stadium (并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu3047 Zjnu Stadium【带权并查集】
<题目链接> <转载于 >>> > 题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这 ...
- HDU3047 Zjnu Stadium 带权并查集
转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...
- 带权并查集--hdu3047 ZJnu stadium
题意:给出一个n,m,n表示的是有n 个人,m表示的是 有m 对关系: 接下来输入的就是这m对关系,a,b,x:表示的是a,b相距x个距离:然后判断输入的是否与这个数的上面的数信息一致, 输出不一致的 ...
- hdu 3074 Zjnu Stadium (带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3047 Zjnu Stadium(带权并查集,难想到)
M - Zjnu Stadium Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- hdu 3047 Zjnu Stadium 并查集高级应用
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- c#文件流汇总
操作文件比较常见,项目中经常出现这样的需求:按每个月自动创建文件,并且向文件里面插入一些数据,那么我们将要分析,文件是否存在的情况:如果存在则直接打开文件流向文件中插入数据,如果不存在,则创建文件再插 ...
- TFS二次开发-基线文件管理器(4)-标签的创建
上一节已经完成了源码文件的读取,如果要将已经选择的文件保存为一个标签(Lable).在VS我们只能对一个目录做标签,非常的不方便.如果用下面的方法,将选择的文件路径保存为一个List在打标签,就非常的 ...
- 2048 worker_connections are not enough while connecting to upstream
2048 worker_connections are not enough while connecting to upstream http://mailman.nginx.org/piperma ...
- SQL SERVER临时表的使用
SQL SERVER临时表的使用 drop table #Tmp --删除临时表#Tmpcreate table #Tmp --创建临时表#Tmp( ID int IDENTITY (1 ...
- 一、Nuxt简介
1.Nuxt是什么 Nuxt.js是基于vue的服务器端渲染框架,常用来做SSR(服务器端渲染) 2.为什么用Nuxt Vue开发的SPA(单页应用)不利于搜索引擎的SEO优化 3 ...
- android学习一---搭建开发环境
android基于Java并运行Linux内核上的轻量级操作系统.由于是基于java的,学习起来也不是太难,对java有一定了解并知道一些基本的图形用户界面,入门就很简单了. 一.了解JDK ,SDK ...
- GPS坐标(WGS84)转换百度坐标(BD09) python测试
基础知识坐标系说明: WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系. GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统.由WGS84坐标系经加密后的坐标 ...
- render httprequest
render def my_view(request):# View code here... t = loader.get_template('myapp/index.html') c = Requ ...
- ionic项目注意点
1.controller 名字一定要大写 config如过传递controller,则要使用controllerAs ,开头使用小写 2.在scss中新添加scss文件,要重启
- iOS Swift 熊猫🐼跑酷 第一个小项目
前言:想用swift 写个小游戏 慢慢转化 能写出 ARKit来.但是又不能一口吃个胖子,慢慢来,在网络视频教程中撸了视频教学,断断续续看了半个多月,基本实现了 游戏主角