良心送分题(牛客挑战赛35E+虚树+最短路)

题目链接

传送门

题意

给你一棵树,然后把这棵树复制\(k\)次,然后再添加\(m\)条边,然后给你起点和终点,问你起点到终点的最短路。

思路

由于将树复制\(k\)遍后结点个数高达\(10^{10}\)个,因此不能直接复制跑。

我们注意到\(m\leq 50000\),那么与这\(m\)条边有关的结点最多只有\(2m\)个(记作关键点),那么我们可以考虑把这些点抠出来跑最短路,不同版本之间的点的边由于题目给的\(m\)条边因此不同版本的两结点之间的距离就是\(1\),但是同一版本两结点之间的该怎么办呢?

直接暴力枚举同一版本中关键点之间的距离很容易退化到\(4m^2\),因此需要考虑其他的方法,这个时候就引入了虚树,将每个版本的树重构建虚树,虚树上的边的长度就是原树上这两点之间的最短距离。

虚树学习博客及模板可以看这篇博客:博客

最后我们把所有版本上的虚树上的边和题目给的\(m\)条边抠出来重新构成一张图,然后直接跑最短路就行。

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 500000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, m, k, tot, id, s, x, t, y, cnt;
int head[maxn], dfn[maxn];
vector<int> has[maxn];
unordered_map<LL,int> mp;

int getid(int x, int y) {
	LL tmp = 1LL * (x - 1) * n + y;
	if(mp.count(tmp)) return mp[tmp];
	else return mp[tmp] = ++id;
}

struct edge {
	int v, w, next;
}ed[maxn];

void add(int u, int v, int w) {
	ed[tot].v = v;
	ed[tot].w = w;
	ed[tot].next = head[u];
	head[u] = tot++;
}

struct LCA {
    int tot;
    int deep[maxn], fa[maxn][30], head[maxn], cost[maxn];
    struct edge {
        int v, w, next;
    }ed[maxn<<1];

    void init() {
        tot = 0;
        for(int i = 0; i <= n; i++) {
            head[i] = -1;
            cost[i] = deep[i] = 0;
        }
    }

    void add(int u, int v, int w) {
        ed[tot].v = v;
        ed[tot].w = w;
        ed[tot].next = head[u];
        head[u] = tot++;
    }

    void dfs(int u, int d, int p) {
        deep[u] = d, fa[u][0] = p, dfn[u] = ++cnt;
        for(int i = head[u]; ~i; i = ed[i].next) {
            int v = ed[i].v;
            if(v == p) continue;
            cost[v] = cost[u] + ed[i].w;
            dfs(v, d + 1, u);
        }
    }

    void lca() {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; (1<<j) <= n; j++) {
                fa[i][j] = -1;
            }
        }
        for(int j = 1; (1<<j) <= n; j++) {
            for(int i = 1; i <= n; i++) {
                if(fa[i][j-1] != -1) {
                    fa[i][j] = fa[fa[i][j-1]][j-1];
                }
            }
        }
    }

    int query(int u, int v) {
        if(deep[u] <= deep[v]) swap(u, v);
        int k;
        for(k = 0; (1 << (1 + k)) <= deep[u]; k++);
        for(int i = k; i >= 0; i--) {
            if(deep[u] - (1<<i) >= deep[v]) {
                u = fa[u][i];
            }
        }
        if(u == v) return u;
        for(int i = k; i >= 0; i--) {
            if(fa[u][i] != -1 && fa[u][i] != fa[v][i]) {
                u = fa[u][i], v = fa[v][i];
            }
        }
        return fa[u][0];
    }

    int dis(int u, int v) {
        return cost[u] + cost[v] - 2 * cost[query(u, v)];
    }
}L;

bool cmp(int x, int y) {
	return dfn[x] < dfn[y];
}

int stk[maxn], top;

void ins(int id, int x) {
	if(top == 1) {
		stk[++top] = x;
		return;
	}
	int fa = L.query(x, stk[top]);
	if(fa == stk[top]) {
		stk[++top] = x;
		return;
	}
	while(top > 1 && dfn[stk[top-1]] >= dfn[fa]) {
		int dis = L.dis(stk[top-1], stk[top]);
		int p1 = getid(id, stk[top-1]), p2 = getid(id, stk[top]);
		add(p1, p2, dis);
		add(p2, p1, dis);
		--top;
	}
	if(stk[top] != fa) {
		int dis = L.dis(fa, stk[top]);
		int p1 = getid(id, fa), p2 = getid(id, stk[top]);
		add(p1, p2, dis);
		add(p2, p1, dis);
		stk[top] = fa;
	}
	stk[++top] = x;
}

void build(int id, vector<int>& vec) {
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	sort(vec.begin(), vec.end(), cmp);
	stk[top = 1] = 1;
	for(decltype(vec.size()) i = 0; i != vec.size(); ++i) ins(id, vec[i]);
	while(top > 1) {
		int dis = L.dis(stk[top], stk[top-1]);
		int p1 = getid(id, stk[top-1]), p2 = getid(id, stk[top]);
		add(p1, p2, dis);
		add(p2, p1, dis);
		--top;
	}
}

int vis[maxn];
LL dis[maxn];

LL dij(int s, int t) {
	memset(dis, INF, sizeof(dis));
	priority_queue<pLi, vector<pLi>, greater<pLi> > q;
	dis[s] = 0;
	q.push({0, s});
	while(!q.empty()) {
		int u = q.top().se; q.pop();
		if(vis[u]) continue;
		vis[u] = 1;
		for(int i = head[u]; ~i; i = ed[i].next) {
			int v = ed[i].v, w = ed[i].w;
			if(dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				q.push({dis[v], v});
			}
		}
	}
	if(dis[t] < INF) return dis[t];
	else return -1;
}

int main() {
#ifndef ONLINE_JUDGE
	FIN;
#endif
	scanf("%d%d%d", &n, &m, &k);
	L.init();
	memset(head, -1, sizeof(head));
	for(int i = 1; i < n; ++i) {
		scanf("%d%d", &x, &y);
		L.add(x, y, 1);
		L.add(y, x, 1);
	}
	L.dfs(1, 0, 0);
	L.lca();
	for(int i = 1; i <= m; ++i) {
		scanf("%d%d%d%d", &s, &x, &t, &y);
		int p1 = getid(s, x), p2 = getid(t, y);
		has[s].emplace_back(x);
		has[t].emplace_back(y);
		add(p1, p2, 1);
		add(p2, p1, 1);
	}
	scanf("%d%d%d%d", &s, &x, &t, &y);
	int p1 = getid(s, x), p2 = getid(t, y);
	has[s].emplace_back(x);
	has[t].emplace_back(y);
	for(int i = 1; i <= k; ++i) build(i, has[i]);
	printf("%lld\n", dij(p1, p2));
    return 0;
}
posted @ 2019-12-21 12:21  Dillonh  阅读(311)  评论(0编辑  收藏  举报