洛谷P3389兼2019 09 21日常總結

2021-09-27 07:59:42 字數 3295 閱讀 6270

【題意】:

小倉鼠的和他的基(mei

meime

i)友(zizi

zi)s ug

ar

sugar

suga

r住在地下洞穴中,每個節點的編號為1

n1~n

1n。地下洞穴是乙個樹形結構。這一天小倉鼠打算從從他的臥室(a

aa)到餐廳(b

bb),而他的**同時要從他的臥室(c

cc)到圖書館(d

dd)。他們都會走最短路徑。現在小倉鼠希望知道,有沒有可能在某個地方,可以碰到他的**?

小倉鼠那麼弱,還要天天被zzq

zzqzz

q大爺虐,請你快來救救他吧!

【思路】:

題意即讓我們判斷一顆樹上的兩條路徑(a,

b)

(a,b)

(a,b)和(c,

d)

(c,d)

(c,d

)有沒有交點,這有兩中方法:

方法1 :1:

1:當兩條路徑長度和≥

\geq

≥兩條路徑起點之距離+

++兩條路徑終點之距離,則兩條路徑有交點(求(x,

y)

(x,y)

(x,y

)距離公式:dep

[x]+

dep[

y]−2

×dep

[lca

(x,y

)]

dep[x]+dep[y]-2 \times dep[lca(x,y)]

dep[x]

+dep

[y]−

2×de

p[lc

a(x,

y)])

方法2 :2:

2:當兩條路徑有交點時,其中一條路徑的lca

lcalc

a在另一條路徑上(判斷乙個點x

xx是否在路徑(s,

t)

(s,t)

(s,t

)上:dep

[x]≥

dep[

lca(

s,t)

]dep[x]\geq dep[lca(s,t)]

dep[x]

≥dep

[lca

(s,t

)]&&( lc

a(s,

x)==

x∣∣l

ca(t

,x)=

=t

)(lca(s,x)==x||lca(t,x)==t)

(lca(s

,x)=

=x∣∣

lca(

t,x)

==t)

[code of 方法1]:

#include

using

namespace std;

const

int n=

100100

;struct nodee[n<<1]

;int h[n]

,tot;

inline

void

add(

int a,

int b)

;h[a]

=tot;

e[++tot]

=(node)

;h[b]

=tot;

}int dep[n]

,f[n][23

];void

dfs_init

(int u,

int fa)

}inline

intlca

(int x,

int y)

if(x==y)

return x;

//重要

for(

int i=

20;i>=

0;i--)if

(f[x]

[i]!=f[y]

[i])

return f[x][0

];}inline

intdis

(int a,

int b)

inline

bool

check

(int a,

int b,

int c,

int d)

#define gc getchar()

#define g(c) isdigit(c)

inline

intread()

int n,i,p,a,b,c,d;

intmain()

return0;

}

[code of 方法2]:

#include

using

namespace std;

const

int n=

100100

;struct nodee[n<<1]

;int h[n]

,tot;

inline

void

add(

int a,

int b)

;h[a]

=tot;

e[++tot]

=(node)

;h[b]

=tot;

}int dep[n]

,f[n][22

];void

dfs_init

(int u,

int fa)

}inline

intlca

(int x,

int y)

if(x==y)

return x;

//重要

for(

int i=

20;i>=

0;i--)if

(f[x]

[i]!=f[y]

[i])

return f[x][0

];}inline

bool

check

(int a,

int s,

int t)

#define gc getchar()

#define g(c) isdigit(c)

inline

intread()

int n,i,p,a,b,c,d;

intmain()

return0;

}

洛谷P3389 模板 高斯消元法

題目描述 給定乙個線性方程組,對其求解。輸入樣例 1 3 1 3 4 5 1 4 7 3 9 3 2 2 輸出樣例 1 0.97 5.18 2.39 分析 對於第i個方程用它的第i個元消掉第i 1個方程後面每個方程的第i個元,如果第i個方程的i消為0則此方程組有無窮解或無解,這樣到最後乙個方程只有乙...

洛谷P3389 模板 高斯消元法

題目傳送門 題目分析 時隔多年 月 我終於入了高消這個坑。表示掛一發模板就跑,以後複習用。具體細節什麼的還是自己yy吧,有益身心健康。code include include include include include include include include using namespac...

洛谷 P3389 模板 高斯消元法

傳送門 給出乙個n nn元一次的方程,對其求解 大概思路就是不斷用某個式子去抵消其他式子的某一項未知數的係數,這樣進行n 1 n 1n 1,我們就能得到aix i bi a ix i b i ai xi bi 直接求解然後將x ix i xi 向其他式子帶入,乙個個求出其他未知數 include i...