SolutionLTCManContestWord下载.docx
- 文档编号:16423342
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:12
- 大小:79.65KB
SolutionLTCManContestWord下载.docx
《SolutionLTCManContestWord下载.docx》由会员分享,可在线阅读,更多相关《SolutionLTCManContestWord下载.docx(12页珍藏版)》请在冰豆网上搜索。
n.SothereexistsC(n–1,k-1)waystochooseanotherk-1nodestomakeupconnectedcomponentC.SothereexistsC(n–1,k–1)*F(k)differentconnectedcomponentC.AnygraphsG(n-k)forremainingnodesareokay.Sotheansweris:
IttakesO(n^2)tocalculatethisformula.
利用补集转化思想,若不考虑连通性,则有n个点的任意图有G(n)=2^C(n,2)个.只要求出不连通图的个数就知道答案F(n).考虑含结点1的连通块C,|C|=k.由于不连通,有k<
n成立.则有C(n-1,k–1)种方法选择另k-1个结点与结点1组成C.所以有C(n-1,k–1)*F(k)种方法组成C.剩下的结点组成一个任意图即可.答案为:
复杂度为O(n^2).
[Reference]
[1]TheinformationfromtheOn-LineEncyclopediaofIntegerSequences:
A001187Numberofconnectedlabeledgraphswithnnodes.
[2]杨俊,<
<
PKU1737解题报告>
>
IOI2005国家集训队报告
ProblemB(POJ1738):
AnoldStoneGame
Therearen(n<
=50000)pilesofstonesinaline.Eachtimewecanmergetwoadjoiningpilestoanewpileuntilthereisonlyonepile.Minimizethesummationofthenumberofstonesinthenewpileaftermerging.
在开始有n(n<
=50000)堆的石头堆排成一行.每次可以合并相邻的两个石头堆成为一个新堆,可以得到这个新堆的石头数的分数.直到剩下一堆.找出一种合并计划,使分数最小,求这个最小分数.
ThisproblemisequivalenttoconstructanOptimalAlphabeticBinarySearchTree(OABST).AsthesameAsHuffmantree,minimizethesummationoftheweightedpathslengthsoftheexternalnodes(leaves).ThedifferenceisthattheexternalnodesinOABSTmustbeinorder(alphabetic),namelymergetheonlyadjoiningpileseachtime.ItcanbesolvedinO(n^2)bydynamicprogrammingafteroptimizingbythequadrilateralinequation.Butitisnotfastenoughforthisproblem.TheHu-TuckeralgorithmwiththecomplexityO(nlogn)willbeintroducedinthefollowing.Thisalgorithmhas3phases.Becausethisproblemdoesnotneedtooutputthemergingmethod,justtoimplementthePhase1isokay.
Phase1,Combination:
ThegoalofthisphaseistobuildanoptimalbinarytreeinwhichthedepthsoftheexternalnodesarethesameasthefinalOABST.
Theworksequenceisasequencewhichconsistsoftheinternalorexternalnodes.
Theinitialworksequenceofnodesconsistsofalltheexternalnodesinorder.
Duringeachiterationofthisphaseanewworkingsequenceisproducedbycombiningtwonodeswiandwjinthepreviousworkingsequenceintooneinternalnodewi+wjwhichreplacestheleftmostnodewiofthetwoandremovingtherightmostonewjfromthecurrentsequence.
Wecallthepairofthenodeswi,wjwhichiscombinedineachiterationtheLocalMinimumCompatiblePairs(LMCP).Ineachiteration,LMCPisunique.Itsatisfiesthefollowing4rules:
Rule
(1):
Inthecurrentworkingsequencenoexternalnodesoccurbetweenwiandwj.
Rule
(2):
Thecombinedweightw=wi+wjisminimum.
Rule(3):
Ifincaseoftie,thesubscriptioftheleftmostnodeswineedsminimized.
Rule(4):
Ifstillincaseoftie,thesubscriptjoftherightmostnodeswjneedsminimized.
Weusetherectangletodenoteanexternalnodeandthecircletodenoteaninternalnodeinthefollowing.
Forexample,n=5,w=(5,2,2,3,6).NoticethatinStep
(2),LMCP(5,3)spanstheinternalnode(2+2).
Theimplementingofthisphaseisthemostcomplexandthemostimportantalso.Wecanfindthattheorderofthecontinuousinternalnodesdoesnotmatter.NamelytheinternalnodesbetweenthetwoadjoiningexternalnodesaresimilartobeexecutedthegreedyalgorithmofHuffmantreeinpart.Inotherwords,externalnodesdividetheworksequenceintosomesegments,theinternalnodesbetweenthetwoadjoiningexternalnodesbuildsasetoftheinternalnodes(usethedashedcircletodenote).
Becausetheexternalnodeandthesetoftheinternalnodesoccursalternately,weuseablock(usethebigrectangletodenote)tocontaintheexternalnodeandthesetoftheinternalnodesintheright.Weusetwo-directedlinkstolinkthemup.
ListthecaseoftheoccurringoftheLMCP
LetBbethecurrentblock.
Case1:
TheLMCPistheexternalnodeinBandtheminimuminternalnodeinB.
AftercombiningtheLMCP,putthenewnodeintothesetofinternalnodesinAandunitethesetofinternalnodesinBintoA'
s.
Case2:
TheLMCPistheexternalnodeinBandtheexternalnodeinC.
AftercombiningtheLMCP,putthenewnodeintothesetofinternalnodesinAandunitethesetofinternalnodesinBandCintoA'
Case3:
TheLMCPistheminimumandsecondminimuminternalnodeinB.
AftercombiningtheLMCP,putthenewnodeintothesetofinternalnodesinB.
Case4:
TheLMCPistheminimuminternalnodeinBandtheexternalnodeinC.
AftercombiningtheLMCP,putthenewnodeintothesetofinternalnodesinBandunitethesetofinternalnodesinCintoB'
Thesetofinternalnodesneedstheoperationoffindingtheminimumandsecondminimumelementanduniting.ThenormalmergableheapsuchasLeftistTreeandPairHeapandFibonacciHeapareokay.
Ineachiteration,itisneededtofindtheLMCP.Becausetheblockcanbemerged,thereexiststhelargenumberoftheinsertinganddeletingoperation.Soweuseadeletablepriorityqueuetomaintainthepairsofthenodes.Therelationofthepairsofthenodesisthattheweightsummationofthepairsasthefirstkey,theblockIDasthesecondkeyandthenumberofthecaseasthethirdkey.
SothemaintainingofanycasecanbedoneinO(logn).
ThetotaltimeofthisphaseisO(nlogn).
Phase2,LevelAssignment:
ThegoalofthisphaseistogetthedepthofalltheexternalnodesintheoptimaltreewhichisconstructedinPhase1.InPhase1,recordthechildreninformationofeachnode(bothinternalandexternalnodes).Travelthetreebyrecursioncangetthedepths.
ThetotaltimeofthisphaseisO(n).
Phase3,Recombination:
TheinternalnodesinPhase1andPhase2isnotusedinthisphase.ThisphasebuildsanOABSTfromtheinitialsequenceofexternalnodesandtheirdepthscalculatedinPhase2.
Wearrangetheexternalnodesandtheirdepthstotheworksequence.
Duringeachiterationofthisphase,thepairofadjacentnodeswhichareatthesamemaximumdepthdarecombinedtoproduceanodeatdepthd–1andreplacetheleftmostoneofthetwoandremovingtherightmostone.Exactly,thepair(wi,wj)satisfies:
Thenodeswiandwjmustbeadjoiningintheworksequence.
Thedepthsdianddjmustbemaximizedamongalltheremainingnodes.
Ifincaseoftie,thesubscriptiofthenodewimustbeminimized.
Accordingtotherulesabove,thedepthsinthepair(wi,wj)mustbethesame.Actually,onlythedepthsproducedbyPhase1,2canbeexecutedthePhase3correctly.Notanydepthsassignmentcanbedone.
Useonequeueandonestacktoimplementthisalgorithm:
1.Putthewholeworksequencetothequeueinorder.Thestackisempty.
2.Ifthe2elementsatthetopofstackhaveequaldepthsd,thenpopthetwo,pushthenewnodesatdepthd–1producedbycombiningthetwo,andturnStep2;
elseturnStep3.
3.Ifthequeueisnon-empty,poptheheadofthequeueandpushitintothestack;
elsefinishthealgorithm.
“石子归并”问题等价于构造一棵OptimalAlphabeticBinarySearchTree(OABST).和Huffman树最优性要求一样,都是最小化外部结点(叶子)到根的带权路径总长.区别是OABST的外部结点要求有序.即每次合并要求石子是相邻的.可以用四边形优化后的动态规划在O(n^2)内解决,但对于本题来说是不够快的.下面介绍O(nlogn)的Hu-Tucker算法,该算法分为三个阶段,由于本题不要求输出路径,就是说执行阶段1即可.
阶段1,组合:
先不考虑有序性,构造一棵最优树,使得所有叶子的深度和最终所求的OABST的叶子的深度一样且最优性一样.
定义工作序列是一个可以包含内部或外部结点的序列.
开始时,把所有外部结点按顺序放在工作序列内.
在每次的迭代中,选出两个结点wi,wj合并为一个内部结点w=wi+wj,来替换结点wi,从工作序列中删去原来的wj,形成一个新序列.
称每次的迭代被组合的点对(wi,wj),为LocalMinimumCompatiblePairs(LMCP).每次迭代的选择的LMCP是唯一的,它满足如下4条规则:
规则
(1):
在工作序列中,结点wi到结点wj之间没有外部结点(最重要的规则);
规则
(2):
它们的权和w=wi+wj要最小;
规则(3):
在满足
(1)
(2)后,下标i要最小;
规则(4):
在满足
(1)
(2)(3)后,下标j要最小.
以后统一用方框表示外部结点,用圆圈表示内部结点.
下面举例,n=5,w=(5,2,2,3,6).注意:
在第
(2)步时,LMCP(5,3)跨越了内部结点(2+2).
本阶段的实现最为复杂,也最为重要,下面介绍实现.观察算法,发现连续的内部结点之间的顺序没有意义.即两个相邻外部结点间的内部结点,相当于做局部的Huffman树的贪心.可以看成,外部结点将整个工作序列划分为若干的段,两个相邻外部结点间的内部结点,组成一个内部结点集合(用虚圆圈表示内部结点集合).
由于外部结点与内部结点集合交替出现,可以用一个块结构(用外框表示)把外部结点与其右方的内部结点集合包含进来.再将这些块结构用双向链表串起来.
下面讨论,LMCP可能出现的情况:
设图中的块结构B为当前块结构.
情况
(1):
在当前块结构中,外部结点与最小内部结点.
合并LMCP后,放入A的内部结点集合,同时将B的内部结点集合合并到A中.
情况
(2):
在当前块结构中的外部结点,与右边块结构中的外部结点.
合并LMCP后,放入A的内部结点集合,同时将B,C的内部结点集合合并到A中.
情况(3):
在当前块结构的内部结点集合中,最小与次小的内部结点.
合并LMCP后,放入B的内部结点集合.
情况(4):
在当前块结构中的最小内部结点,与右边块结构中的外部结点.
合并LMCP后,放入B的内部结点集合,同时将C的内部结点集合合并到B中.
内部结点集合需要支持找最小和次小元素与合并操作的数据结构来维护.常见的可并优先队列:
左偏树LeftistTree,配对堆PairHeap,FibonacciHeap均可胜任.
每次要找一个全局最小的点对LMCP,则需要用一个总的优先队列来维护这些点对.由于块结构的合并,会造成大量合法的点对的改变,这要求总的优先队列支持插入删除操作.在点对优先级上,点对之间的比较关系为:
以点对权值和为第1关键字,以当前结构的编号作为第2关键字,最后以情况的编号做为第3关键字.
这样每种情况的维护都可以在O(logn)的时间内解决.
整个阶段用时O(nlogn)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SolutionLTCManContest