题解 AT1221 【水筒】

· · 题解

题目链接

看到题解区都是Kruskal重构树的题解,我来一发平易近人的Lca倍增题解qwq。

容易想到放到图上来做。

发现每一次询问的都是建筑物。 如果把建筑物看成点,也就是说对于任意两个点之间, 肯定有若干条路径。 其中肯定有一条最优的,也就是说其余的路径都是不需要的, 我们关心的只是最优的那一条。 把这些边删去, 我们就会惊讶的发现所有的建筑物连成了一棵树!

因此我们要求的就是这张网格图的MST了。 对于每一条边的长度, 我们可以多源 bfs 来求。 最早即最优。 如果两个源点所扩张到的点相交了, 那么这一条边的长度就是两个源点到该点距离和。 把 所有的边压入堆里, 最后并查集来实现 Kruskal 即可。

于是就得到了一棵树, 对于每一次询问这棵树的节点 x,y 之间的最大距离。 我们可以在 Lca 的同 时倍增解决了qwq!类似于货车运输。

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 2e3 + 10;
const int Q = 2e5 + 10;
int h,w,p,qq,sum;
int first[Q],d[N][N],star[N][N],f[Q][21],dis[Q][21],dep[Q];
int father[Q];
int fx[5] = {0 , 1 , -1 , 0 , 0};
int fy[5] = {0 , 0 , 0 , 1 , -1};
string s[N];
char dt[N][N];
bool v[Q],b[N][N];
void in(int &x) {
    x = 0;
    char ee = getchar();
    while(ee < '0' || ee > '9') ee = getchar();
    while(ee >= '0' && ee <= '9') x = (x << 1) + (x << 3) + ee - '0' , ee = getchar();
} struct NO
{int x,y;} Node[N * N],Build[N * N];
struct queue {
    int head,tail;
    NO a[N * N];
    void clear() {
        head = tail = 0;
    }
    void push(NO x) {
        a[tail++] = x;
    }
    NO front() {
        return a[head];
    }
    void pop() {
        head++;
    }
    bool empty() {
        return head == tail ? true : false;
    }
} q;
struct E {
    int next;
    int to;
    int w;
    void add(int x , int y , int w_) {
        next = first[x];
        to = y;
        w = w_;
        first[x] = sum;
    }
} e[Q * 2];
struct Edge {
    int l,r,w;
    bool operator < (const Edge b) const {
        return w > b.w;
    }
};
priority_queue<Edge> G;
inline int find(int x) {
    return father[x] == x ? x : father[x] = find(father[x]);
}
void bfs() {
    while(!q.empty()) {
        NO top = q.front();
        q.pop();
        int x = top.x;
        int y = top.y;
        b[x][y] = 1;
        for(int i = 1 ; i <= 4 ; i++) {
            int xx = x + fx[i];
            int yy = y + fy[i];
            if(xx <= 0 || xx > h || yy <= 0 || yy > w || dt[xx][yy] == '#' || b[xx][yy])
                continue;
            b[x][y] = 1;
            if(!star[xx][yy]) {
                star[xx][yy] = star[x][y];
                d[xx][yy] = d[x][y] + 1;
                q.push((NO) {
                    xx , yy
                });
            } else if(star[xx][yy] != star[x][y])
                G.push((Edge) {
                star[x][y] , star[xx][yy] , d[xx][yy] + d[x][y]
            });
        }
    }
} void Kruskal() {
    while(!G.empty()) {
        Edge now = G.top();
        G.pop();
        int x = now.l;
        int y = now.r;
        int fx = find(x);
        int fy = find(y);
        if(fx == fy) continue;
        father[fx] = fy;
        e[++sum].add(x , y , now.w);
        e[++sum].add(y , x , now.w);
    }
} void dfs(int x , int fa) {
    dep[x] = dep[fa] + 1;
    v[x] = 1;
    f[x][0] = fa;
    for(int i = first[x] ; i ; i = e[i].next) {
        int to = e[i].to;
        if(to == fa) continue;
        dis[to][0] = e[i].w;
        dfs(to , x);
    }
} void Dp() {
    for(int j = 1; j <= 20; j++)
        for(int i = 1; i <=p ; i++) {
            f[i][j] = f[f[i][j - 1]][j - 1];
            dis[i][j] = max(dis[f[i][j - 1]][j - 1] , dis[i][j - 1]);
        }
} int Lca(int x , int y) {
    int ans = 0;
    if(dep[x] < dep[y]) swap(x , y);
    for(int i = 20; i >= 0; i--)
        if(dep[f[x][i]] >= dep[y]) {
            ans =max(ans , dis[x][i]);
            x = f[x][i];
        }
    if(x == y) return ans;
    for(int i = 20; i >= 0; i--)
        if(f[x][i] && f[y][i])
            if(f[x][i] != f[y][i]) {
                ans =max(ans , max(dis[y][i] , dis[x][i]));
                x = f[x][i];
                y = f[y][i];
            }
    return max(ans , max(dis[x][0] , dis[y][0]));
} int main() {
//  freopen("2.in","r",stdin);
//  freopen("2.out","w",stdout);
    in(h);
    in(w);
    in(p);
    in(qq);
    for(int i = 1; i <= h; i++)
        cin>>s[i];
    for(int i = 1; i <= h; i++)
        for(int j = 0; j < w; j++)
            dt[i][j + 1] = s[i][j];
    for(int i = 1; i <= p; i++) {
        in(Build[i].x);
        in(Build[i].y);
        int ai = Build[i].x;
        int bi = Build[i].y;
        star[ai][bi] = i;
        father[i] = i;
        q.push((NO) {
            ai , bi
        });
    } bfs();
    Kruskal();
    for(int i = 1; i <= p; i++)
        if(!v[star[Build[i].x][Build[i].y]])
            dfs(star[Build[i].x][Build[i].y] , 0);
    Dp();
    for(int i = 1; i <= qq; i++) {
        int x,y;
        in(x);
        in(y);
        if(find(x) != find(y)) printf("-1\n");
        else printf("%d\n",Lca(x , y));
    } return 0;
}