<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wandsea.com &#187; Usaco</title>
	<atom:link href="http://wandsea.com/blog/tag/usaco/feed" rel="self" type="application/rss+xml" />
	<link>http://wandsea.com/blog</link>
	<description>好吧..大家都要加油哦～</description>
	<lastBuildDate>Wed, 08 Sep 2010 15:17:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>USACO首页上的一段录音</title>
		<link>http://wandsea.com/blog/265.htm</link>
		<comments>http://wandsea.com/blog/265.htm#comments</comments>
		<pubDate>Sat, 09 Aug 2008 13:26:19 +0000</pubDate>
		<dc:creator>Wandsea</dc:creator>
				<category><![CDATA[Internet Vision]]></category>
		<category><![CDATA[Usaco]]></category>
		<category><![CDATA[动画]]></category>
		<category><![CDATA[搞笑]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[不知道大家平时刷题的时候有没有注意过呢 Listen to this amusing MP3 file that explains it all for computer geeks. 看上去很熟悉对吧 下面是稍微完整一些的一个版本 It's worse than I feared. What is it? I'm afraid your s... ]]></description>
			<content:encoded><![CDATA[<p class='fp'>不知道大家平时刷题的时候有没有注意过呢</p>
<p><span style="font-family: Verdana,Tahoma,sans-serif,Arial,Lucida Sans,Gill Sans;"><a href="http://train.usaco.org/usaco/theknack.mp3">Listen to this amusing MP3 file that explains it all for computer geeks.</a></span></p>
<p>看上去很熟悉对吧</p>
<p>下面是稍微完整一些的一个版本<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="src" value="http://www.youtube.com/v/CmYDgncMhXw&amp;hl=en&amp;fs=1" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/CmYDgncMhXw&amp;hl=en&amp;fs=1" allowfullscreen="true"></embed></object></p>
<blockquote><p>It's worse than I feared.</p>
<p>What is it?</p>
<p>I'm afraid your son has ... the Knack.</p>
<p>The knack?</p>
<p>The Knack. It's a rare condition characterized by an extreme intuition about all things mechanical and electrical ... and utter social ineptitude.</p>
<p>Can he lead a normal life?</p>
<p>No. He'll be an engineer.</p>
<p>Oh, no! [crying]</p>
<p>There, there. Don't blame yourself.</p></blockquote>
<p>BBS.oifans.cn 上找到的一部分，前面有一段听不大清楚＝ ＝BS我的英语吧 ，Knack－－<span class="ee_title">a special way of doing something</span><br />
再来找到的另外一个吧～<span id="more-265"></span><br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="src" value="http://www.youtube.com/v/lXGDRrkaRgU&amp;hl=en&amp;fs=1" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/lXGDRrkaRgU&amp;hl=en&amp;fs=1" allowfullscreen="true"></embed></object></p>
<p>Orz Orz Orz Engineer~</p>
<hr/><small>Copyright &copy; 2009<br /> 本Blog采用 知识共享署名-非商业性使用-相同方式共享 许可协议进行许可 轉載請署名 <br/> </small>
<a href="http://wandsea.com/blog/265.htm#comments" title="to the comments"><font color="red">加油！已经盖到2楼啦</font></a><small> － 
<a href="http://Wandsea.com/blog/" title="http://Wandsea.com/blog/">去看看我的Blog？</a> － 
<a href="http://wandsea.com/blog/never-forget" title="http://wandsea.com/blog/never-forget">民主歌声献中华 20年</a><br/> </small></p><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li><a href="http://wandsea.com/blog/433.htm" title="Nothing Here 09.09.27">Nothing Here 09.09.27</a> (10)</li><li><a href="http://wandsea.com/blog/408.htm" title="随机闲聊网站-Omegle.com">随机闲聊网站-Omegle.com</a> (25)</li><li><a href="http://wandsea.com/blog/276.htm" title="Mozilla之书">Mozilla之书</a> (2)</li><li><a href="http://wandsea.com/blog/270.htm" title="Tux 福音 1.0 中文版">Tux 福音 1.0 中文版</a> (2)</li><li><a href="http://wandsea.com/blog/254.htm" title="Flash動畫的反擊">Flash動畫的反擊</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://wandsea.com/blog/265.htm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://train.usaco.org/usaco/theknack.mp3" length="147319" type="audio/mpeg" />
		</item>
		<item>
		<title>USACO Chapter1报告</title>
		<link>http://wandsea.com/blog/63.htm</link>
		<comments>http://wandsea.com/blog/63.htm#comments</comments>
		<pubDate>Sat, 03 May 2008 02:46:12 +0000</pubDate>
		<dc:creator>Wandsea</dc:creator>
				<category><![CDATA[Program Impossible]]></category>
		<category><![CDATA[Usaco]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Usaco第一章做完了，总结一下~ &#160; Section 1.1.1 PROB Your Ride Is Here [ANALYSIS] 这道题就是考你会不会写程序啦，还有呢，就是 ord(c) 这个Function的运用了~ &#160; Section 1.1.2 PROB Greedy Gift Givers [ANALYSIS] 这... ]]></description>
			<content:encoded><![CDATA[<p class='fp'>Usaco第一章做完了，总结一下~ </p>
<p>&#160; </p>
<h4>Section 1.1.1 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=ride">PROB Your Ride Is Here</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=ride">[ANALYSIS]</a></h4>
<p>这道题就是考你会不会写程序啦，还有呢，就是 ord(c) 这个Function的运用了~ </p>
<p>&#160; </p>
<h4>Section 1.1.2 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=gift1">PROB Greedy Gift Givers</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=gift1">[ANALYSIS]</a></h4>
<p>这道题细心一点，名字对应好就不会错了~ </p>
<p>&#160; </p>
<h4>Section 1.1.3 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=friday">PROB Friday the Thirteenth</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=friday">[ANALYSIS]</a></h4>
<p>这一道题的关键就是：下一个月13号的星期数就等于(这个月13号的星期数＋这个月的天数MOD 7)MOD 7 </p>
<p>PS：我记星期天为0。 </p>
<p>&#160; </p>
<h4>Section 1.1.4 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=beads">PROB Broken Necklace</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=beads">[ANALYSIS]</a></h4>
<p>O（N）的算法。Dynamic Programming is good method for solving this problem in O(N). </p>
<p>If we consider two copies of the string we easy transform cyclic configuration of the necklace to linear. </p>
<p>Now we can compute for each breaking point how many beads of the same color can be collected on the left and on the right from the breaking point. I show how we can compute it only for the left side. </p>
<p>For right side it is analogical. Let r[p] and b[p] be the number of red / blue beads that can be collected, when necklace is broken in point p. If we know this and color of next bead (c) we can compute r[p+1] and b[p+1]. </p>
<p>r[0] = p[0] = 0 If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0 because the length of the blue beads is 0. </p>
<p>if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0if c = 'w' then both length of the red and length of blue beads canbe longer. so r[p+1] = r[p]+1 and b[p+1] = b[p] + 1. </p>
<p>The number of beads that can be collected in breaking point p is then </p>
<p>max(left[r[p]], left[b[p]]) + max(right[r[p]], right[b[p]]). </p>
<p>And themaximum from this value is answer for the problem. </p>
<p>PS：我是看了标程写出来的呃~做了好久，原来的烦死掉了，就重写了一个。先把珠子复制一遍~ </p>
<p>&#160; </p>
<h4>Section 1.2.1 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=milk2">PROB Milking Cows</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=milk2">[ANALYSIS]</a></h4>
<p>先按起始时间进行快速排序，然后按顺序判断时间段    <br />if t1[i]&lt;=r then     <br />r:=max(t2[i],r)     <br />t1[i]，t2[i]为每一位工人的起始结束时间。     <br />l、r为时间段的起始结束时间。     <br />每一个连续的时间段结束后。     <br />a1:=max(a1,r-l);     <br />a2:=max(a2,t1[i]-r);     <br />l:=t1[i];     <br />r:=t2[i];     <br />a1为最长的连续工作时间；     <br />a2为最长的空闲时间；     <br />最后再执行一次求最长的连续工作时间 a1:=max(a1,r-l);     <br />完成~ </p>
<p>&#160;</p>
<div id='extendedEntryBreak' name='extendedEntryBreak'></div>
<h4>Section 1.2.2 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=transform">PROB Transformations</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=transform">[ANALYSIS]</a></h4>
<p>这道题，好奇怪厄~主程序都一样，原来的就是提交不通过~呃~郁闷的~Procedure N4 为翻转Procedure N1 为顺时针90'最后再用 function bj 来判断两图案是否相同~ </p>
<blockquote><p>procedure N1(l:arr2; var m:arr2); </p>
<p>var i,j:integer; </p>
<p>begin </p>
<p>for i:=1 to n do </p>
<p>for j:=1 to n do </p>
<p>m[j,n-i+1]:=l[i,j]; </p>
<p>end; </p>
<p>function bj(a,b:arr2):boolean; </p>
<p>var i,j:integer; </p>
<p>begin </p>
<p>equal:= true; </p>
<p>for i:=1 to n do </p>
<p>for j:=1 to n do </p>
<p>if a[i,j] &lt;&gt; b[i,j] then begin </p>
<p>equal:=false; exit; </p>
<p>end; </p>
<p>end;</p>
</blockquote>
<p>&#160; </p>
<h4>Section 1.2.3 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=namenum">PROB Name That Number</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=namenum">[ANALYSIS]</a></h4>
<p>掌握好文件的读取写入操作，逐个读入字典中的人名，并转换成数字，与起始的数字比较，相同就输出，全部用字符串，不知道为什么我用int64和longint都不可以。厄~一开始想把数字转换成名字作但复杂度为O（3^N），太复杂，后来看了别人的报告，发觉用字典好~ </p>
<p>s:array['A'..'Z'] of char=('2','2','2','3','3','3','4','4','4','5','5','5', '6','6','6','7','7','7','7','8','8','8','9','9','9','9'); </p>
<p>&#160; </p>
<h4>Section 1.2.4 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=palsquare">PROB Palindromic Squares</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=palsquare">[ANALYSIS]</a></h4>
<p>最主要的是进制转换。Procedure ToB 然后倒转字符串若相等，即为回文数。输出。 </p>
<blockquote><p>procedure ToB (a:longint;var c:string); </p>
<p>var k,i:longint; </p>
<p>begin </p>
<p>k:=0; </p>
<p>c:=''; </p>
<p>repeat inc(k); </p>
<p>i:=a mod b; </p>
<p>a:=a div b; </p>
<p>c:=s[i]+c; </p>
<p>until a=0; </p>
<p>end;</p>
</blockquote>
<p>&#160; </p>
<h4>Section 1.2.5 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=dualpal">PROB Dual Palindromes</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=dualpal">[ANALYSIS]</a></h4>
<p>和上一道差不多 TOB +倒转字符串 轻松AC </p>
<p>&#160; </p>
<h4></h4>
<h4>Section 1.3.1 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=milk">PROB Mixing Milk</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=milk">[ANALYSIS]</a></h4>
<p>我先用快排Qsort把所有数据排序，每次选最小的卖牛奶的人把他&#8220;榨干&#8221;，求出钱数~ 贪心第一题 </p>
<p>&#160; </p>
<h4>Section 1.3.2 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=barn1">PROB Barn Repair</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=barn1">[ANALYSIS]</a></h4>
<p>~~~好汗的~已经是第三道关于牛的了呃~    <br />第一次数据太弱，程序完全错掉还AC3个，汗     <br />先读入全部数据，进行快排。     <br />然后把满牛棚之间的连续的空牛棚的数量存入数组，进行快排~     <br />舍去最大的M-1个数字~     <br />剩下的加上C即为最终解~ 还是贪心 </p>
<p>&#160; </p>
<h4>Section 1.3.3 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=calfflac">PROB Calf Flac</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=calfflac">[ANALYSIS]</a></h4>
<p>三个数组~a为原文c为a中的字母，转为大写存入~b为c中字母在a中对应的位置，最后输出时使用~ </p>
<p>&#160; </p>
<h4>Section 1.3.4 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=crypt1">PROB Prime Cryptarithm</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=crypt1">[ANALYSIS]</a></h4>
<p>厄~一开始没看清题目~以为最后输出的数为回文数~汗~USACO的题目太奇怪了~做到现在不是回文数就是COW呃~思维定势~别的就没有什么叻~枚举第一行，第二行的每一种可能性，使3，4，5行都满足条件~ </p>
<p>&#160; </p>
<h4>Section 1.4.1 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=packrec">PROB Packing Rectangles</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=packrec">[ANALYSIS]</a></h4>
<p>郁闷死了,从开学做到现在,不爽 ,Copy lhyxxh1 同学的, </p>
<p>这道题目数据规模很小，大可枚举4个矩形的摆放方式，然后再计算大矩形的边长。    <br />以下用width[1..4]表示矩形的长，height[1..4]表示矩形的宽。用w表示大矩形的长，h表示大矩形的宽：     <br />方式一：     <br />w = width[1] + width[2] + width[3] + width[4]     <br />h = ＭＡＸ｛height[1], height[2], height[3], height[4]｝     <br />方式二：     <br />w = ＭＡＸ｛width[1] + width[2] + width[3], width[4]｝     <br />h = ＭＡＸ｛height[1], height[2], height[3]｝ + height[4]     <br />方式三：     <br />w = ＭＡＸ｛width[1] + width[2], width[3]｝ + width[4]     <br />h = ＭＡＸ｛ＭＡＸ｛height[1], height[2]｝ + height[3], height[4]｝     <br />方式四：     <br />w = width[1] + width[2] + ＭＡＸ｛width[3], width[4]｝     <br />h = ＭＡＸ｛height[1], height[2], height[3] + height[4]｝     <br />方式五：     <br />同方式四。     <br />方式六：     <br />h = ＭＡＸ｛height[1] + height[3], height[2] + height[4]}     <br />w = ＭＡＸ: width[1] + width[2], 一般条件     <br />width[3], 一般条件     <br />width[4], 一般条件     <br />width[2] + width[3], 当height[1] &lt; height[2]时     <br />width[1] + width[4], 当height[1] &gt; height[2]时     <br />width[3] + width[4], 当height[1] + height[3] &gt; height[2]时     <br />注意以上是把矩形1放到左下，2放到右下，3放到右上，4放到左上为例的。 </p>
<p>&#160; </p>
<h4>Section 1.4.2 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=clocks">PROB The Clocks</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=clocks">[ANALYSIS]</a></h4>
<p>枚举所有方法 </p>
<p>还有一种Ox的 </p>
<blockquote><pre>program clocks;
const
INV : array[3..12] of byte =(1, 0, 0, 2, 0, 0, 3, 0, 0, 0);

var inp, out: text;
a, b, c, d, e, f, g, h, i: integer;
ax, bx, cx, dx, ex, fx, gx, hx, ix,l: integer;
t,an: array[1..9] of integer;
begin
assign (inp, 'clocks.in'); reset (inp);
readln(inp, ax, bx, cx);
readln(inp, dx, ex, fx);
readln(inp, gx, hx, ix);
a:=inv[ax]; b:=inv[bx]; c:=inv[cx]; d:=inv[dx];
e:=inv[ex]; f:=inv[fx]; g:=inv[gx]; h:=inv[hx];
i:=inv[ix];
t[1] := ( 8 + a + 2 * b + c + 2 * d +2 * e - f + g - h ) mod 4;
t[2] := ( a + b + c + d + e + f + 2 * g + 2 * i ) mod 4;
t[3] := ( 8 + a + 2 * b + c - d + 2 * e + 2 * f -h + i ) mod 4;
t[4] := ( a + b + 2 * c + d + e + g + h + 2 * i ) mod 4;
t[5] := ( 4 + a + 2 * b + c + 2 * d - e + 2 * f + g + 2 * h+ i ) </pre>
<pre>mod 4;
t[6] := ( 2 * a + b + c + e + f + 2 * g + h + i ) mod 4;
t[7] := ( 8 + a - b + 2 * d + 2 * e - f + g + 2 * h + i ) mod 4;
t[8] := ( 2 * a + 2 * c + d + e + f + g + h + i) mod 4;
t[9] := ( 8 - b + c - d + 2 * e + 2 * f + g + 2 * h+ i) mod 4;
assign(out, 'clocks.out'); rewrite(out);
for a := 1 to 9 do
for b := 1 to t[a] do Begin
inc(l);
an[l]:=a;
end;
for a:=1 to l-1 do
write(out,an[a],' ');
write(out,an[l]);
writeln(out); close(out)
end.</pre>
<pre>&#160;</pre>
</blockquote>
<h4>Section 1.4.3 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=ariprog">PROB Arithmetic Progressions</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=ariprog">[ANALYSIS]</a></h4>
<p>说实话，没看懂是怎么做的~ </p>
<p>&#160; </p>
<h4>Section 1.4.4 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=milk3">PROB Mother's Milk</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=milk3">[ANALYSIS]</a></h4>
<p>说实话，没看懂是怎么做的~ </p>
<p>&#160; </p>
<h4>Section 1.5.1 <a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=numtri">PROB Number Triangles</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=numtri">[ANALYSIS]</a></h4>
<p>dp[i,j]:=max(dp[i-1,j],dp[i-1,j-1])+tree[i,j]; </p>
<p>dp为到该节点的最大权值，tree为该点权值 </p>
<p>动规？ </p>
<p>&#160; </p>
<h4>Section 1.5.2<a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=pprime">PROB Prime Palindromes</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=pprime">[ANALYSIS]</a></h4>
<p>判断素数 </p>
<blockquote>
<p>function prime(z:longint):boolean;<br />
    <br />var </p>
<p>i:longint; </p>
<p>begin </p>
<p>begin </p>
<p>i:=2; </p>
<p>while (i*i&lt;=z) and (z mod i &lt;&gt;0) do inc(i); </p>
<p>if z mod i = 0 then prime:=false else prime:=true; </p>
<p>end; </p>
<p>end;</p>
</blockquote>
<p>还有就是符合条件的偶数位的数只有11（可以证明） 从三位数搜索吧（&lt;100 的只有 5 7 11） </p>
<p>&#160; </p>
<h4>Section 1.5.3<a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=sprime">PROB SuperPrime Rib</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=sprime">[ANALYSIS]</a></h4>
<blockquote>
<p>procedure sp(x,k:longint);<br />
    <br />var </p>
<p>i:longint; </p>
<p>begin </p>
<p>if k&lt;=n then </p>
<p>for i:=1 to 4 do begin </p>
<p>x:=x*10+y[i]; </p>
<p>if prime(x) then sp(x,k+1); </p>
<p>x:=(x-y[i]) div 10; </p>
<p>end </p>
<p>else </p>
<p>writeln(x); </p>
<p>end;</p>
</blockquote>
<p>x为当前数 k为当前数的长度 DFS 搜吧 </p>
<p>&#160; </p>
<h4>Section 1.5.4<a href="http://ace.delos.com/usacoprob2?a=U658EzgtBaJ&amp;S=checker">PROB Checker Challenge</a> <a href="http://ace.delos.com/usacoanal2?a=U658EzgtBaJ&amp;S=checker">[ANALYSIS]</a></h4>
<p>经典的8皇后问题，小于11的我能通过，大的就TLE了~ </p>
<p>&#160; </p>
<p>就这样吧~先更新到这里了~ </p>
<p>BY <a href="http://wandsea.com/">Wandsea</a> 转载请注明~ </p>
<hr/><small>Copyright &copy; 2009<br /> 本Blog采用 知识共享署名-非商业性使用-相同方式共享 许可协议进行许可 轉載請署名 <br/> </small>
<a href="http://wandsea.com/blog/63.htm#comments" title="to the comments"><font color="red">加油！已经盖到0楼啦</font></a><small> － 
<a href="http://Wandsea.com/blog/" title="http://Wandsea.com/blog/">去看看我的Blog？</a> － 
<a href="http://wandsea.com/blog/never-forget" title="http://wandsea.com/blog/never-forget">民主歌声献中华 20年</a><br/> </small></p><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li><a href="http://wandsea.com/blog/265.htm" title="USACO首页上的一段录音">USACO首页上的一段录音</a> (2)</li><li><a href="http://wandsea.com/blog/59.htm" title="Flood Fill 种子染色法">Flood Fill 种子染色法</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://wandsea.com/blog/63.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flood Fill 种子染色法</title>
		<link>http://wandsea.com/blog/59.htm</link>
		<comments>http://wandsea.com/blog/59.htm#comments</comments>
		<pubDate>Fri, 02 May 2008 11:10:22 +0000</pubDate>
		<dc:creator>Wandsea</dc:creator>
				<category><![CDATA[Program Impossible]]></category>
		<category><![CDATA[Usaco]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://FloodFill按原意应翻译成“水流式填充法”（如果我没译错），有些中文书籍上将它称作“种子染色法”，然而大部分的书籍（包括中文书籍）都直接引用其英文原名：FloodFill。介于此，下文所有涉及到FloodFill的都直接引用英文——译者）h4样例：相连的农场/h4FarmerJohn的农场被一次意外事故破坏了，有一些农场与其他的农场之间有道路相连，而有些道路却已被破坏。这使得FarmerJohn无法了解到从一个农场能否到达另一个农场。你的任务就是帮助FarmerJ</guid>
		<description><![CDATA[Flood Fill 按原意应翻译成“水流式填充法”（如果我没译错），有些中文书籍上将它称作“种子染色法”，然而大部分的书籍（包括中文书籍）都直接引用其英文原名：Flood Fill。介于此，下文... ]]></description>
			<content:encoded><![CDATA[<p class='fp'>Flood Fill 按原意应翻译成“水流式填充法”（如果我没译错），有些中文书籍上将它称作“种子染色法”，然而大部分的书籍（包括中文书籍）都直接引用其英文原名：Flood Fill。介于此，下文所有涉及到Flood Fill的都直接引用英文 ——译者）</p>
<h4>样例： 相连的农场</h4>
<p>Farmer John的农场被一次意外事故破坏了，有一些农场与其他的农场之间有道路相连，而有些道路却已被破坏。这使得Farmer John 无法了解到从一个农场能否到达另一个农场。你的任务就是帮助Farmer John来了解哪些农场是连通的。</p>
<h4>给出：</h4>
<p>上题实际上就是要求寻找一张无向图的所有极大连通子图。</p>
<p>给出一张未知连通性的图，如下图：</p>
<p><a><img src="http://www.oiers.cn/usaco%20training/flood1.gif" alt="" /></a><br />
可知，该图的极大连通子图是：{1,4,8}， {2,5,6,7,9} 和 {3}。</p>
<h4>算法: Flood Fill</h4>
<p>Flood Fill 可以用深度优先搜索，广度优先搜索或广度优先扫描来实现。他的实现方式是寻找到一个未被标记的结点对它标记后扩展，将所有由它扩展出的结点标上与它相同的标号，然后再找另一个未被标号的 结点重复该过程。这样，标号相同的结点就属于同一个连通子图。</p>
<p>深搜：取一个结点，对其标记，然后标记它所有的邻结点。对它的每一个邻结点这么一直递归下去完成搜索。</p>
<p>广搜：与深搜不同的是，广搜把结点加入队列中。</p>
<p>广度扫描（不常见）：每个结点有两个值，一个用来记录它属于哪个连通子图（c），一个用来标记是否已经访问（v）。算法对每一个未访问而在某个连通子图当中的结点扫描，将其标记访问，然后把它的邻结点的（c）值改为当前结点的（c）值。</p>
<p>深搜最容易写，但它需要一个栈。搜索显式图没问题，而对于隐式图，栈可能就存不下了。</p>
<p>广搜稍微好一点，不过也存在问题。搜索大的图它的队列有可能存不下。深搜和广搜时间复杂度均为O(N+M)。其中，N为结点数，M为边数。</p>
<p>广度扫描需要的额外空间很少，或者可以说根本不要额外空间，但是它很慢。时间复杂度是O(N^2+M)。</p>
<p>（实际应用中，我们一般写的是DFS，因为快。空间不是问题，DFS可改用非递归的栈操作完成。但为了尊重原文，我们还是译出了广度扫描的全过程。——译者）</p>
<h4>广度扫描的伪代码</h4>
<p>代码中用了一个小技巧，因此无须额外空间。结点若未访问，将其归入连通子图（-2），就是代码里的component -2。这样无须额外空间来记录结点是否访问，请读者用心体会。<br />
<tt><span style="font-size: x-small;"><br />
# component(i) denotes the<br />
# component that node i is in<br />
1 function flood_fill(new_component) </span></tt></p>
<p><tt><span style="font-size: x-small;">2 do<br />
3    num_visited = 0<br />
4    for all nodes i<br />
5      if component(i) = -2<br />
6        num_visited = num_visited + 1<br />
7        component(i) = new_component<br />
8        for all neighbors j of node i<br />
9          if component(j) = nil<br />
10            component(j) = -2<br />
11 until num_visited = 0 </span></tt></p>
<p><tt><span style="font-size: x-small;">12 function find_components </span></tt></p>
<p><tt><span style="font-size: x-small;">13   num_components = 0<br />
14   for all nodes i<br />
15     component(node i) = nil<br />
16   for all nodes i<br />
17     if component(node i) is nil<br />
18       num_components =<br />
num_components + 1<br />
19       component(i) = -2<br />
20       flood_fill(component<br />
num_components)<br />
</span></tt></p>
<p>算法的时间复杂度是 O(<em>N <sup>2</sup></em>)，每个结点访问一次，每条边经过两次。</p>
<h4>实例</h4>
<p>考虑刚才的那张图：</p>
<p><a><img src="http://www.oiers.cn/usaco%20training/flood1.gif" alt="" /></a></p>
<p>开始时，所有的结点都没有访问。（下例中未访问被表示为 -2)</p>
<p>首先从结点1开始，结点1未访问，那么先处理结点1，将它归入连通子图1。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center"><strong>1</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
</tbody>
</table>
<p>标记完成后，对它进行第一步的扩展，由结点4和结点8与结点1连通，故它们被扩展出来。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>4</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center"><strong>8</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
</tbody>
</table>
<p>之后，先处理结点4，将它与结点1归入相同的连通子图。现在它没有可扩展的结点了（结点1已被扩展过）</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>4</strong></td>
<td style="text-align: center;"><strong>1</strong></td>
</tr>
<tr>
<td align="center"><strong>8</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
</tbody>
</table>
<p align="left">接着处理结点8。结果与结点4一样。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>8</strong></td>
<td align="center"><strong>1</strong></td>
</tr>
</tbody>
</table>
<p align="left">现在，所有与结点1连通的结点都已扩展，标号为1的连通子图产生了。那么我们将跳出扩展步骤，寻找下一个连通子图，标号为2。</p>
<p align="left">与上一步相同的顺序，找到结点2。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>2</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
</tbody>
</table>
<p align="left">扩展结点2，结点7与结点9出现。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>7</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>9</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
</tbody>
</table>
<p align="left">下一步，扩展结点7，结点5出现。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>5</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center"><strong>7</strong></td>
<td align="center"><strong>2</strong></td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">-2</td>
</tr>
</tbody>
</table>
<p align="left">然后是结点9。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">-2</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center"><strong>9</strong></td>
<td align="center"><strong>2</strong></td>
</tr>
</tbody>
</table>
<p align="left">扩展结点5。结点6出现。</p>
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center"><strong>6</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">2</td>
</tr>
</tbody>
</table>
<p align="left">很遗憾，结点6没有可供扩展的结点。至此连通子图2产生。</p>
<p align="left">
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center"><strong>6</strong></td>
<td align="center"><strong>2</strong></td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">2</td>
</tr>
</tbody>
</table>
<p align="left">之后寻找连通子图3，至此，仅有结点3未被扩展。</p>
<p align="left">
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center"><strong>3</strong></td>
<td align="center"><strong>-2</strong></td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">2</td>
</tr>
</tbody>
</table>
<p align="left">结点3没有可供扩展的结点，这样，结点3就构成了仅有一个结点的连通子图3。</p>
<p align="left">
<table border="1">
<tbody>
<tr>
<td align="center">结点</td>
<td align="center">连通子图</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">2</td>
</tr>
</tbody>
</table>
<p align="left">结点3处理结束后，整个图的所有9个结点就都被归入相应的3个连通子图。Flood Fill 结束。</p>
<h4>问题提示</h4>
<p align="left">这类问题一般很清晰，求解关于“连通”的问题会用到 Flood Fill。它也很经常用作某些算法的预处理。</p>
<h4>扩展与延伸</h4>
<p align="left">有向图的连通性比较复杂。</p>
<p align="left">同样的填充算法可以找出从一个结点能够到达的所有结点。每一层递归时，若一个结点未访问，就将其标记为已访问（表示他可以从源结点到达)，然后对它所有能到达且为访问的结点进行下一层递归。</p>
<p align="left">若要求出可以到达某个结点的所有结点，你可以对后向弧做相同的操作。</p>
<h4>例题</h4>
<h5>控制公司 [有删节, IOI 93]</h5>
<p align="left">已知一个带权有向图，权值在0-100之间。</p>
<p align="left">如果满足下列条件，那么结点A“拥有”结点B：</p>
<ul>
<li>
<p align="left">A = B</p>
</li>
<li>
<p align="left">从A到B有一条权值大于50的有向弧。</p>
</li>
<li>
<p align="left">存在一系列结点 <em>C <sub>1</sub></em> 到 <em>C <sub>k</sub></em> 满足 A 拥有 <em>C <sub>1</sub></em> 到 <em>C <sub>k</sub></em>, 每个节点都有一条弧到B，记作<em>x <sub>1</sub></em> <em>，x <sub>2 </sub></em>...<em>x <sub>k，</sub></em>并且 <em>x <sub>1</sub></em> + <em>x <sub>2</sub></em> + ... + <em>x <sub>k</sub></em> &gt; 50。</p>
</li>
</ul>
<p align="left">找出所有的（A，B）对，满足A拥有B。</p>
<p align="left">分析：这题可以用上面提到的“给出一个源，在有向图中找出它能够到达的结点”算法的改进版解决。要计算A拥有的结点，要对每个结点计算其“控股百分比”。 把它们全部设为0。现在，</p>
<hr/><small>Copyright &copy; 2009<br /> 本Blog采用 知识共享署名-非商业性使用-相同方式共享 许可协议进行许可 轉載請署名 <br/> </small>
<a href="http://wandsea.com/blog/59.htm#comments" title="to the comments"><font color="red">加油！已经盖到0楼啦</font></a><small> － 
<a href="http://Wandsea.com/blog/" title="http://Wandsea.com/blog/">去看看我的Blog？</a> － 
<a href="http://wandsea.com/blog/never-forget" title="http://wandsea.com/blog/never-forget">民主歌声献中华 20年</a><br/> </small></p><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li><a href="http://wandsea.com/blog/265.htm" title="USACO首页上的一段录音">USACO首页上的一段录音</a> (2)</li><li><a href="http://wandsea.com/blog/63.htm" title="USACO Chapter1报告">USACO Chapter1报告</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://wandsea.com/blog/59.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
