http://poj.org/problem?id=1984 (题目链接)

题意

  给出一棵树,这棵树是以平面直角坐标系为基准建立的,也就是每个节点最多只有上下左右4条边。现在动态建树,同时询问两点间的曼哈顿距离

Solution

  一开始没看懂题,当做图写了个SPFA。。后来发现是树于是删掉重新写了个DFS。。最后又发现要动态建树。。尼玛。。又重新写了个带全并查集。。

  注意询问并不保证时间是升序的,要按照给定询问顺序输出。

代码

// poj1984
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 100000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct edge {int u,v,w,t;}e[maxn];
struct data {int u,v,id,d;}t[maxn];
int n,m,q,X[maxn],Y[maxn],ans[maxn],fa[maxn]; bool cmp(data a,data b) {
return a.id<b.id;
}
int find(int x) {
if (x==fa[x]) return x;
int f=find(fa[x]);
X[x]+=X[fa[x]];
Y[x]+=Y[fa[x]];
return fa[x]=f;
}
int main() {
scanf("%d%d",&n,&m);
char ch;
for (int x,u,v,w,i=1;i<=m;i++) {
scanf("%d%d%d %c",&u,&v,&w,&ch);
if (ch=='N') x=0;else if (ch=='S') x=1;else if (ch=='W') x=2;else x=3;
e[i]=(edge){u,v,w,x};
}
scanf("%d",&q);
for (int i=1;i<=q;i++) scanf("%d%d%d",&t[i].u,&t[i].v,&t[i].id),t[i].d=i;
sort(t+1,t+1+q,cmp);
for (int i=1;i<=n;i++) fa[i]=i;
int pp=1;
for (int u,v,w,ty,i=1;i<=m;i++) {
u=e[i].u,v=e[i].v,w=e[i].w,ty=e[i].t;
int r1=find(u),r2=find(v);
fa[r1]=v;
if (ty==0) X[r1]-=X[u],Y[r1]=e[i].w-Y[u];
else if (ty==1) X[r1]-=X[u],Y[r1]=-e[i].w-Y[u];
else if (ty==2) X[r1]=-e[i].w-X[u],Y[r1]-=Y[u];
else X[r1]=e[i].w-X[u],Y[r1]-=Y[u];
while (t[pp].id==i && pp<=q) {
if (find(t[pp].u)!=find(t[pp].v)) ans[t[pp].d]=-1;
else ans[t[pp].d]=abs(X[t[pp].u]-X[t[pp].v])+abs(Y[t[pp].u]-Y[t[pp].v]);
pp++;
}
if (pp==q+1) break;
}
for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
return 0;
}

  

【poj1984】 Navigation Nightmare的更多相关文章

  1. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  2. POJ1984:Navigation Nightmare(带权并查集)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7871   Accepted: 2 ...

  3. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

  4. 【转】Navigation Drawer(导航抽屉)

    创建一个导航抽屉 创建抽屉布局 初始化抽屉列表 处理导航项选点击事件 监听导航抽屉打开和关闭事件 点击应用图标来打开和关闭导航抽屉   创建一个导航抽屉 导航抽屉是一个位于屏幕左侧边缘用来显示应用程序 ...

  5. 【HDOJ】1072 Nightmare

    bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...

  6. 【HDOJ】3085 Nightmare Ⅱ

    双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...

  7. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  8. 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    [bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 ​ 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...

  9. 【译】UI设计基础(UI Design Basics)--导航(Navigation)(六)

    [译]UI设计基础(UI Design Basics)--导航(Navigation)(六)

随机推荐

  1. Windows Server 2008 R2中的ASP.NET环境架设

    .NET Framework的部分功能在Windows Server 2008 R2得到支持,包括:.NET 2/3/3.5的子集和ASP.NET.另外,PowerShell也在Server Core ...

  2. C# 发送邮件,QQ企业邮箱测试成功

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  3. linux基本工具使用(二)

    1 查找某个目录下面一个所有的可执行文件,并且删除(对删除一个工程的可执行文件格外有用) find . -maxdepth 1 -file f -perm -111 | xargs rm

  4. Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashSet进行学习.我们先对HashSet有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashSet.内容包括:第1部分 HashSet介绍第2部分 HashSe ...

  5. 利用mybatis-generator自动生成代码

    mybatis-generator有三种用法:命令行.eclipse插件.maven插件.个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用. 下面是从官 ...

  6. 牛X的CSS3

    See the Pen Dot Wave by Rich Howell (@roborich) on CodePen

  7. C# FTP上传

    public class FtpHelper { public FtpHelper(string p_url, string p_user, string p_password) { if (!p_u ...

  8. UTF-8和Unicode

    What's the difference between unicode and utf8? up vote 103 down vote favorite 49 Is it true that un ...

  9. Java Little Knowledge

    1.Constructor running order of Base class and Derived class This is Alibaba's audition problem. clas ...

  10. 【前端积累】createElement createTextNode

    <!DOCTYPE html> <html><!--树根--> <head> <meta charset="utf-8"> ...