链接
题解
建立有根圆方树
现在每个环在圆方树中都呈现出这样的形态:有一个特殊的圆点,这个圆点是整个环中按照 最先遍历到的点;有一个方点;还有其余若干个圆点。方点的父亲节点是特殊的圆点,其余圆点的父亲都是方点
回想树上 ,我们用 来计算距离
在仙人掌上面,还要解决环的问题
可以发现,我们仍然可以用类似的方法来求解仙人掌上两点距离,上述“特殊圆点”的特殊之处可以这么理解:如果我想从一个点经过某个环走到根节点,那么肯定要经过这个环的“特殊圆点”
一个环上的每个点到”特殊圆点“都有两条路,二者取较小值,然后把这个较小值作为其到方点的边权
然后就会发现,一个点到根节点的最短路正好就是圆方树上这个点到根节点的最短路!
我们开始类比树上 的方法,看一下如果两个查询点 是圆点,那么直接用上述和树上相同的公式就ok
如果 是方点,就说明 是个环,求出环上对应的两个点,然后会发现有一段优弧和一段劣弧,取较小的那一段即可
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 200010
#define maxe 1000010
#define maxk 18
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=0;
}
void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
#define forp(_,__) for(auto p=__.head[_];p;p=__.next[p])
}G;
struct Circle_Square_Tree
{
ll dfn[maxn], low[maxn], tim, tot, s[maxn], e[maxn], n;
vector<ll> cir[maxn], w[maxn];
Graph T;
void dfs(Graph &G, ll u, ll fa)
{
ll ch=0;
s[++*s]=u;
dfn[u]=low[u]=++tim;
forp(u,G)
{
auto v=G.to[p];
if(!dfn[v])
{
ch++;
e[v]=p;
dfs(G,v,u);
low[u]=min(low[u],low[v]);
if(low[v]==dfn[u])
{
if(s[*s]==v)
{
ll W;
forp(u,G)if(G.to[p]==s[*s])W=G.w[p];
T.adde(v,u,W);
T.adde(u,v,W);
--*s;
}
else
{
tot++;
forp(u,G)
if(G.to[p]==s[*s])
w[tot].emb(G.w[p]);
for(ll x=0;x!=v;--*s)
{
x=s[*s];
cir[tot].emb(x);
ll bk=w[tot].back();
w[tot].emb(bk+G.w[e[x]]);
}
cir[tot].emb(u);
ll i; rep(i,0,cir[tot].size()-1)
{
ll W=min((ll)w[tot].at(i),w[tot].back()-w[tot].at(i));
T.adde(n+tot,cir[tot].at(i),W);
T.adde(cir[tot].at(i),n+tot,W);
}
}
}
}
else low[u]=min(low[u],dfn[v]);
}
}
void build(Graph &G, ll N)
{
ll i;
for(i=1;i<=N;i++)dfn[i]=low[i]=0;
T.clear(N*2);
*s=tim=tot=0;
n=N;
for(i=1;i<=N;i++)if(!dfn[i])dfs(G,i,-1);
}
}cstree;
struct Doubling_LCA
{
int f[maxn][maxk+1], depth[maxn], dist[maxn];
void clear(int n){for(int i=1;i<=n;i++)depth[i]=0, cl(f[i]);}
void dfs(Graph &G, int pos, int pre)
{
for(auto k=1;(1<<k)<=depth[pos];k++)f[pos][k]=f[f[pos][k-1]][k-1];
for(auto p(G.head[pos]);p;p=G.next[p])
if(G.to[p]!=pre)
{
f[G.to[p]][0]=pos;
depth[G.to[p]]=depth[pos]+1;
dist[G.to[p]]=dist[pos]+G.w[p];
dfs(G,G.to[p],pos);
}
}
void run(Graph &G, int root)
{
depth[root]=1;
dfs(G,root,0);
}
int q(int x, int y)
{
if(depth[x]<depth[y])swap(x,y);
for(auto k(maxk);~k;k--)
if(depth[f[x][k]]>=depth[y])
x=f[x][k];
if(x==y)return x;
for(auto k(maxk);~k;k--)
if(f[x][k]!=f[y][k])
x=f[x][k], y=f[y][k];
return f[x][0];
}
int jp(int x, int b)
{
for(auto k=0;k<=maxk;k++)
if(b&(1<<k))x=f[x][k];
return x;
}
}db;
ll n, m, q;
unordered_map<ll,ll> tb[maxn];
int main()
{
ll i, u, v, w, j;
n=read(), m=read(), q=read();
rep(i,1,m)
{
u=read(), v=read(), w=read();
G.adde(u,v,w), G.adde(v,u,w);
}
cstree.build(G,n);
db.run(cstree.T,1);
rep(i,1,cstree.tot)
rep(j,0,cstree.cir[i].size()-1)
tb[i][cstree.cir[i].at(j)]=j;
while(q--)
{
u=read(), v=read();
auto lca = db.q(u,v);
if(lca<=n)
{
printf("%lld\n",db.dist[u]+db.dist[v]-2*db.dist[lca]);
}
else
{
ll x=db.jp(u,db.depth[u]-db.depth[lca]-1), y=db.jp(v,db.depth[v]-db.depth[lca]-1);
ll p1=tb[lca-n][x], p2=tb[lca-n][y];
ll dlt = abs( cstree.w[lca-n].at(p1) - cstree.w[lca-n].at(p2) ), ans = min( dlt, cstree.w[lca-n].back() - dlt );
ans += db.dist[u] - db.dist[x] + db.dist[v] - db.dist[y];
printf("%lld\n",ans);
}
}
return 0;
}