﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-心無雜念的空白-文章分类-图论</title><link>http://www.cppblog.com/lshain/category/17131.html</link><description>Beyond this world...</description><language>zh-cn</language><lastBuildDate>Tue, 21 Jun 2011 23:21:34 GMT</lastBuildDate><pubDate>Tue, 21 Jun 2011 23:21:34 GMT</pubDate><ttl>60</ttl><item><title>树的中心</title><link>http://www.cppblog.com/lshain/articles/149049.html</link><dc:creator>Lshain</dc:creator><author>Lshain</author><pubDate>Mon, 20 Jun 2011 09:30:00 GMT</pubDate><guid>http://www.cppblog.com/lshain/articles/149049.html</guid><description><![CDATA[<a href="http://acm.timus.ru/problem.aspx?space=1&amp;num=1056">http://acm.timus.ru/problem.aspx?space=1&amp;num=1056</a> 
<h2 class="problem_title">1056. Computer Net</h2>
<div class="problem_limits">Time Limit: 2.0 second<br />Memory Limit: 16 MB<br /></div>
<div id="problem_text">
<h3 class="problem_subtitle">Background</h3>
<div class="problem_par">
<div class="problem_par_normal">Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the number of its parent computer in the net. Thus, protocol consists of several numbers; the first of them is always 1, because the second computer can only be connected to the first one, the second number is 1 or 2 and so forth. The total quantity of numbers in the protocol is <em>N</em>&nbsp;&#8722;&nbsp;1 (<em>N</em> is a total number of computers). For instance, protocol 1, 1, 2, 2 corresponds to the following net: </div></div>
<div class="problem_par_pre">
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tbody>
<tr>
<td><pre class="intable">1 - 2 - 5
|   |
3   4
            </pre></td></tr></tbody></table></div>
<div class="problem_par">
<div class="problem_par_normal">The distance between the computers is the quantity of mutual connections (between each other) in chain. Thus, in example mentioned above the distance between computers #4 and #5 is 2, and between #3 and #5 is 3.</div></div>
<div class="problem_par">
<div class="problem_par_normal"><em>Definition.</em> Let the center of the net be the computer which has a minimal distance to the most remote computer. In the shown example computers #1 and #2 are the centers of the net.</div></div>
<h3 class="problem_subtitle">Problem</h3>
<div class="problem_par">
<div class="problem_par_normal">Your task is to find all the centers using the set protocol. </div></div>
<h3 class="problem_subtitle">Input</h3>
<div class="problem_par">
<div class="problem_par_normal">The first line of input contains an integer <em>N</em>, the quantity of computers (2&nbsp;&#8804;&nbsp;<em>N</em>&nbsp;&#8804;&nbsp;10000). Successive <em>N</em>&nbsp;&#8722;&nbsp;1 lines contain protocol. </div></div>
<h3 class="problem_subtitle">Output</h3>
<div class="problem_par">
<div class="problem_par_normal">Output should contain ordinal numbers of the determined net centers in ascending order.</div></div>
<h3 class="problem_subtitle">Sample</h3>
<table style="width: 542px; height: 108px" class="sample">
<colgroup>
<col width="350">
<col width="350">
<tbody>
<tr>
<th>input</th>
<th>output</th></tr>
<tr>
<td style="width: 247px; height: 78px"><pre class="intable">5
1
1
2
2</pre></td>
<td style="width: 352px"><pre class="intable">1 2
            </pre></td></tr></tbody></table>
<h3><span style="font-size: 10pt">给定一棵树，求该树的中心。</span><br /><span style="font-size: 10pt">中心定义：到其它点的距离最大值最小</span><br /><span style="font-size: 10pt">树的中心即为树中最长链的中心，2次dfs即可<br /><br />Code<br /></span><span style="font-size: 10pt">#include&lt;cstdio&gt;</span><cstdio><span style="font-size: 10pt"><br />struct Node{</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;int n;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;int next;</span><br /><span style="font-size: 10pt">}EE[20001];</span><br /><span style="font-size: 10pt"><br />int ind;</span><br /><span style="font-size: 10pt">int n;</span><br /><span style="font-size: 10pt">int top;</span><br /><span style="font-size: 10pt">int head[10001];</span><br /><span style="font-size: 10pt">int check[10001];</span><br /><span style="font-size: 10pt">int pre[10001];</span><br /><span style="font-size: 10pt">int maxDep;</span><br /><span style="font-size: 10pt">void dfs(int u, int t){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;int q;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;check[u] = 1;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;if(t &gt; maxDep){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxDep = t;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind = u;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;q = head[u];</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;while(q != -1){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!check[EE[q].n]){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre[EE[q].n] = u;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(EE[q].n, t + 1);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q = EE[q].next;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">}</span><br /><span style="font-size: 10pt"><br />int main(int argc, char* argv[], char* env[])</span><br /><span style="font-size: 10pt">{</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;int i = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;int j = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;while(scanf("%d", &amp;n) != EOF){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i = 1; i &lt;= n; i++){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head[i] = -1;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check[i] = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i = 2; i &lt;= n; i++){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d", &amp;j);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EE[top].n = i;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EE[top].next = head[j];</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head[j] = top++;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EE[top].n = j;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EE[top].next = head[i];</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head[i] = top++;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxDep = -1;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(1, 1);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i = 1; i &lt;= n; i++){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check[i] = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre[i] = -1;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxDep = -1;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(ind, 1);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = ind;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top = 0;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(i != -1){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check[top++] = i;</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = pre[i];</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(top&amp;1){</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n", check[top&gt;&gt;1]);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else{</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d %d\n", check[(top&gt;&gt;1) - 1], check[top&gt;&gt;1]);</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;return 0;</span><br /><span style="font-size: 10pt">}<br />&nbsp;</span></h3></div> <img src ="http://www.cppblog.com/lshain/aggbug/149049.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lshain/" target="_blank">Lshain</a> 2011-06-20 17:30 <a href="http://www.cppblog.com/lshain/articles/149049.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>