P5344 【XR-1】逛森林 题解
题目链接:逛森林
很早就想写写倍增优化建图,尤其是这题,奈何之前知识点没点够,本题线段树优化建图要优一些,不再赘述,没注意 \(m\) 是 \(1e6\),挂了 \(n\) 多发才发现。后续再详细讲解倍增优化建图,这里简述本题做法。
倍增优化建图其实和线段树优化建图恰不多的思想,为倍增求 \(LCA\) 的每个倍增点开一个 入点 和 出点。对于出点,从下往上连,入点从上往下连,并且初始化开销为 \(0\)。在做树上倍增时,同时对出入点两个数组也做倍增连接,即 \(p_1 \rightarrow p_2\rightarrow p_3\),在倍增数组上是这样的,我们的 \(p_1 \rightarrow p_3\),可以用 \(p_2\) 做中转,即 \(p_1\) 跳 \(2^i\) 步到 \(p_2\) 再跳 \(2^i\) 步到 \(p_3\),这是倍增的最基本思想,我们借助这个思想的过程,同时也对出入点进行这样的倍增连边预处理。这就是倍增优化建图最基本的初始化,当然对于 \(2\) 操作,直接基本连边就行了,并且主要,当前点也要和出入点的起点进行连边,这是 有向边。
对于有效的操作 \(1\),我们开两个新的虚拟点,分别与深度深的那个点连接,把它当做 入口/出口,即起点或者终点,在跳 \(LCA\) 的过程中,顺便与每个倍增点进行建边连接,最后这两个点之间建立题目所给的权值边。其实这两个虚拟点也即是深度更深的那个点。
红色的点即为 \(u_1\) 抽象出来的新点,它也即是 \(u_1\),用它和倍增点连接,也即使 \(u_1\) 和各个倍增点连接,不直接用 \(u_1\) 连接倍增点的原因很简单,每组和 \(u_1\) 连接的 \(u_2\) 并不一定是同一个,且 \(v_1\) 也不一定是同一个,但有可能经过相同的倍增点,但这个倍增点由于都是从 \(u_1\) 出发,所以和很有可能被包含了,导致了新的不存在路径的出现,其实和线段树优化建图是很相似的,用虚拟新 \(u_1\) 来表示这条 \(u_1\) 到 \(u_2\) 的点情况,这两个深度更深的点即为代表元对象。
常规倍增优化建图初始化显然是 \(n\log{n}\) 个点,但本题由于 \(m=1e6\),且最坏全为 \(1\) 操作,每次都有效的情况,点数达到 \(1e6\) 完全超过 \(n\log{n}\),点数最坏 \(2m>f(n)\),\(f(n)\) 表示关于 \(n\) 的点数数量。那么跑最后跑堆优化的 dijkstra 的时候,假设总共有 \(t\) 条边,来思考 \(t\) 的数量级。初始化的点数和边数其实是一致的 \(f(n)\),但涉及到 \(m\) 个操作,每次最多产生 \(2\log{n}\) 条边,所以 \(t \sim m\log{n}\),最终堆优化的 dijkstra 的复杂度为:\(O(m\log{n}\log{m})\sim O(mlog{^2n})\)
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 5e4 + 10;
constexpr int M = 1e6 + 10;
vector<int> oldTree[N];
vector<pii> child[N << 7];
int n, m, s;
struct
{
int fa[N];
void init()
{
forn(i, 1, n)fa[i] = i;
}
int find(const int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool merge(int u, int v)
{
u = find(u), v = find(v);
if (u == v)return false;
fa[u] = v;
return true;
}
} UnionFind;
inline void addEgde(const int u, const int v)
{
oldTree[u].push_back(v), oldTree[v].push_back(u);
}
inline void add(const int u, const int v, const int val = 0)
{
child[u].emplace_back(v, val);
}
constexpr int T = 16;
int cnt, idx;
tuple<int, int, int, int, int> op[M];
int fa[N][T + 1], in[N][T + 1], out[N][T + 1];
int deep[N];
inline void dfs(const int curr, const int pa)
{
deep[curr] = deep[fa[curr][0] = pa] + 1;
add(in[curr][0] = ++idx, curr), add(in[curr][0], pa);
add(curr, out[curr][0] = ++idx), add(pa, out[curr][0]);
forn(i, 1, T)
{
const int pre = fa[curr][i - 1];
fa[curr][i] = fa[pre][i - 1];
int currIn = in[curr][i] = ++idx, currOut = out[curr][i] = ++idx;
add(currIn, in[curr][i - 1]), add(currIn, in[pre][i - 1]);
add(out[curr][i - 1], currOut), add(out[pre][i - 1], currOut);
}
for (const auto nxt : oldTree[curr])if (nxt != pa)dfs(nxt, curr);
}
inline void add(const int curr, const int x, const int i, const bool isOut)
{
isOut ? add(out[x][i], curr) : add(curr, in[x][i]);
}
inline void LCA(int x, int y, const int curr, const bool isOut)
{
if (deep[x] < deep[y])swap(x, y);
isOut ? add(y, curr) : add(curr, y);
forv(i, T, 0)if (deep[fa[x][i]] >= deep[y])add(curr, x, i, isOut), x = fa[x][i];
if (x == y)return;
forv(i, T, 0)
{
if (fa[x][i] != fa[y][i])
{
add(curr, x, i, isOut), add(curr, y, i, isOut);
x = fa[x][i], y = fa[y][i];
}
}
add(curr, x, 0, isOut);
}
inline int lca(const int u, const int v, const bool isOut)
{
const int curr = ++idx;
LCA(u, v, curr, isOut);
return curr;
}
int dist[N << 6];
constexpr int INF = 1e9 + 7;
inline void dijkstra()
{
forn(i, 0, idx)dist[i] = INF;
stdHeap<pll, vector<pll>, greater<>> q;
q.emplace(dist[s] = 0, s);
while (!q.empty())
{
auto [minVal,curr] = q.top();
q.pop();
if (minVal > dist[curr])continue;
for (const auto [nxt,val] : child[curr])if (dist[nxt] > minVal + val)q.emplace(dist[nxt] = minVal + val, nxt);
}
}
inline void solve()
{
read(n, m, s);
idx = n;
UnionFind.init();
while (m--)
{
int opt;
read(opt);
if (opt == 1)
{
int u1, v1, u2, v2, d;
read(u1, v1, u2, v2, d);
if (UnionFind.find(u1) != UnionFind.find(v1) or UnionFind.find(u2) != UnionFind.find(v2))continue;
op[++cnt] = tuple(u1, v1, u2, v2, d);
}
else
{
int u, v, val;
read(u, v, val);
if (!UnionFind.merge(u, v))continue;
addEgde(u, v), add(u, v, val), add(v, u, val);
}
}
forn(curr, 1, n)if (!deep[curr])dfs(curr, 0);
forn(i, 1, cnt)
{
auto [u1,v1,u2,v2,val] = op[i];
add(lca(u1, v1, true), lca(u2, v2, false), val);
}
dijkstra();
forn(i, 1, n)write(' ', (dist[i] == INF ? -1 : dist[i]));
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}