BZOJ 3362 Navigation Nightmare
一道带权并查集题目.
带权并查集的重点是信息的合并.
这类题出现得并不多,练习一下.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FILE "dealing"
#define up(i,j,n) for(int i=j;i<=n;i++)
#define db long double
#define pii pair<int,int>
#define pb push_back
#define mem(a,L) memset(a,0,sizeof(int)*(L+1))
template<class T> inline bool cmin(T& a,T b){return a>b?a=b,true:false;}
template<class T> inline bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T> inline T squ(T a){return a*a;}
const ll maxn=+,inf=1e9+,mod=,M=;
ll read(){
ll x=,f=,ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')x=(x<<)+(x<<)+ch-'',ch=getchar();
return x*f;
}
int l[maxn],r[maxn],fa[maxn];
int getfa(int x){
if(fa[x]==x)return x;
int y=getfa(fa[x]);
if(y==fa[x])return y;
l[x]+=l[fa[x]],r[x]+=r[fa[x]];
fa[x]=y;
return y;
}
int n,m,Q;
int f1[maxn],f2[maxn],d[maxn],op[maxn];
int xx[maxn],yy[maxn],t[maxn],ans[maxn];
vector<int> linkk[maxn];
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(),m=read();
char ch;
up(i,,m){
f1[i]=read(),f2[i]=read(),d[i]=read();
scanf(" %c",&ch);
if(ch=='E')op[i]=;
else if(ch=='W')op[i]=;
else if(ch=='N')op[i]=;
else if(ch=='S')op[i]=;
}
Q=read();
up(i,,Q){
xx[i]=read(),yy[i]=read(),t[i]=read();
linkk[t[i]].push_back(i);
}
up(i,,n)fa[i]=i;
up(i,,m){
int x=f1[i],y=f2[i],fx=getfa(x),fy=getfa(y);
if(fx!=fy){
if(fx!=x)l[fx]=-l[x],r[fx]=-r[x],l[x]=r[x]=,fa[fx]=x,fa[x]=x;
if(fy!=y)l[fy]=-l[y],r[fy]=-r[y],l[y]=r[y]=,fa[fy]=y,fa[y]=y;
fa[x]=y;
if(op[i]==)l[x]=d[i];
else if(op[i]==)l[x]=-d[i];
else if(op[i]==)r[x]=d[i];
else r[x]=-d[i];
}
for(int j=;j<linkk[i].size();j++){
int t=linkk[i][j];
int x=xx[t],y=yy[t];
int fx=getfa(x),fy=getfa(y);
if(fx!=fy)ans[t]=-;
else ans[t]=abs(l[x]-l[y])+abs(r[x]-r[y]);
}
}
up(i,,Q)printf("%d\n",ans[i]);
return ;
}
BZOJ 3362 Navigation Nightmare的更多相关文章
- BZOJ 3362 Navigation Nightmare 带权并查集
题目大意:给定一些点之间的位置关系,求两个点之间的曼哈顿距离 此题土豪题.只是POJ也有一道相同的题,能够刷一下 别被题目坑到了,这题不强制在线.把询问离线处理就可以 然后就是带权并查集的问题了.. ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1984 Navigation Nightmare (数据结构-并检查集合)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4072 Accepted: 1 ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- [POJ1984]Navigation Nightmare
[POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
随机推荐
- 全国天气预报数据API调用PHP示例
本代码示例是基于PHP的聚合数据全国天气预报API服务请求的代码样例,使用前你需要: ①:通过https://www.juhe.cn/docs/api/id/39 申请一个天气预报API的appkey ...
- 聚合数据 iOS 项目开发实战:条码查询器
记录下,聚合数据 iOS 项目开发实战:条码查询器:视频地址:http://www.jikexueyuan.com/course/324.html 条码查询API:https://www.juhe.c ...
- show processlist 各个状态说明
执行状态分析 1.Sleep状态 通常代表资源未释放,如果是通过连接池,sleep状态应该恒定在一定数量范围内 实战范例:因前端数据输出时(特别是输出到用户终端)未及时关闭数据库连接,导致因网络连接速 ...
- TCP/IP详解 卷一(第四、五章 ARP、RARP)
数据链路如 以太网都有自己的寻址机制(MAC)地址,而IP层使用的是IP地址. 当一台主机把以太网数据发送定位于同一局域网上的另一台主机时,是根据MAC地址来确定目的接口的.设备驱动程序从不检查IP数 ...
- Sql 复习3之存储管理
一.事务管理 单个工作单元称为事务,我们将若干条sql命令组合在一起,形成存储过程.触发器等,利用存储过程和触发器可以进行事务管理. 二.编程基础介绍 主要有:函数.程序设计语句等. 程序设计语句: ...
- Mongo-Hadoop
下载 https://github.com/mongodb/mongo-hadoop/releases 解压到/home/kevin/hadoop/hadoop/share/mongo-hadoop- ...
- (一)关于jQuery的网上资源
jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...
- Snubber电路
http://www.elecfans.com/yuanqijian/dianrongqi/20170601520736.html https://wenku.baidu.com/view/166f1 ...
- 如何在DOS窗口中显示UTF-8字符
在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即 ...
- 一份还热乎的蚂蚁面经(已拿Offer)!附答案!!
本文来自我的知识星球的球友投稿,他在最近的校招中拿到了蚂蚁金服的实习生Offer,整体思路和面试题目由作者--泽林提供,部分答案由Hollis整理自知识星球<Hollis和他的朋友们>中「 ...