锘??xml version="1.0" encoding="utf-8" standalone="yes"?>72种姿势欧美久久久久大黄蕉,久久亚洲AV成人无码国产,韩国三级中文字幕hd久久精品http://www.shnenglu.com/Icyflame/瀛︿範綆楁硶zh-cnThu, 08 May 2025 20:48:10 GMTThu, 08 May 2025 20:48:10 GMT60鍓茬偣涓庢ˉhttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.htmlIcyflameIcyflameSun, 05 Jul 2009 08:18:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.htmlhttp://www.shnenglu.com/Icyflame/comments/89227.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.html#Feedback1http://www.shnenglu.com/Icyflame/comments/commentRss/89227.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/89227.html涓銆佸畾涔?br>      鍓茬偣錛氬鏋滃湪鍥綠涓垹鍘諱竴涓粨鐐箄鍚庯紝鍥綠鐨勮繛閫氬垎鏋濇暟澧炲姞錛屽嵆W(G-u)>W(G)錛屽垯縐扮粨鐐箄涓篏鐨勫壊鐐癸紝鍙堢О鍏寵妭鐐廣?br>      妗ワ細濡傛灉鍦ㄥ浘G涓垹鍘諱竴鏉¤竟e鍚庯紝鍥綠鐨勮繛閫氬垎鏀暟澧炲姞錛屽嵆W(G-e)>W(G)錛屽垯縐拌竟u涓篏鐨勬ˉ錛屽張縐板壊杈規垨鍏寵妭杈廣?br>      鍙岃繛閫氬垎鏀細G涓笉鍚壊鐐圭殑鏋佸ぇ榪為氬瓙鍥劇О涓篏鐨勫弻榪為氬垎鏀紝鍙堢О涓篏鐨勫潡銆?br>浜屻丏FS
      鎻忚堪錛?/strong>鍦ㄥ浜庝換閫変竴涓浘涓粨鐐逛負鏍圭殑DFS鎼滅儲鏍戜腑寤虹珛涓涓狶AB鏁扮粍涓嶭OW鏁扮粍錛孡AB鏁扮粍瀛樺偍涓粨鐐圭殑緙栧彿錛孡OW鏁扮粍瀛樺偍鍚勭偣鍙婂叾瀛愭爲鐨勫悇緇撶偣鑳藉埌杈劇殑鏈灝忕紪鍙風粨鐐圭殑緙栧彿銆?br>
1 //lab涓轟竴涓叏灞鍙橀噺錛屽垵濮嬩負1錛?nbsp;LAB鍚勯」鍒濆涓?
2 DFS(u)
3     LAB[u] = LOW[u] = lab++
4     for each (u, v) in E(G)
5         if LAB[v] is 0
6             DFS(v)
7             LOW[u] = min{LOW[u], LOW[v]}
8         else if  v isnot parent of u
9             LOW[u] = min{LOW[u], LAB[v]}

      絎?琛屼腑錛屽鏋?u, v)鏄爲杈癸紝鍒欏v鍋氭繁搴︿紭鍏堟悳绱紝騫朵笖LOW[u] = min{LOW[u], LOW[v]}錛屽鏋?u, v)鏄弽鍚戣竟錛屽垯LOW[u] = min{LOW[u], LAB[v]}銆?/span>
涓夈佸壊鐐?br>      鎻忚堪錛?/strong>褰撲竴涓粨鐐箄鏄壊鐐規椂蹇呮弧瓚充互涓嬩袱涓潯浠朵箣涓錛?br>            1錛塽涓烘牴涓旇嚦灝戞湁涓ゆ5瀛愭爲錛?br>            2錛塽涓嶄負鏍逛笖瀛樺湪涓涓猽鍦ㄦ繁鎼滄爲涓殑瀛愬コv浣垮緱LOW[v] ≥ LAB[u]銆?br>      紺轟緥錛?/strong>POJ 1523 瑙i鎶ュ憡銆?br>鍥涖佹ˉ
       鎻忚堪錛?/strong>涓鏉¤竟e=(u, v)鏄ˉ錛屽綋涓斾粎褰揺涓烘爲鏋濊竟涓擫OW[v] > LAB[u]銆?br>      紺轟緥錛?/strong>POJ 3352 瑙i鎶ュ憡銆?/span>
      

Icyflame 2009-07-05 16:18 鍙戣〃璇勮
]]>
POJ 3352 妗?/title><link>http://www.shnenglu.com/Icyflame/archive/2009/07/05/89289.html</link><dc:creator>Icyflame</dc:creator><author>Icyflame</author><pubDate>Sun, 05 Jul 2009 08:08:00 GMT</pubDate><guid>http://www.shnenglu.com/Icyflame/archive/2009/07/05/89289.html</guid><wfw:comment>http://www.shnenglu.com/Icyflame/comments/89289.html</wfw:comment><comments>http://www.shnenglu.com/Icyflame/archive/2009/07/05/89289.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/Icyflame/comments/commentRss/89289.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/Icyflame/services/trackbacks/89289.html</trackback:ping><description><![CDATA[<p>涓銆侀鐩弿榪?br></p> <p class=pst>Description</p> <div id="su8a8ik" class=ptx lang=en-US> <div> <p>It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.</p> <p>The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.</p> <p>Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.</p> <p>So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.</p> </div> </div> <p class=pst>Input</p> <div id="o8ko4ee" class=ptx lang=en-US> <p>The first line of input will consist of positive integers <em>n</em> and <em>r</em>, separated by a space, where 3 ≤ <em>n</em> ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ <em>r</em> ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to <em>n</em>. Each of the following <em>r</em> lines will consist of two integers, <em>v</em> and <em>w</em>, separated by a space, indicating that a road exists between the attractions labelled <em>v</em> and <em>w</em>. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.</p> </div> <p class=pst>Output</p> <div id="wmwgkk4" class=ptx lang=en-US> <p>One line, consisting of an integer, which gives the minimum number of roads that we need to add.</p> </div> <p class=pst>Sample Input</p> <pre class=sio>Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10 Sample Input 2 3 3 1 2 2 3 1 3</pre> <p class=pst>Sample Output</p> <pre class=sio>Output for Sample Input 1 2 Output for Sample Input 2 0 </pre> <p><br>浜屻佸垎鏋?br><span style="FONT-SIZE: 10pt">      鐢―FS瑙e喅闂錛岃緇嗙畻娉曪細<a title=鍓茬偣銆佹ˉ涓庡弻榪為氬垎鏀?href="http://www.shnenglu.com/Icyflame/archive/2009/07/04/89227.html">鍓茬偣涓庢ˉ</a>銆?/span><br>涓夈佷唬鐮?br></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top>#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">list</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n, r;<br></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top>list</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> g[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> num, lab[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">], low[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top>list</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">pair</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> edge;<br></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> degree[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> parent[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">], rank[</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> init_set()<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_207_274_Open_Image onclick="this.style.display='none'; Codehighlighter1_207_274_Open_Text.style.display='none'; Codehighlighter1_207_274_Closed_Image.style.display='inline'; Codehighlighter1_207_274_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_207_274_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_207_274_Closed_Text.style.display='none'; Codehighlighter1_207_274_Open_Image.style.display='inline'; Codehighlighter1_207_274_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_207_274_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_207_274_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">1001</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_237_272_Open_Image onclick="this.style.display='none'; Codehighlighter1_237_272_Open_Text.style.display='none'; Codehighlighter1_237_272_Closed_Image.style.display='inline'; Codehighlighter1_237_272_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_237_272_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_237_272_Closed_Text.style.display='none'; Codehighlighter1_237_272_Open_Image.style.display='inline'; Codehighlighter1_237_272_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_237_272_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_237_272_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        parent[i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> i;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        rank[i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> find(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> k)<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_292_371_Open_Image onclick="this.style.display='none'; Codehighlighter1_292_371_Open_Text.style.display='none'; Codehighlighter1_292_371_Closed_Image.style.display='inline'; Codehighlighter1_292_371_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_292_371_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_292_371_Closed_Text.style.display='none'; Codehighlighter1_292_371_Open_Image.style.display='inline'; Codehighlighter1_292_371_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_292_371_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_292_371_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(parent[k] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> k) <br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> k;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> parent[k] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> find(parent[k]);<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> union_set(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> u, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> v)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_402_555_Open_Image onclick="this.style.display='none'; Codehighlighter1_402_555_Open_Text.style.display='none'; Codehighlighter1_402_555_Closed_Image.style.display='inline'; Codehighlighter1_402_555_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_402_555_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_402_555_Closed_Text.style.display='none'; Codehighlighter1_402_555_Open_Image.style.display='inline'; Codehighlighter1_402_555_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_402_555_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_402_555_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pu </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> find(u), pv </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> find(v);<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(rank[pu] </span><span style="COLOR: #000000"><=</span><span style="COLOR: #000000"> rank[pv])<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_464_504_Open_Image onclick="this.style.display='none'; Codehighlighter1_464_504_Open_Text.style.display='none'; Codehighlighter1_464_504_Closed_Image.style.display='inline'; Codehighlighter1_464_504_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_464_504_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_464_504_Closed_Text.style.display='none'; Codehighlighter1_464_504_Open_Image.style.display='inline'; Codehighlighter1_464_504_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_464_504_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_464_504_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        parent[pu] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pv;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        rank[pv] </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> pu;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img id=Codehighlighter1_513_553_Open_Image onclick="this.style.display='none'; Codehighlighter1_513_553_Open_Text.style.display='none'; Codehighlighter1_513_553_Closed_Image.style.display='inline'; Codehighlighter1_513_553_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_513_553_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_513_553_Closed_Text.style.display='none'; Codehighlighter1_513_553_Open_Image.style.display='inline'; Codehighlighter1_513_553_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_513_553_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_513_553_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        parent[pv] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pu;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        rank[pu] </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> pv;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> u, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> p)<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img id=Codehighlighter1_580_926_Open_Image onclick="this.style.display='none'; Codehighlighter1_580_926_Open_Text.style.display='none'; Codehighlighter1_580_926_Closed_Image.style.display='inline'; Codehighlighter1_580_926_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_580_926_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_580_926_Closed_Text.style.display='none'; Codehighlighter1_580_926_Open_Image.style.display='inline'; Codehighlighter1_580_926_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_580_926_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_580_926_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    lab[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> low[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> num</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    list</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::iterator it;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(it </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> g[u].begin(); it </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> g[u].end(); it</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img id=Codehighlighter1_682_924_Open_Image onclick="this.style.display='none'; Codehighlighter1_682_924_Open_Text.style.display='none'; Codehighlighter1_682_924_Closed_Image.style.display='inline'; Codehighlighter1_682_924_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_682_924_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_682_924_Closed_Text.style.display='none'; Codehighlighter1_682_924_Open_Image.style.display='inline'; Codehighlighter1_682_924_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_682_924_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_682_924_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> v </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">it;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(lab[v] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img id=Codehighlighter1_719_870_Open_Image onclick="this.style.display='none'; Codehighlighter1_719_870_Open_Text.style.display='none'; Codehighlighter1_719_870_Closed_Image.style.display='inline'; Codehighlighter1_719_870_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_719_870_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_719_870_Closed_Text.style.display='none'; Codehighlighter1_719_870_Open_Image.style.display='inline'; Codehighlighter1_719_870_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </span><span id=Codehighlighter1_719_870_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_719_870_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            dfs(v, u);<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            low[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> min(low[u], low[v]);<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(low[v] </span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> lab[u])<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>                edge.push_back(make_pair(u, v));<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>                union_set(u, v); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">u涓巚鑳借繘琛岀緝鐐?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #008000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top></span><span style="COLOR: #000000">        }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(v </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> p)<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            low[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> min(low[u], lab[v]);<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main()<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img id=Codehighlighter1_939_1628_Open_Image onclick="this.style.display='none'; Codehighlighter1_939_1628_Open_Text.style.display='none'; Codehighlighter1_939_1628_Closed_Image.style.display='inline'; Codehighlighter1_939_1628_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_939_1628_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_939_1628_Closed_Text.style.display='none'; Codehighlighter1_939_1628_Open_Image.style.display='inline'; Codehighlighter1_939_1628_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_939_1628_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_939_1628_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">n, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">r);<br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000"><=</span><span style="COLOR: #000000">n; i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        g[i].clear();<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000"><=</span><span style="COLOR: #000000">r; i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img id=Codehighlighter1_1032_1122_Open_Image onclick="this.style.display='none'; Codehighlighter1_1032_1122_Open_Text.style.display='none'; Codehighlighter1_1032_1122_Closed_Image.style.display='inline'; Codehighlighter1_1032_1122_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1032_1122_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1032_1122_Closed_Text.style.display='none'; Codehighlighter1_1032_1122_Open_Image.style.display='inline'; Codehighlighter1_1032_1122_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_1032_1122_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_1032_1122_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> v1, v2;<br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">v1, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">v2);<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        g[v1].push_back(v2);<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        g[v2].push_back(v1);<br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    memset(lab, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> lab);<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    memset(low, </span><span style="COLOR: #000000">0x7f</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> low);<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    num </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    init_set();<br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    dfs(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">76</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    memset(degree, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> degree);<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    list</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">pair</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::iterator it;<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> res </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(it </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> edge.begin(); it </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> edge.end(); it</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img id=Codehighlighter1_1355_1595_Open_Image onclick="this.style.display='none'; Codehighlighter1_1355_1595_Open_Text.style.display='none'; Codehighlighter1_1355_1595_Closed_Image.style.display='inline'; Codehighlighter1_1355_1595_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1355_1595_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1355_1595_Closed_Text.style.display='none'; Codehighlighter1_1355_1595_Open_Image.style.display='inline'; Codehighlighter1_1355_1595_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_1355_1595_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_1355_1595_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> u </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> it</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">first, v </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> it</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">second;<br></span><span style="COLOR: #008080">82</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        degree[find(u)]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(degree[find(u)] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            res</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(degree[find(u)] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">86</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            res</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">87</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        degree[find(v)]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">88</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(degree[find(v)] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">89</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            res</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">90</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(degree[find(v)] </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">91</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            res</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">92</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">93</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, (res</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">94</span><span style="COLOR: #000000"><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div> <img src ="http://www.shnenglu.com/Icyflame/aggbug/89289.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/Icyflame/" target="_blank">Icyflame</a> 2009-07-05 16:08 <a href="http://www.shnenglu.com/Icyflame/archive/2009/07/05/89289.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>POJ 1523 鍓茬偣http://www.shnenglu.com/Icyflame/archive/2009/07/04/89231.htmlIcyflameIcyflameSat, 04 Jul 2009 08:12:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/04/89231.htmlhttp://www.shnenglu.com/Icyflame/comments/89231.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/04/89231.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/89231.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/89231.html涓銆侀鐩弿榪?span style="FONT-SIZE: 10pt">

Description

Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.

Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.

Input

The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

Output

For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist.

The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.

Sample Input

1 2
5 4
3 1
3 2
3 4
3 5
0
1 2
2 3
3 4
4 5
5 1
0
1 2
2 3
3 4
4 6
6 3
2 5
5 1
0
0

Sample Output

Network #1
SPF node 3 leaves 2 subnets
Network #2
No SPF nodes
Network #3
SPF node 2 leaves 2 subnets
SPF node 3 leaves 2 subnets


浜屻佸垎鏋?br>      鐢―FS瑙e喅闂錛岃緇嗙畻娉曪細鍓茬偣涓庢ˉ銆?/span>
涓夈佷唬鐮?br>

 1#include<iostream>
 2#include<list>
 3using namespace std;
 4int t;
 5int v1, v2;
 6list<int> g[1001];
 7bool flag;
 8int root;
 9int counter;
10bool spf[1001];
11int low[1001], lab[1001];
12bool visit[1001];
13void dfs(int u, int fa)
14{
15    low[u] = lab[u] = counter++;
16    list<int>::iterator it;
17    int counter = 0;
18    for(it = g[u].begin(); it != g[u].end(); it++)
19    {
20        int v = *it;
21        if(!lab[v])
22        {
23            counter++;
24            dfs(v, u);
25            low[u] = min(low[u], low[v]);
26            if((u==root && counter>=2|| (u!=root && low[v]>=lab[u]))
27                spf[u] = flag = true;
28        }

29        else if(v != fa)
30            low[u] = min(low[u], lab[v]);
31    }

32}

33void find(int u)
34{
35    visit[u] = true;
36    list<int>::iterator it;
37    for(it = g[u].begin(); it != g[u].end(); it++)
38        if(!visit[*it])
39            find(*it);
40}

41int main()
42{
43    t = 1;
44    while(1)
45    {
46        scanf("%d"&v1);
47        if(v1 == 0break;
48        for(int i=1; i<=1000; i++)
49            g[i].clear();
50        memset(spf, 0sizeof spf);
51        memset(low, 0sizeof low);
52        memset(lab, 0sizeof lab);
53        while(v1 != 0)
54        {
55            scanf("%d"&v2);
56            g[v1].push_back(v2);
57            g[v2].push_back(v1);
58            root = v1;
59            scanf("%d"&v1);
60        }

61        counter = 1;
62        flag = false;
63        dfs(root, -1);
64        printf("Network #%d\n", t++);
65        if(flag)
66        {
67            for(int i=1; i<=1000; i++)
68            {
69                if(!spf[i]) continue;
70                int cnt = 0;
71                memset(visit, 0sizeof visit);
72                visit[i] = true;
73                list<int>::iterator it;
74                for(it = g[i].begin(); it != g[i].end(); it++)
75                    if(!visit[*it])
76                    {
77                        cnt++;
78                        find(*it);
79                    }

80                    printf("  SPF node %d leaves %d subnets\n", i, cnt);
81            }

82        }

83        else
84            printf("  No SPF nodes\n");
85        printf("\n");
86    }

87}


Icyflame 2009-07-04 16:12 鍙戣〃璇勮
]]>
LCA闂錛堝惈RMQ鐨凷T綆楁硶錛?/title><link>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html</link><dc:creator>Icyflame</dc:creator><author>Icyflame</author><pubDate>Sat, 04 Jul 2009 07:25:00 GMT</pubDate><guid>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html</guid><wfw:comment>http://www.shnenglu.com/Icyflame/comments/88987.html</wfw:comment><comments>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/Icyflame/comments/commentRss/88987.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/Icyflame/services/trackbacks/88987.html</trackback:ping><description><![CDATA[<p><strong>涓銆佸畾涔変笌瀹氱悊<br></strong><span style="FONT-SIZE: 10pt">      LCA錛歀east Common Ancestors錛堟渶榪戝叕鍏辯鍏堬級錛屽浜庝竴媯墊湁鏍規爲T鐨勪換鎰忎袱涓妭鐐箄錛寁錛屾眰鍑篖CA(T, u, v)錛屽嵆紱昏窡鏈榪滅殑鑺傜偣x錛屼嬌寰梮鍚屾椂鏄痷鍜寁鐨勭鍏堛?br>      鍦ㄧ嚎綆楁硶錛氱敤姣旇緝闀跨殑鏃墮棿鍋氶澶勭悊錛屼絾鏄瓑淇℃伅鍏呰凍浠ュ悗姣忔鍥炵瓟璇㈤棶鍙渶瑕佺敤姣旇緝灝戠殑鏃墮棿銆?br>      紱葷嚎綆楁硶錛氬厛鎶婃墍鏈夌殑璇㈤棶璇誨叆錛岀劧鍚庝竴璧鋒妸鎵鏈夎闂洖絳斿畬鎴愩?br>      RMQ錛氱粰鍑轟竴涓暟緇凙錛屽洖絳旇闂甊MQ(A, i, j)錛屽嵆A[i...j]涔嬮棿鐨勬渶鍊肩殑涓嬫爣銆?br></span><strong>浜屻丏FS+RMQ</strong><br><span style="FONT-SIZE: 10pt">      1錛塖parse Table錛圫T錛夌畻娉?br>      <strong>鎻忚堪錛?/strong><br></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #008000">//</span><span style="COLOR: #008000">鍒濆鍖?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">INIT_RMQ<br></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">max[i][j]涓瓨鐨勬槸閲峧寮濮嬬殑i涓暟鎹腑鐨勬渶澶у鹼紝鏈灝忓肩被浼鹼紝num涓瓨鏈夋暟緇勭殑鍊?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> i : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to n<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">        max[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> num[i]<br></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> i : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to log(n)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">log(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> j : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to n<br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">            max[i][j] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> MAX(max[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j], max[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)]<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">鏌ヨ</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">RMQ(i, j)<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">    k </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> log(j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> log(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> MAX(max[k][i], max[k][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">k</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])</span></div> <p>      <strong>鍒嗘瀽錛?/strong>鍒濆鍖栬繃紼嬪疄闄呬笂鏄竴涓姩鎬佽鍒掔殑鎬濇兂銆傛槗鐭ワ紝鍒濆鍖栬繃紼嬫晥鐜囨槸O(<em>n</em>log<em>n</em>)錛岃屾煡璇㈣繃紼嬫晥鐜囨槸O(1)銆係T鏄竴涓湪綰跨畻娉曘?br>      <strong>紺轟緥錛?/strong><a title="POJ 3368 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/01/88999.html">POJ 3368 瑙i鎶ュ憡</a><br>      2錛夋眰瑙CA闂<br>      <strong>鎻忚堪錛?/strong><br>      錛?錛塂FS錛氫粠鏍慣鐨勬牴寮濮嬶紝榪涜娣卞害浼樺厛閬嶅巻錛屽茍璁板綍涓嬫瘡嬈″埌杈劇殑欏剁偣銆傜涓涓殑緇撶偣鏄痳oot(T)錛屾瘡緇忚繃涓鏉¤竟閮借褰曞畠鐨勭鐐廣傜敱浜庢瘡鏉¤竟鎭板ソ緇忚繃2嬈★紝鍥犳涓鍏辮褰曚簡2n-1涓粨鐐癸紝鐢‥[1, ... , 2n-1]鏉ヨ〃紺恒?br>      錛?錛夎綆桼錛氱敤R[i]琛ㄧずE鏁扮粍涓涓涓間負i鐨勫厓绱犱笅鏍囷紝鍗沖鏋淩[u] < R[v]鏃訛紝DFS璁塊棶鐨勯『搴忔槸E[R[u], R[u]+1, ..., R[v]]銆傝櫧鐒跺叾涓寘鍚玼鐨勫悗浠o紝浣嗘繁搴︽渶灝忕殑榪樻槸u涓巚鐨勫叕鍏辯鍏堛?br>      錛?錛塕MQ錛氬綋R[u] ≥ R[v]鏃訛紝LCA[T, u, v] = RMQ(L, R[v], R[u])錛涘惁鍒橪CA[T, u, v] = RMQ(L, R[u], R[v])錛岃綆桼MQ銆?br>      鐢變簬RMQ涓嬌鐢ㄧ殑ST綆楁硶鏄湪綰跨畻娉曪紝鎵浠ヨ繖涓畻娉曚篃鏄湪綰跨畻娉曘?br>      <strong>紺轟緥錛?/strong><a title="ZOJ 3195 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/02/89107.html">ZOJ 3195 瑙i鎶ュ憡</a>銆?br></span><strong>涓夈乀arjan綆楁硶<br></strong><span style="FONT-SIZE: 10pt">      <strong>鎻忚堪錛?/strong>Tarjan綆楁硶鏄竴涓綰跨畻娉曪紝涔熷氨鏄鍙湁鍏堣幏寰楁墍鏈夌殑鏌ヨ錛屽啀鎸変竴涓壒瀹氱殑欏哄簭榪涜榪愮畻銆?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #008000">//</span><span style="COLOR: #008000">parent涓哄茍鏌ラ泦錛孎IND涓哄茍鏌ラ泦鐨勬煡鎵炬搷浣?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">Tarjan(u)<br></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">    visit[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> each (u, v) </span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000"> QUERY<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> visit[v]<br></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">            ans(u, v) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> FIND(v)<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> each (u, v) </span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000"> TREE    <br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">visit[v]<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">            Tarjan(v)<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">            parent[v] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> u</span></div> </span><font size=2>      <strong>紺轟緥錛?/strong><a title="HDOJ 2586 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/02/89118.html">HDOJ 2586 瑙i鎶ュ憡</a>銆?/font> <img src ="http://www.shnenglu.com/Icyflame/aggbug/88987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/Icyflame/" target="_blank">Icyflame</a> 2009-07-04 15:25 <a href="http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>HDOJ 2586 LCA:Tarjanhttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89118.htmlIcyflameIcyflameThu, 02 Jul 2009 14:39:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89118.htmlhttp://www.shnenglu.com/Icyflame/comments/89118.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89118.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/89118.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/89118.html涓銆侀鐩弿榪?br>

Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 


 

Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 


 

Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 


 

Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
 


 

Sample Output
10
25
100
100


浜屻佸垎鏋?br>      鐢═arjan瑙e喅鐨凩CA闂錛岃緇嗙畻娉曪細LCA闂銆?/span>
涓夈佷唬鐮?/p>

 1#include<iostream>
 2#include<list>
 3using namespace std;
 4struct node
 5{
 6    int v, d;
 7    void init(int vv, int dd) {v = vv; d = dd;}
 8}
;
 9int t, n, m, v1, v2, len;
10list<node> g[40001];
11list<node> query[40001];
12int dis[40001];
13int res[201][3];
14int parent[40001];
15bool visit[40001];
16int find(int k)
17{
18    if(parent[k] == k)
19        return k;
20    return parent[k] = find(parent[k]);
21}

22void tarjan(int u)
23{
24    if(visit[u]) return;
25    visit[u] = true;
26    parent[u] = u;
27    list<node>::iterator it = query[u].begin();
28    while(it != query[u].end())
29    {
30        if(visit[it->v])
31            res[it->d][2= find(it->v);
32        it++;
33    }

34    it = g[u].begin();
35    while(it != g[u].end())
36    {
37        if(!visit[it->v])
38        {
39            dis[it->v] = min(dis[it->v], dis[u] + it->d);
40            tarjan(it->v);
41            parent[it->v] = u;
42        }

43        it++;
44    }

45}

46int main()
47{
48    scanf("%d"&t);
49    while(t--)
50    {
51        scanf("%d%d"&n, &m);
52        for(int i=1; i<=n; i++)
53            g[i].clear();
54        for(int i=1; i<=m; i++)
55            query[i].clear();
56        for(int i=1; i<n; i++)
57        {
58            scanf("%d%d%d"&v1, &v2, &len);
59            node n1; n1.init(v2, len);
60            g[v1].push_back(n1);
61            node n2; n2.init(v1, len);
62            g[v2].push_back(n2);
63        }

64        for(int i=1; i<=m; i++)
65        {
66            scanf("%d%d"&v1, &v2);
67            res[i][0= v1;
68            res[i][1= v2;
69            node n1; n1.init(v2, i);
70            query[v1].push_back(n1);
71            node n2; n2.init(v1, i);
72            query[v2].push_back(n2);
73        }

74        memset(visit, 0sizeof(visit));
75        memset(dis, 0x7fsizeof(dis));
76        dis[1= 0;
77        tarjan(1);
78        for(int i=1; i<=m; i++)
79            printf("%d\n", dis[res[i][0]] + dis[res[i][1]] - 2*dis[res[i][2]]);
80    }

81}


Icyflame 2009-07-02 22:39 鍙戣〃璇勮
]]>
ZOJ 3195 LCA:RMQhttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89107.htmlIcyflameIcyflameThu, 02 Jul 2009 12:55:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89107.htmlhttp://www.shnenglu.com/Icyflame/comments/89107.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/02/89107.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/89107.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/89107.html闃呰鍏ㄦ枃

Icyflame 2009-07-02 20:55 鍙戣〃璇勮
]]>
POJ 3368 RMQhttp://www.shnenglu.com/Icyflame/archive/2009/07/01/88999.htmlIcyflameIcyflameWed, 01 Jul 2009 08:24:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/01/88999.htmlhttp://www.shnenglu.com/Icyflame/comments/88999.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/01/88999.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88999.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88999.html闃呰鍏ㄦ枃

Icyflame 2009-07-01 16:24 鍙戣〃璇勮
]]>
鏈灝忚垂鐢ㄦ渶澶ф祦闂http://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.htmlIcyflameIcyflameTue, 30 Jun 2009 14:29:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.htmlhttp://www.shnenglu.com/Icyflame/comments/88891.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88891.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88891.html涓銆佸畾涔変笌瀹氱悊
      鏈灝忚垂鐢ㄦ渶澶ф祦錛氳G鏄互s涓烘簮t涓烘眹鐨勭綉緇滐紝c鏄疓鐨勫閲忥紝b鏄疓鐨勫崟浣嶆祦閲忚垂鐢紝涓旀湁b[i][j] = -b[i][j]錛宖鏄疓鐨勬祦錛屽垯b(f)=鈭?fij*bij)錛?i, j)∈E(G) 涓攆ij>0銆傛渶灝忚垂鐢ㄦ渶澶ф祦闂錛屽氨鏄眰緗戠粶G鐨勬渶澶ф祦f涓斾嬌璐圭敤b(f)鏈灝忋傝繖鏍風殑嫻佺О涓烘渶灝忚垂鐢ㄦ渶澶ф祦銆?/span>
浜屻佺畻娉曟濇兂
      鐢‵ord-Fulkerson綆楁硶鐨勬濇兂錛屼笉鏂湴鍦ㄦ畫鐣欑綉緇滀腑瀵繪壘澧炲箍璺紝鍙笉榪囪繖涓騫胯礬鏄綋鍓嶇綉緇滀腑s鍒皌鐨勪互鍗曚綅嫻侀噺璐圭敤涓烘潈鐨勬渶鐭礬錛屽榪欐潯澧炲箍璺繘琛屾搷浣溿傜敱浜庤垂鐢ㄦ湁璐熷鹼紝寤鴻鐢⊿PFA綆楁硶銆?br>涓夈佺畻娉曚粙緇?br>      鎻忚堪錛?/span>

1 MCMF(G, s, t)
2     for each edge(u, v) in E(G)
3         do f[u, v] = 0
4            f[v, u] = 0
5     while exists a path p from s to t in Gf and p is the shortest path
6         do cf(p) = min{cf(u, v) : (u, v) in p}
7            for each edge(u, v) in p
8                do f[u, v] = f[u, v] + cf(p)
9                   f[v, u] = - f[u, v]
      瀹炵幇錛?br>
 1mcmf()
 2{
 3    while(true)
 4    {
 5        for(int i=1; i<=n+m+1; i++)
 6            d[i] = MAX;
 7        d[s] = 0;
 8        spfa(); //p涓瓨鏈夎鐐圭殑鍓嶇戶鐐?/span>
 9        if(p[t] == -1//琛ㄧず宸叉棤澧炲箍璺?/span>
10            break;
11        int minf = INT_MAX;
12        int it = t;
13        while(p[it] != -1)
14        {
15            minf = min(minf, c[p[it]][it] - f[p[it]][it]);
16            it = p[it];
17        }

18        it = t;
19        while(p[it] != -1)
20        {
21            f[p[it]][it] += minf;
22            f[it][p[it]] = -f[p[it]][it];
23            it = p[it];
24        }

25    }

26}

涓夈佺畻娉曠ず渚?br>      POJ 2516 瑙i鎶ュ憡


Icyflame 2009-06-30 22:29 鍙戣〃璇勮
]]>
POJ 2516 鏈灝忚垂鐢ㄦ渶澶ф祦http://www.shnenglu.com/Icyflame/archive/2009/06/30/88941.htmlIcyflameIcyflameTue, 30 Jun 2009 14:09:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88941.htmlhttp://www.shnenglu.com/Icyflame/comments/88941.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88941.html#Feedback1http://www.shnenglu.com/Icyflame/comments/commentRss/88941.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88941.html闃呰鍏ㄦ枃

Icyflame 2009-06-30 22:09 鍙戣〃璇勮
]]>
鍖歸厤闂http://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.htmlIcyflameIcyflameMon, 29 Jun 2009 06:48:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.htmlhttp://www.shnenglu.com/Icyflame/comments/88604.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88604.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88604.html涓銆佸畾涔変笌瀹氱悊
      鍖歸厤錛氳G(V, E)涓烘棤鐜浘錛岃M涓篍鐨勪竴涓潪絀哄瓙闆嗭紝濡傛灉M涓殑浠繪剰涓ゆ潯杈瑰湪G涓笉鐩擱偦錛屽垯縐癕鏄浘G涓殑涓涓尮閰嶃傝嫢瀵瑰浘G鐨勪換浣曞尮閰峂'錛屽潎鏈墊M'|≤|M|錛屽垯縐癕涓篏鐨勬渶澶у尮閰嶃?br>      楗卞拰鐐癸細璁綧鏄浘G涓殑鍖歸厤錛孏涓笌M涓殑杈瑰叧鑱旂殑欏剁偣縐頒負M楗卞拰鐐癸紝鍚﹀垯縐頒負M闈為ケ鍜岀偣銆傝嫢鍥句腑欏剁偣鍧囨槸M楗卞拰鐐癸紝鍒欑ОM涓篏鐨勫畬緹庡尮閰嶃?br>      M浜ら敊璺細璁綪鏄疓鐨勪竴鏉¤礬錛屼笖鍦≒涓紝M鐨勮竟鍜孍-M鐨勮竟浜ら敊鍑虹幇錛屽垯縐癙鏄疓鐨勪竴鏉浜ら敊璺傝嫢M浜ら敊璺疨鐨勪袱涓鐐逛負M闈為ケ鍜岀偣錛屽垯縐癙涓篗鍙騫胯礬銆?nbsp;
      鏍瑰湪x鐨凪浜ら敊瀛愬浘錛氳x涓篏涓璏闈為ケ鍜岀偣銆侴涓敱璧風偣涓簒鐨凪浜ら敊璺墍鑳借繛鎺ョ殑欏剁偣闆嗘墍瀵煎嚭鐨凣鐨勫鍑哄瓙鍥俱?br>      S鐨勯偦闆嗭細璁維涓篏鐨勪換涓欏剁偣闆嗭紝G涓笌S鐨勯《鐐歸偦鎺ョ殑鎵鏈夐《鐐圭殑闆嗗悎錛岀О涓篠鐨勯偦闆嗭紝璁頒綔N(S)銆?br>      鏈浼樺尮閰嶏細瀵逛簬涓涓姞鏉冧簩閮ㄥ浘錛屼竴涓潈鏈澶х殑鍖歸厤鍙綔鏈浼樺尮閰嶃?br>      鍙瀹氭爣錛氭槧灝刲錛歏(G)→R錛屾弧瓚沖G鐨勬瘡鏉¤竟e={u, v}錛屽潎鏈塴(u)+l(v)≥w(u, v)錛屽叾涓瓀(u, v)琛ㄧず杈筫鐨勬潈錛屽垯縐發涓篏鐨勫彲琛岄《鏍囥備護El={(u, v) | {u, v}∈E(G)錛宭(u)+l(v)=w(u, v)}錛孏l涓轟互El涓鴻竟闆嗙殑G鐨勭敓鎴愬瓙鍥撅紝鍒欑ОGl涓簂絳夊瓙鍥俱?br>
浜屻佹渶澶у尮閰嶏紙鍖堢墮鍒╃畻娉曪級
      鎻忚堪錛?/strong>
      錛?錛塆鏄叿鏈夊垝鍒?V1, V2)鐨勪簩鍒嗗浘錛屼換緇欏垵濮嬪尮閰峂錛?br>      錛?錛夎嫢M楗卞拰V1鍒欑粨鏉燂紱
      錛?錛夊惁鍒欙紝鍦╒1涓壘涓M闈為ケ鍜岀偣錛岋紝緗甋={x}錛?T涓虹┖錛?br>      錛?錛夎嫢N(S) = T錛屽垯鍋滄錛屽惁鍒欎換閫変竴鐐箉∈N(S)-T錛?br>      錛?錛夎嫢y涓篗闈為ケ鍜岀偣錛屽垯姹備竴鏉′粠x鍒皔鐨凪鍙騫胯礬P錛岀疆M涓篗寮傛垨P騫惰漿錛?錛夛紱
      錛?錛夊惁鍒欙紝鐢變簬y鏄疢鐨勯ケ鍜岀偣錛屾晠M涓湁涓杈箋y, u}錛岀疆S = S∪{u}錛孴 = T∪{y}錛岃漿錛?錛夈?br>      瀹炵幇錛?br>

 1 HUNGARY
 2     for i : 1 to |V2|
 3         do match[i] = 0    
 4     for each vertex u in V1
 5         do for i : 1 to |V2|
 6                do visit[i] = false
 7            DFS(u)
 8 DFS(u)
 9     for each vertex v in V2
10         if (u, v) in E(G) and visit[v] is false
11             then visit[v]=true
12                  if match[v] is 0 or DFS(match[v]) is true
13                      then match[v] = u
14                           return true
15     return false

      璇存槑錛?/strong>絎?琛岀殑DFS(u)榪囩▼錛屽綋瀛樺湪浠巙寮濮嬬殑M鍙騫胯礬錛屽垯榪斿洖true錛屽茍瀹屾垚M鐨勬墿灞曪紝姝ゆ椂|M|鍔犱竴銆傚鏋滆繑鍥瀎alse錛屽垯琛ㄧず涓嶅瓨鍦∕鍙騫胯礬銆?nbsp;
      紺轟緥錛?/strong>POJ 1274 瑙i鎶ュ憡銆?/span>
涓夈佹渶浼樺尮閰嶏紙KM綆楁硶錛?br>      鎻忚堪錛?/strong>
      錛?錛変粠浠繪剰鍙欏舵爣l寮濮嬶紝紜畾l絳夊瓙鍥綠l錛屽茍涓斿湪Gl涓夊彇鍖歸厤M銆傝嫢M楗卞拰V1錛屽垯M鏄畬緹庡尮閰嶏紝涔熷嵆M鏄渶浼樺尮閰嶏紝綆楁硶緇堟錛?br>      錛?錛夊惁鍒欙紝榪愮敤鍖堢墮鍒╃畻娉曪紝緇堟浜嶴灞炰簬V1錛孴灞炰簬V2涓斾嬌瀵逛簬Gl錛孨(S)=T銆備護al=min{l(x)+l(y)-w(x, y) | x∈S, y∈V2-T}錛屼護l'(u)=l(u)-al濡傛灉u∈S錛沴'(u)=l(u)+al濡傛灉u∈T錛沴'(u)=l(u)錛屽叾瀹冦傜敤l'浠f浛l錛岀敤Gl'浠f浛Gl杞叆錛?錛夈?br>      瀹炵幇錛?/strong>

 1 KUHN-MUNKRES(G)
 2     for each vertex u in V1
 3         do lx[u] = max{w[u][v] | (u, v) in E(G)}
 4     for each vertex v in V2
 5         do ly[v] = 0
 6     for each vertex u in V1
 7         do while(true)
 8                do for each vertex u in V1
 9                       do vx[u] = false
10                   for each vertex v in V2
11                       do vy[v] = false
12                          slack[v] = MAX
13                   if DFS(u) is true
14                       then break
15                   d = min{slack[v] | v in V2 and vy[v] is false}
16                   for each vertex u in V1
17                       do lx[u] = lx[u] - d
18                   for each vertex v in V2
19                       do ly[v] = ly[v] + d
20 DFS(u)
21     vx[u] = true
22     for each vertex v in V2
23         do if lx[u]+ly[v]==w[u][v] and vy[v] is false
24                then vy[v] = true
25                     if match[v] is NIL or DFS(match[v])
26                         then match[v] = u
27                              return true
28             else if lx[u]+ly[v]>w[u][v]
29                 then slack[v] = min{slack[v], lx[u]+ly[v]-w[u][v]}
30     return false
      紺轟緥錛?/span>POJ 2195 瑙i鎶ュ憡

Icyflame 2009-06-29 14:48 鍙戣〃璇勮
]]>
精品人妻伦一二三区久久| 色偷偷888欧美精品久久久| 久久久久无码精品国产| 久久无码AV一区二区三区| 久久精品亚洲福利| 久久影视综合亚洲| 一本久久a久久精品综合夜夜| 国产精品久久久久久福利漫画 | 久久久久人妻一区二区三区 | 99久久免费国产精品特黄| 理论片午午伦夜理片久久| 久久夜色精品国产亚洲av| 久久人人爽人人爽AV片| 久久综合成人网| 国产香蕉久久精品综合网| 99久久精品免费看国产一区二区三区| 久久乐国产综合亚洲精品| 久久夜色精品国产噜噜麻豆| 狠色狠色狠狠色综合久久| 99热成人精品免费久久| 久久久久香蕉视频| 亚洲欧美伊人久久综合一区二区| 久久精品国产清高在天天线| 一级做a爰片久久毛片16| 亚洲精品高清一二区久久| 精品国产99久久久久久麻豆| 久久99免费视频| 无码人妻少妇久久中文字幕| 久久综合给合久久狠狠狠97色69| 国产精品久久久久久久久鸭| 热久久国产欧美一区二区精品 | 国产亚洲精品久久久久秋霞 | 久久久久无码精品| 精品久久8x国产免费观看| 久久精品国产一区二区电影| 性高湖久久久久久久久| 精品久久久久一区二区三区| 午夜精品久久久久久久久| 久久久久久亚洲精品不卡| 精品国产一区二区三区久久久狼 | 国产69精品久久久久久人妻精品|