BJ模擬(2) D2T3 路徑規劃

2021-07-28 06:24:11 字數 1856 閱讀 7815

路徑規劃

題目背景:

thoj27

分析:這道題我打了乙個暴力,用樹鏈剖分實現不知道為什麼前兩個點都沒有過,但是別人完全不優化的暴力竟然都過了,這樣我很不服啊,不開心

qnq,本來呢,這道題敲乙個無腦的點分是可以卡卡常數過的,複雜度

o(nlog2n)

,但是正如某學長所說,這樣非常的不優雅,那我們考慮一些優雅些的做法,首先我們這裡給出乙個結論。對於樹上的兩個不相交的點集s,

t,若集合

s中的最長鏈(即直徑)是

sx à

sy,集合

t內的最長鏈(直徑)是

tx à

ty 那麼如果我們合併這兩個點集,那麼現在的合併的點集的最長鏈(直徑)一定是,

sx à

sy, sx 

àtx, sx 

àty, sy 

àtx, sy 

àty, tx àty

這6條中的乙個,那現在問題就簡單了,我們將樹邊按照邊權由大到小直接排序,對於然後維護連通塊直徑,每一次將邊兩端的連通塊合併,然後更新新的直徑就可以了,這樣的複雜度為

o(nlogn)

source:

#include 

#include

#include

#include

#include

#include

#include

#include

using namespace std; inline char read() return *s++; } template

inline bool r(t &x) for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int out_len = 1024 * 1024; char obuf[out_len], *oh = obuf; inline void writechar(char c) template

inline void w(t x) } inline void flush() const int maxn = 300000 + 10; struct data data() {} bool operator < (const data &a) const } side[maxn]; struct node node() {} }; vector

edge[maxn]; struct tree tree[maxn]; struct comp comp() {} } dis[maxn]; int ind, n, x, y, z; int father[maxn], pos[maxn]; inline void addedge(int u, int v, int w) inline void dfs1(int cur, int fa, int w) } inline void dfs2(int cur, int top) /*這裡的樹鏈剖分只是用來求lca*/ inline int query_length(int u, int v) if (tree[u].dep > tree[v].dep) return tree[p].dis + tree[q].dis - 2 * tree[v].dis; else return tree[p].dis + tree[q].dis - 2 * tree[u].dis; } inline void readin() } inline int getfather(int x) inline bool update(long long &x, int y) inline void work() w(ans); } int main()

BJ模擬(1) D2T3 鏈上求和

鏈上求和 題目背景 分析 這個題,我真的很想呵呵,當時聽滿分神犇講了一次,然後學長講了一次,然後默默的自己看了 n次,然後在給別人講了兩次,才終於算是基本懂了 現在讓我寫,恐怕還是只有呵呵 我們先一步步進行考慮,首先對於一條鏈上的某乙個點,他在這一條鏈上的統計次數,是這條鏈上比它的權值小的點的個數加...

BJ模擬(2) D2T1 簡單粗暴的題目

簡單粗暴的題目 題目背景 thoj25 分析 本題一看真的很粗暴,在一想也的確非常粗爆,那麼我們就用粗暴的方法,首先,我們發現直接暴力,我們需要算n2 個數的k 次方,顯然複雜度上是不能接受的,我們發現對於第i個 answer 其實就是,s i k s i s i 1 k s i s i 1 s i...

snoi省選模擬賽 day3t1 路徑規劃

kac在數軸上有n片西瓜地。第 i片的座標是x i 注意 x並沒有排序 任意兩片西瓜地座標不同。有一天他要給這n片西瓜地澆水。初始他在x 1 的位置。他必須按1.n 的順序澆水,也就是說,必須先去x 1 再去x 2 最後到x n 他可以沿著座標軸正方向或者負方向走 給西瓜地澆水不需要花費時間。每走1...