


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uang_spoj1674解題石家莊二中 賈Jia Zhihao From No.2 Midd le School,In Shijizhuang, Of Hebei Province題目大意設(shè)圖 G 有 N 個點,每個點恰好向外連一條有向邊,現(xiàn)在讓你找出最小的點集S,使得對于任何不屬于 S 的點,有向邊一定連向?qū)儆?S 的點。解題方法首先, 以單獨考慮每可以發(fā)現(xiàn),有向圖的連通分支之間是相互沒有影響的,所以通分支。可對于每通分支,它的結(jié)構(gòu)都大致如下圖??梢灾灰獙D中的環(huán)上刪去一個點,
2、那么原圖就變成了一棵樹,且每個點的有向邊恰好連向他的父節(jié)點。于是不難想到:可以將圖轉(zhuǎn)化成樹形 DP 來做。第 1 頁 共 8 頁IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uang方法如下:對于每通分支,任意找環(huán)上的一條邊,則他兩端的點至少有一個在S 中,枚哪個點在 S 中,之后可以去掉枚舉的點向外連的有向邊,該連通分支轉(zhuǎn)化成了樹。下面兩幅圖為兩種情況。對于每一種情況,用樹形 DP 計算|S|的最小值。子-右兄弟的二叉表示法表示,用lch(v) 表示v 的先將多叉樹用子, rch(v) 表示 v 的右兒子。定義dp(v,0)
3、 表示v 的父親節(jié)點屬于 S 時, v 在二叉表示的子樹中,最少有幾個節(jié)點屬于 S;定義dp(v,1) 表示 v 的父親節(jié)點不屬于 S 時, v 在二叉表示的子樹中,最少有幾個節(jié)點屬于 S;則dp(lch(v),0) dp(rch(v),0) 1dp(lch(v),1) dp(rch(v),0)dp(v,0) min dp(lch(v),0) dp(rch(v),1) 1dp(lch(v),1) dp(rch(v),1)dp(v,1) dp(lch(v),0) dp(rch(v),1) 1dp(0,0) 0dp(0,1) 0特別的,于是,可以通過這個方法求出每通分支的 min|S|,相加起來就
4、是最終。總結(jié)第 2 頁 共 8 頁IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uang要發(fā)現(xiàn)題目的特點:圖 G 有 N 個點,每個點恰好向外連一條有向邊,則圖 G 的每通分支有且僅有一個環(huán)。多叉樹的子-右兄弟的二叉表示法。用 DP解決樹。附代碼第 3 頁 共 8 頁IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uang第 4 頁 共 8 頁PROGRAM spoj; CONSTmaxnum=60000000; VARlast,pre,next,fa,lch,
5、rch:array1.100000of long; d:array0.100000,0.1of long; c:array0.100000of record x,y:long;end; k1,k2,ans,i,numtest,test,n,m:long;Function find(v:long):long;Beginwhile favfafav do fav:=fafav; exit(fav);End;Procedure build(root:long);Vari:long;Begini:=lastfind(root); while i0 do beginlchi:=0;rchi:=0; di
6、,0:=-1;di,1:=-1;i:=prei; end;whilei:=lastfind(root); while i0 do beginif iroot then beginif lchnexti=0 then lchnexti:=i else beginrchi:=lchnexti;lchnexti:=i; end;elseend; i:=prei;IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uang第 5 頁 共 8 頁end;while End;Function dp(v,c:long):long;Vari,k1,
7、k2:long;Beginif dv,c-1 then dp:=dv,c else if v=0 then dp:=0else begin dv,c:=maxnum;if(c=0)then dv,c:=dp(lchv,1)+dp(rchv,0)+1 else beginif dp(rchv,0)dp(rchv,1) then k1:=dp(rchv,0) else k1:=dp(rchv,1);if dp(lchv,0)dp(lchv,1)+1 then k2:=dp(lchv,0) else k2:=dp(lchv,1)+1;dv,c:=k1+k2; end;else dp:=dv,c;en
8、d;else End;BEGINread(numtest);for test:=1 to numtest do begin read(n);m:=0;for i:=1 to n do fai:=i; fillchar(last,sizeof(last),0); for i:=1 to n do beginread(nexti);if find(i)find(nexti) then fafind(i):=find(nexti) else begininc(m);cm.x:=i;cm.y:=nexti; end;elseend;for iIOI2009by Jia Zhihao from No.2
9、 Middle School in Shijiazh uang附英文原題SPOJ Problem Set (classical)1674. The ExploProblem code: EXPLOSNegabyand began calm and quietly as any other day. Some people wentThe day of 6.XII.2003to work, some - to school, some - to store to buy food. Drivers were traditionally stuckedrafficjams, drinking co
10、ffee and reading morning newspr. Suddenly the regularity of this day wasdisturbed by huge explo began to run away in panic.They blew up the embassy of Bajtocja! - somebody cried. Everybodyworks pretty goodegabyand andradiocars appeared near the embassy only fewseconds after the explo. All the people
11、 near the embassy were detained. Some of these people aretheanizers of the explo, but the others could by just occaal witnesses. During thetestification eachnamed exactly one petrator. It is known,t if a man is notrpetrator,n he always says the truth (he havent a reason to, have he?). However, petra
12、tors want tomake the work of themore difficult, sorpetrator can name anyduring thetestification (even himself).第 6頁 共 8 頁for i:=1 to n do begin prei:=lastfind(i);lastfind(i):=i;end;for ians:=0;for i:=1 to m do begin build(ci.x);k1:=dp(lchci.x,1)+1; build(ci.y);k2:=dp(lchci.y,1)+1;if k1k2 then ans:=a
13、ns+k1 else ans:=ans+k2; end;for iwrin(ans); end;for testEND.IOI2009by Jia Zhihao from No.2 Middle School in Shijiazh uangThemen arehe very hard situation. They should arrest some group of potential petrators,but it is difficult to determine who is guilty and who is not from the dahey have. There exi
14、sts manygroups of potential petrators,t dont contradict to any of the testimonies. Themen want toarrest as small innocent people assible. So they would like to choose the group with minimal number of people.Write a programt, given the number of detained people and their testimonies, will determine t
15、henumber of peoplehe smallest group of potential p testimonies.etrators,t dont contradict to theInputTheline of the inpontains a singleeger T, the number of testcases (1=T=10).line of each testcase containseger number N (2 = N = 100000), equal to the number ofdetained people (the people are numbered
16、 from 1 to N). The i-th of the following N lines contain oneeger numbi (1 = Pi = N). Heris the man whom i-th man testified to be guilty.OutputThe output should consist of T lines, containing oneeger number for each testcase - the number of t dont contradict to the testimonies.peoplehe smallest group of potential petrators,ExleInput:1
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 幼兒書包的課件
- 散打說課課件
- 2025年下半年四川省疾病預防控制中心招聘工作人員擬聘人員(一)筆試歷年典型考題及考點剖析附帶答案詳解
- 新課標過關(guān)活動方案
- 春節(jié)國網(wǎng)公司活動方案
- 數(shù)學小論文活動方案
- 旅游文化公司策劃方案
- 春節(jié)家庭創(chuàng)意活動方案
- 春節(jié)后公司活動方案
- 教育銷售活動方案
- 2025-2030中國功率半導體器件市場格局與投資方向報告
- 統(tǒng)編版五年級升六年級語文暑期銜接《課外閱讀》專項測試卷及答案
- 2025年 煙草陜西公司招聘考試筆試試題附答案
- 2025年老年教育課程設(shè)置:藝術(shù)修養(yǎng)與審美教學探索報告
- 2025年河北高考真題化學試題+解析(參考版)
- 護理事業(yè)十五五發(fā)展規(guī)劃(2026-2030)
- 2025至2030中國數(shù)字出版行業(yè)產(chǎn)業(yè)運行態(tài)勢及投資規(guī)劃深度研究報告
- 2025至2030中國中藥材種植行業(yè)運作模式與競爭格局分析報告
- 武漢大學2020年強基計劃物理試題(原卷版)
- 2025年隨州國投集團公開招聘42名工作人員筆試參考題庫附帶答案詳解
- 2025泰和安消防產(chǎn)品選型手冊
評論
0/150
提交評論