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 ...
随机推荐
- UI auto程序结构组织方式
UI Auto分三个layer: 1. Object finding – 单独一个类,寻找到控件.因为UI auto最容易改动的就是UI界面,这样全部放到一起就便于统一修改. 2. Task - 对控 ...
- Martin Fowler’s Active Record design pattern.
P of EAA: Active Record https://www.martinfowler.com/eaaCatalog/activeRecord.html Active Record An o ...
- css 生效顺序 less 写法
<!DOCTYPE html><html><style type="text/css">.c{color:red;}.c{color:green ...
- 【转】 通过VMLibrary在client partition上安装AIX全程实录
现场实录,应该对新手有所帮助,因为本人就是新手...最近一段时间跟Linux无缘,都是在和PowerVM打交道,博文更新慢,不赖我,哈哈本文参考链接:AIX Migration with File-B ...
- Windows常见窗口样式和控件风格
Windows常见窗口样式和控件风格 王佰营 徐丽红 一.窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用)WS_CHILDWINDOW 子窗口(不能与WS_PO ...
- (2)linux未使用eth0,未使用IPV4导致无法连接
首先ifconfig查看网络IP 看,我这里默认启用了2个网卡,一个是eth0,另一个是lo(基于loopback方式) 1.如果有eth0则做:界面修改 (1)输入命令setup,选择network ...
- 剑指offer 面试14题
面试14题: 题目:剪绳子 题:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,且n>1,m>1),每段绳子的长度记为k[0],k[1],k[2],...,k[m].请问k[0] ...
- 解释*args和**kwargs的含义
当我们不知道向函数传递多少参数时,比如我们向传递一个列表或元组,我们就使用*args def func(*args): for i in args: print(i) func(3,2,1,4,7) ...
- VS 无法调试 IIS
用附加的方式断点无效时. 解决方案: 一.VS设置的 .Net版本 与 IIS应用程序池的版本 不一致. 操作步骤: 1. 在VS-> 项目属性, 配置 自定义Web服务器 , 这里 ...
- python有哪些好的学习资料或者博客?
推荐Full Stack Python 有各种python资源汇总,从基础入门到各种框架web应用开发和部署,再到高级的ORM.Docker都有.以下是Full Stack Python 上总结的一些 ...