ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ1984 Navigation Nightmare ——并查集 注意细节

2021-10-01 21:30:18  阅读:211  来源: 互联网

标签:int Nightmare 查集 fa maxn POJ1984 include nor define


注意合并x y集合时的操作:找到根结点fx fy, fa[fx] = fy, 接下来修改fx点的权值,这个值要考虑到点x和y和x y间的关系,具体见代码74-77

先存下所有询问,然后离线做

POJ1984

题意:

有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1。

思路:

用east数组表示i到父节点需要向东走的距离,nor表示i到父节点需要向北走的距离(负值表示正西和正南),然后直接用带权并查集,询问时曼哈顿距离就是两个权值的绝对值之和。由于询问是嵌在给出关系中间的,所以要先存下所有关系和询问,离线做就行。

// Decline is inevitable,
// Romance will last forever.
// POJ1417
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
using namespace std;
#define mst(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
//#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
//#define int long long
const int maxn = 4e4 + 10;
const int maxm = 25;
const int P = 1e9 + 7;
int fa[maxn];
struct node {
    int num, a, b, t;
    bool operator < ( const node &x) const {
        return t < x.t;
    }
}q[maxn];
int n, m;
int east[maxn]; //走east[i]到达根结点
int nor[maxn];  //走nor[i]到达根结点
int find(int x) {
    if(x == fa[x]) return x;
    int tmp = fa[x];
    fa[x] = find(fa[x]);
    east[x] = east[x] + east[tmp];
    nor[x] = nor[x] + nor[tmp];
    return fa[x];
}
int ans[10010];
int a[maxn], b[maxn], l[maxn];
char c[maxn][3];
int abs(int x) {
    return x > 0 ? x : -x;
}

void solve() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        fa[i] = i;
    for(int i = 1; i <= m; i++)
        scanf("%d%d%d%s", &a[i],&b[i],&l[i],c[i]);
    int k;
    scanf("%d", &k);
    for(int i = 1; i <= k; i++){
        scanf("%d%d%d", &q[i].a,&q[i].b, &q[i].t);
        q[i].num = i;
    }
    sort(q + 1, q + k + 1);
    int pos = 1;
    for(int i = 1; i <= m; i++) {
        if(pos == k + 1) break;
        int fx = find(a[i]);
        int fy = find(b[i]);
        if(fx != fy) {
            fa[fx] = fy;
            if(c[i][0] == 'E') east[fx] = l[i] + east[b[i]] - east[a[i]],nor[fx]=nor[b[i]]-nor[a[i]];
            else if(c[i][0] == 'W') east[fx] = -l[i] + east[b[i]] - east[a[i]],nor[fx]=nor[b[i]]-nor[a[i]];
            else if(c[i][0] == 'N') nor[fx] =l[i]+nor[b[i]]-nor[a[i]],east[fx]=east[b[i]]-east[a[i]];
            else nor[fx] = -l[i] + nor[b[i]] - nor[a[i]],east[fx]=east[b[i]]-east[a[i]];
        }
        while(i == q[pos].t) {
            int fx = find(q[pos].a);
            int fy = find(q[pos].b);
            if(fx != fy) {
                ans[q[pos].num] = -1;
                pos++;
                continue;
            }
            int dx = abs(east[q[pos].a] - east[q[pos].b]);
            int dy = abs(nor[q[pos].a] - nor[q[pos].b]);
            ans[q[pos].num] = dx + dy;
            pos++;
        }
    }
    for(int i = 1; i <= k; i++)
        printf("%d\n", ans[i]);
}
signed main() {
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int T; scanf("%d", &T); while(T--)
//    int T; cin >> T; while(T--)
    solve();
    return 0;
}

标签:int,Nightmare,查集,fa,maxn,POJ1984,include,nor,define
来源: https://blog.csdn.net/m0_59273843/article/details/120580786

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有