SlideShare una empresa de Scribd logo
1 de 48
Codeforces #400
D. The Door Problem
satanic @satanic0258
🔓
🔒
🔓
A B C
以下の例(サンプル2)を例にとって解説していきます。
なお今後、下の図において、
・黒は未決定の状態
・橙はオンにした状態
・青はオフにした状態
を表します。
a
c
b
🔓
🔒
🔓
A B C
まずAを押した場合、Aは全てのドアに繋がるため、
全てのドアの状態が反転します。
a
c
b
初期状態
🔒
🔓
🔒
A B C
まずAを押した場合、Aは全てのドアに繋がるため、
全てのドアの状態が反転します。
a
c
b
初期状態
🔒
🔓
🔒
A B C
まずAを押した場合、Aは全てのドアに繋がるため、
全てのドアの状態が反転します。
すると、aはオフになってしまいました。
aをオンにするにはCもオンである必要があります。
a
c
b
初期状態
🔒
🔓
🔒
A B C
まずAを押した場合、Aは全てのドアに繋がるため、
全てのドアの状態が反転します。
すると、aはオフになってしまいました。
aをオンにするにはCもオンである必要があります。
Cをオンにしました。
それに伴って、ドアの状態は…
a
c
b
初期状態
🔓
🔓
🔓
A B C
まずAを押した場合、Aは全てのドアに繋がるため、
全てのドアの状態が反転します。
すると、aはオフになってしまいました。
aをオンにするにはCもオンである必要があります。
Cをオンにしました。
それに伴って、ドアの状態は次のようになります。
a
c
b
初期状態
🔓
🔓
🔓
A B C
今、考慮していないcの状態も変わりましたが、
結果的にcも開いている状態になったのでよしとします。
a
c
b
初期状態
🔓
🔓
🔓
A B C
これで、全てのドアが開いている状態になりました。
Bの状態がまだ決まっていませんが、
bは片方が決まっていて開いているため、
bに繋がっているBはオフとなります。
a
c
b
初期状態
🔓
🔓
🔓
A B C
これで、全てのドアが開いている状態になりました。
Bの状態がまだ決まっていませんが、
bは片方が決まっていて開いているため、
bに繋がっているBはオフとなります。
よって、ドアを全て開いた状態にするスイッチの組み合わせ
があることが分かりました。
a
c
b
初期状態
この問題を、グラフ上で解いてみましょう。
初期状態
この問題を、グラフ上で解いてみましょう。
2-SATをグラフに帰着するとき、オン/オフの状態を
決めるものそれぞれについて、オンの頂点とオフの頂点を
作ります。
初期状態
A
A
B
B
C
C
この問題を、グラフ上で解いてみましょう。
2-SATをグラフに帰着するとき、オン/オフの状態を
決めるものそれぞれについて、オンの頂点とオフの頂点を
作ります。
ここで、オンの頂点はそのまま、
オフの頂点には上にバーを付けることにします。
初期状態
A
A
B
B
C
C
オン→
オフ→
では、このグラフに辺を張っていきます。
初期状態
A
A
B
B
C
C
オン→
オフ→
では、このグラフに辺を張っていきます。
まずAをオンにすると、a,cを介して繋がるCもオンにする
必要がありました。
そのため、A→Cと辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
では、このグラフに辺を張っていきます。
まずAをオンにすると、a,cを介して繋がるCもオンにする
必要がありました。
そのため、A→Cと辺を張ります。
また、bを介して繋がるBはオフにする必要がありました。
そのため、A→Bと辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、各頂点について辺を張っていきます。
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、各頂点について辺を張っていきます。
Bをオンにすると、Aはオフにする必要があります。
従って、B→Aと辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、各頂点について辺を張っていきます。
Bをオンにすると、Aはオフにする必要があります。
従って、B→Aと辺を張ります。
Cをオンにすると、Aはオンにする必要があります。
しかし、これについては既に調べてあるため、
わざわざ二重に辺を張る必要はありません。
初期状態
A
A
B
B
C
C
オン→
オフ→
Aをオフにすると、Cもオフにする必要があります。
従って、A→Cと辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
Aをオフにすると、Cもオフにする必要があります。
従って、A→Cと辺を張ります。
Bをオフにした場合、Cをオフにした場合については、
既に調べられている状態になっているため、
グラフ上に変化はありません。
初期状態
A
A
B
B
C
C
オン→
オフ→
こうして全ての辺を張ることが出来ました。
初期状態
A
A
B
B
C
C
オン→
オフ→
こうして全ての辺を張ることが出来ました。
このグラフを見ると、つじつまが合うようなスイッチの
組み合わせは全て同じ連結成分になっており、
逆に矛盾が発生するような頂点同士は連結になっていない
ことが分かります。
初期状態
A
A
B
B
C
C
オン→
オフ→
こうして全ての辺を張ることが出来ました。
このグラフを見ると、つじつまが合うようなスイッチの
組み合わせは全て同じ連結成分になっており、
逆に矛盾が発生するような頂点同士は連結になっていない
ことが分かります。
では、ある頂点と連結になっている頂点を全て抜き出せば
それが正しい組み合わせとなるのでしょうか?
初期状態
A
A
B
B
C
C
オン→
オフ→
そうとは限りません。
右の図のように、連結成分の中には
「AがオンならばAをオフにしなければならない」
といったおかしな部分が出ることがあります。
初期状態
A
A
A
A
B
B
C
C
オン→
オフ→
そうとは限りません。
右の図のように、連結成分の中には
「AがオンならばAをオフにしなければならない」
といったおかしな部分が出ることがあります。
逆に言えば、グラフにこのような矛盾が無ければ
正しいスイッチの組み合わせがあるとわかります。
初期状態
A
A
A
A
B
B
C
C
オン→
オフ→
今回の例では、 AとA、 BとB、 CとC、と全ての頂点で、
別々の連結成分に属していることが分かります。
初期状態
A
A
B
B
C
C
オン→
オフ→
今回の例では、 AとA、 BとB、 CとC、と全ての頂点で、
別々の連結成分に属していることが分かります。
よって、グラフからこの例では正しいスイッチの
組み合わせが存在することが分かりました。
初期状態
A
A
B
B
C
C
オン→
オフ→
🔓
🔒
🔓
A
B
C
では、今度は以下の例(サンプル1)について
見ていきます。
a
c
b
初期状態
では、今度は以下の例(サンプル1)について
見ていきます。
スイッチ数は先ほどと同じなため、グラフの初期状態も
同じとなります。
初期状態
A
A
B
B
C
C
オン→
オフ→
まずAをオンにした場合について考えます。
Aはaを介してBに、またcを介してCに繋がっています。
初期状態
A
A
B
B
C
C
オン→
オフ→
まずAをオンにした場合について考えます。
Aはaを介してBに、またcを介してCに繋がっています。
従って、AをオンにするとBとCもオンにする必要があるため、
A→B、A→Cと辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
まずAをオンにした場合について考えます。
Aはaを介してBに、またcを介してCに繋がっています。
従って、AをオンにするとBとCもオンにする必要があるため、
A→B、A→Cと辺を張ります。
同様に、BをオンにするとAはオンに、Cはオフにする必要が
あるため、B→A、B→Cと辺を張ります。
(A-B間は既に辺が張ってあるため実際には張りません)
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、A、
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、A、B、
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、A、B、Cについても辺を張ります。
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、A、B、Cについても辺を張ります。
こうして、全ての辺を張ることが出来ました。
では、どの頂点が連結になっているでしょうか。
初期状態
A
A
B
B
C
C
オン→
オフ→
同様に、C、A、B、Cについても辺を張ります。
こうして、全ての辺を張ることが出来ました。
では、どの頂点が連結になっているでしょうか。
ある頂点から初めて辺を順に辿っていくと分かりますが、
この例では、全ての頂点が連結となっています。
初期状態
A
A
B
B
C
C
オン→
オフ→
特に、AとAが同じ連結成分に属しているため、
この例では正しいスイッチの組み合わせが存在しない
ことが分かります。
初期状態
A
A
B
B
C
C
オン→
オフ→
特に、AとAが同じ連結成分に属しているため、
この例では正しいスイッチの組み合わせが存在しない
ことが分かります。
このように、グラフに帰着してこの問題を
解くことが出来ます。
初期状態
A
A
B
B
C
C
オン→
オフ→
実際にプログラムを実装する際には、
・グラフに辺を追加する
・ある2頂点が連結であるかを調べる
というクエリをこなすデータ構造である、素集合森
(通称Union-Find)を使うと良いでしょう。
初期状態
A
A
B
B
C
C
オン→
オフ→
その後、AとAが同じ連結成分に属しているか否かを
調べることで組み合わせの存在を調べます。
※注意※
しかし、この問題で現れる論理式は、
・同値 ( ⇔ )…両方押すか押さないか
・排他的論理和(XOR)…どちらか片方だけ押す
の積となっています。
おわり

Más contenido relacionado

Último

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024koheioishi1
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationYukiTerazawa
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptyuitoakatsukijp
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料Takayuki Itoh
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2Tokyo Institute of Technology
 
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ssusere0a682
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ssusere0a682
 

Último (7)

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScript
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

(ブログ用)Codeforces #400 D. The Door Problem