BZOJ2125 最短路 圓方樹

2021-08-21 06:00:30 字數 2557 閱讀 1822

思路:

關於靜態仙人掌的問題,建立出圓方樹比較好求解。求出來之後處理每個圓點方點的情況,原來的是樹邊直接處理,環邊的話方點連向環上每個圓點一條邊,權值為每個節點到環的父親節點的最短距離。然後倍增預處理,查詢的時候,如果lc

a lca

是圓點直接輸出,否則兩個點是環上的點,處理他們在環上的最短距離即可。

為啥我的**都這麼慢啊。。。加了輸入輸出掛還900ms。。別人的什麼都不做都400ms。。。

#include

typedef

long

long ll;

const

int maxn = 1e5 + 10;

using

namespace

std;

struct edge

};int n, m, t, kase = 1, q;

vector

g[maxn], bcc[maxn], w[maxn];

int dp[maxn][2], dfn[maxn], dfs_clock, tot;

int pre[maxn], bccno[maxn], bcc_cnt, stk[maxn][2];

vector

et[maxn], ew[maxn];

int rec_cost[maxn], deep[maxn], ds[maxn];

stack

s;const

int hash_size = 1e6 + 10;

int head[hash_size], nxt[hash_size], vt[hash_size], tt[hash_size], key[hash_size], top;

int anc[maxn][15], cost[maxn][15];

void insert(int u, int t, int val)

int ask(int u, int t)

return0;}

int dfs(int u, int fa)

if(bccno[x.u] != bcc_cnt)

if(x.u == u && x.v == v) break;

}if(bcc[bcc_cnt].size() == 2) else else

dis += rec_cost[i];}}

}} else

if(pre[v] < pre[u] && v != fa)

}return lowu;

}const

int max = 10000;

char buf[max], *ps = buf, *pe = buf + 1;

inline

void rnext()

template

inline

bool in(t &ans) while(!isdigit(*ps) && ps != pe);

if(ps == pe) return

false;//eof

do while(isdigit(*ps) && ps != pe);

ans *= f;

return

true;

}void dfs(int x, int fa, int co, int d)

else

break;

}for(int i = 0; i < et[x].size(); i++)

}inline

int query(int x, int y)

if(x == y) return ans;

for(int i = 14; i >= 0; i--)

if(anc[x][0] > n) else ans += cost[x][0] + cost[y][0];

return ans;

}char bufout[max], outtmp[50],*pout = bufout, *pend = bufout + max;

inline

void write()

inline

void out_char(char c)

inline

void out_str(char *s)

}template

inline

void out_int(t x)

if(x < 0) x = -x,out_char('-');

int len = 0;

while(x)

outtmp[len] = 0;

for(int i = 0, j = len - 1; i < j; i++,j--) swap(outtmp[i],outtmp[j]);

out_str(outtmp);

}int main()

tot = n;

memset(anc, -1, sizeof anc);

dfs(1, -1); dfs(1, -1, 0, 1);

while(q--)

write();

return

0;}

bzoj2125 圓方樹 最短路

description 給乙個n個點m條邊的連通無向圖,滿足每條邊最多屬於乙個環,有q組詢問,每次詢問兩點之間的最短路徑。input 輸入的第一行包含三個整數,分別表示n和m和q 下接m行,每行三個整數v,u,w表示一條無向邊v u,長度為w 最後q行,每行兩個整數v,u表示一組詢問 output ...

BZOJ2125最短路 初識圓方樹

sto sto sto yyb orz orz orz bzoj2125 非許可權 不知道圓方樹應該歸在資料結構還是圖論,先放在這裡,圓方樹,對於狹義圓方樹,可以解決一類仙人掌的問題。對於仙人掌的定義,通俗來講就是對於乙個無向圖每一條邊有且僅僅被乙個環所包含 最後看起來就像是乙個乙個環圈圈,最終ju...

BZOJ2125 最短路 圓方樹 倍增LCA

最短路 做法 本題需要用到圓方樹來處理仙人掌。題目中所給的圖稱作仙人掌圖,即每條邊至多在乙個環上的圖。對於這種圖,我們一般把它轉化成樹後,將樹上的某些演算法修改後來解決仙人掌上的問題。常用的一種轉化方式就是圓方樹。在圓方樹中,乙個圓點表示原仙人掌中就有的點,而乙個方點表示乙個點雙連通分量 環 並且乙...