$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Grundy
Search
yosupo
November 18, 2016
Technology
1
3.1k
Grundy
yosupo
November 18, 2016
Tweet
Share
More Decks by yosupo
See All by yosupo
一般グラフの最大マッチング
yosupo
3
2.1k
Other Decks in Technology
See All in Technology
なぜフロントエンド技術を追うのか?なぜカンファレンスに参加するのか?
sakito
8
1.8k
AI開発の定着を推進するために揃えるべき前提
suguruooki
1
470
その設計、 本当に価値を生んでますか?
shimomura
2
120
Capture Checking / Separation Checking 入門
tanishiking
0
110
All About Sansan – for New Global Engineers
sansan33
PRO
1
1.3k
今すぐGoogle Antigravityを触りましょう
rfdnxbro
0
250
Claude Code はじめてガイド -1時間で学べるAI駆動開発の基本と実践-
oikon48
37
18k
ページの可視領域を算出する方法について整理する
yamatai1212
0
140
セキュリティAIエージェントの現在と未来 / PSS #2 Takumi Session
flatt_security
2
400
How native lazy objects will change Doctrine and Symfony forever
beberlei
1
320
シンプルを極める。アンチパターンなDB設計の本質
facilo_inc
0
570
MS Ignite 2025で発表されたFoundry IQをRecap
satodayo
2
190
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8k
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Unsuck your backbone
ammeep
671
58k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
It's Worth the Effort
3n
187
29k
Speed Design
sergeychernyshev
33
1.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
990
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Transcript
Grundy @yosupot
ҎԼͷΛߟ͑·͢ NMลͷDAG͕༩͑ΒΕΔɻ1ʹ͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹۨΛಈ͔͢ɻ ۨลͰͭͳ͕ͬͨʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ
ղ๏ dfsͰղ͘ ಈ͔ͤΔʹʮޙʯͱॻ͔Ε͕ͨ͋Εʮઌʯ ͳ͚Εʮޙʯͱॻ͘ 0ʹʮઌʯͱॻ͔ΕΕઌखඞউ ʮޙʯͱॻ͔ΕΕޙखඞউ
ྫ ޙ ޙ ઌ ઌ 1
ҎԼͷΛߟ͑·͢ NMลͷDAG͕Kछྨ༩͑ΒΕΔɻͦΕͧΕͷάϥϑͷ 1ʹ͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹɺͲΕ͔ͷάϥϑͷۨΛಈ͔͢ɻ ۨลͰͭͳ͕ͬͨʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ
ղ๏ ࣮ෳͷάϥϑ1ͭͷάϥϑʹ·ͱΊΒΕΔ (άϥϑ1ͷۨͷҐஔ, άϥϑ2ͷۨͷҐஔ, …, άϥϑKͷۨͷ Ґஔ)Λͱͯ͠దʹลΛషΔ ͜ͷάϥϑͰಉ͡Α͏ʹdfs͕Ͱ͖Δ ܭࢉྔO((N+M)^K)
O((N+M)^K)ʂʁ N=M=5000ͱͯ͠(N+M)^Kʁ K=1 → 10000 K=2 → 1,000,000,000 K=3 →
10,000,000,000,000 K=100 → 10^400
ͬͱޮྑ͘ܭࢉͰ͖ͳ͍͔ʁ ·ͣK=2ͷ߹Λߟ͑Δ
ߟ 2ͭͷάϥϑ͕ͲͪΒޙखউͪͳΒɺશମͰޙखউͪ 2ͭͷάϥϑ͕(ઌखউͪ + ޙखউͪ)ͳΒɺશମͰઌख উͪ ͩͬͨΒɺ(ઌखউͪ + ઌखউͪ)ͷ߹ʁ ࣮શମͰઌखউͪʹͳΔྫޙखউͪʹͳΔྫ࡞ΕΔ
Ұҙʹఆ·Βͳ͍ ʴ ʴ ʹ ʹ ઌखউͪ ޙखউͪ
Ͳ͏͢Δʁ άϥϑΛʮઌखউͪʯͱʮޙखউͪʯʹ·ͱΊΔ͜ͱͰ ͖ͳ͍ Ͱ2छྨఘΊΔͱͯ͠ɺԿछྨ͔ʹάϥϑΛྨ͢Δ ͜ͱͰ͖ͳ͍ͷ͔ʁ
(άϥϑA + άϥϑX)ͷউഊͱ(άϥϑB + άϥϑX)ͷউഊ ͕ɺXʹ͔͔ΘΒ͍ͣ࣌͠ɺάϥϑA,BΛʮຊ࣭తʹ͠ ͍ʯͱ͢Δ (ޙखউͪ+ޙखউͪ)ͱ(ޙखউͪ+ઌखউͪ)Ұҙʹఆ·Δ ͷͰɺޙखউͪͲ͏͠ʮຊ࣭తʹ͍͠ʯ ͳͷͰɺઌखউͪͲ͏͠ʮຊ࣭తʹ͍͠ʯͷͰʁͱ
Ծఆࣦͯ͠ഊͨ͠ͷ͕ࠓͷঢ়ଶ ઌखউͪͷάϥϑ1छྨʹ·ͱΊΔ͜ͱͰ͖ͳ͍ʂ
ͱ͜ΖͰ ͖ͬࣔͨ͞͠Α͏ʹɺҎԼͷ2ͭͷάϥϑ͘͠ͳ͍ ͳͥʁ
ͱ͜ΖͰ ࠨͷάϥϑޙखউͪʹ͔͠ߦ͚ͳ͍͕ɺӈͷάϥϑޙखউͪͱઌखউͪͷ ྆ํʹߦ͚Δ ޙखউͪͱઌखউ͕ͪ྆ํඋΘΓ࠷ڧʹݟ͑Δ(ӈͷάϥϑͷํ͕ࣗ༝͕ߴͦ ͏)
͡Ό͋ ͜ͷάϥϑߋʹ࠷ڧͳͷͰʁ
͡Ό͋ ͜ͷάϥϑߋʹ࠷ڧͳͷͰʁ
࣮ શDAGͲ͏͠ޓ͍ʹ͘͠ͳ͍͜ͱ͕ࣔͤΔ(TODO) ͳͷͰগͳ͘ͱ༗ݶछྨͰͳ͍
΄΅શDAGʁ શDAGಉ࢜ҟͳΔ͜ͱ͕Θ͔ͬͨ ͡Ό͋શDAGʹ1ݸΛՃ͑ͨͷʁ શDAGͷ͏͍͔ͪͭ͘ͷʹɺ1͔Βล͕ੜ͍͑ͯ ΔɺΑ͏ͳάϥϑ
ྫ͑ ͜Ε1͔Β1, 3ͷDAGʹߦ͚ΔΑ͏ͳάϥϑ
ྫ͑ ͜Ε1͔Β1, 3ͷશDAGʹߦ͚ΔΑ͏ͳάϥϑ ͜Ε2ͷશDAGͱ͍͠
ͳͥʁ 3ͷશDAGʹߦͬͯɺ༗རʹͳΔ͜ͱͳ͍ ͳͥͳΒɺ૬ख࣍ͷखͰ2ͷશDAGҠಈͰ͖ΔͨΊ ݁ہɺ1ͷશDAGߦ͔͘͠ͳ͍ͨΊɺ2ͷશDAGͱຊ࣭తʹ͍͠ ͜ͷลΛͬͯ༗རʹͳΔ͜ͱͳ͍
΄΅શDAG {a, b, c, …}ʹߦ͚Δͱ͢Δͱɺ=ʮ{a, b, c, …}ʹ ग़ͯ͜ͳ͍࠷খͷਖ਼ʯͷશDAGͱຊ࣭తʹ͍͠ ྫ͑{1,
3, 4, 5}ʹߦ͚ΔͳΒ=2 ͳ͔ͥͱ͍͏ͱɺ3,4,5ͷͲΕʹߦ͘ͷɺ࣍ͷखͰ૬खʹ2 ʹ͞ΕΔͷͰɺ༗རʹͳΕͳ͍
Ұൠʹ ઌड़ͷنଇΑΓؼೲతʹɺશͯͷάϥϑશDAGͷͲΕ͔ͱ ຊ࣭తʹ͍͠ =kͷશDAGͱຊ࣭తʹ͍͠άϥϑΛlevel (k-1)ͷάϥ ϑͱݺͿ (0-indexedͰͳ͍ͱ৭ʑ໘) άϥϑͷlevel࠶ؼతʹܭࢉͰ͖ΔͨΊɺO(N+M)ͰٻΊΒΕΔ
݁ہ (aͷશDAG + bͷશDAG)ͱมܗͰ͖Δ ͜Εͷඞউఆ؆୯(a = b͔൱͔) O(N+M)
K=3 K=3ͷ߹ʁ ࠷ॳͷ۪ղΛࢥ͍ग़͢ͱɺෳݸͷάϥϑ1ݸͷάϥϑʹ·ͱΊΒΕΔ ͦͯ͠ɺk=2ͳΒશͯͷάϥϑશDAGมܗͰ͖Δ͜ͱΛ͏ (άϥϑA + άϥϑB + άϥϑC)= {άϥϑA
+ (άϥϑBͱάϥϑCΛ·ͱΊͨͷ)}= (શDAG + άϥϑB + άϥϑC) Αͬͯk=2ͷ࣌ͱಉ͡نଇͰશDAGมܗͯ͠Α͍
K=4 K=4ͷ߹ʁ (άϥϑA + άϥϑB + άϥϑC + άϥϑD)= {άϥϑA+(άϥϑBͱάϥϑCͱάϥϑDΛ·ͱΊͨͷ)}=
(શDAG + άϥϑB + άϥϑC + άϥϑD)
݁ہ (શDAG + શDAG + … + શDAG)ʹมܗͰ͖Δ ͜ͷඞউఆʁ(࣮NimήʔϜͱ͍͏༗໊ͳܗͷήʔϜʹͳ͍ͬͯΔ…) (શDAG
+ શDAG + … + શDAG)= {(શDAGͱશDAGΛ·ͱΊͨͷ) + (K-2ݸͷશDAGΛ·ͱΊͨͷ)}= ʹมܗͰ͖Δ ͱ͜ΖͰɺK=2ͳͷͰ(શDAGͱશDAGΛ·ͱΊͨͷ)શDAGมܗͰ͖Δ ͭ·Γɺ(શDAGͱશDAGΛ·ͱΊͨͷ)ΛશDAGʹมܗͰ͖ΔͱɺK-1ʹؼ ணͰ͖Δ
શDAG + શDAG (શDAGͱશDAGΛ·ͱΊͨͷ)ͱ͍͠શDAGͷ ʁ ࣮ɺ(a+1)ͱ(b+1)Λ·ͱΊΔͱɺ(a xor b)+1 ͱ͘͠ͳΔ(TODOઆ໌) ͭ·Γɺlevel
aͱlevel bΛ߹͢Δͱɺlevel (a xor b)ʹͳΔ (͜Ε͕0-indexedʹͨ͠ཧ༝)
ղ๏ ͱ͍͏͜ͱɺlevel a + b + …ͳΒɺlevel (a xor b
xor …) ͱ͍͠ ͜Ε͕0͔Ͳ͏͔ผ͢Ε͑ O(K(N+M))ʂ