﻿<?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++博客-Initiate-随笔分类-DP</title><link>http://www.cppblog.com/initiate/category/13450.html</link><description>Call A Spade a Spade</description><language>zh-cn</language><lastBuildDate>Fri, 09 Apr 2010 11:55:53 GMT</lastBuildDate><pubDate>Fri, 09 Apr 2010 11:55:53 GMT</pubDate><ttl>60</ttl><item><title>SGU134Centroid </title><link>http://www.cppblog.com/initiate/archive/2010/04/08/112002.html</link><dc:creator>Initiate</dc:creator><author>Initiate</author><pubDate>Thu, 08 Apr 2010 12:58:00 GMT</pubDate><guid>http://www.cppblog.com/initiate/archive/2010/04/08/112002.html</guid><wfw:comment>http://www.cppblog.com/initiate/comments/112002.html</wfw:comment><comments>http://www.cppblog.com/initiate/archive/2010/04/08/112002.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/initiate/comments/commentRss/112002.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/initiate/services/trackbacks/112002.html</trackback:ping><description><![CDATA[<p align=center><strong>&nbsp;&nbsp;134. Centroid </p>
</strong>
<p align=center>time limit per test: 0.50 sec. <br>memory limit per test: 4096 KB </p>
<p align=justify></p>
<p align=justify>You are given an undirected connected graph, with <strong>N</strong> vertices and <strong>N-1</strong> edges (a tree). You must find the centroid(s) of the tree. <br>In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex <strong>k</strong>. If we remove the vertex <strong>k</strong> from the tree (along with its adjacent edges), the remaining graph will have only <strong>N-1</strong> vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex <strong>k</strong> is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex <strong>k</strong>. All the vertices for which the associated value is minimum are considered centroids. </p>
<p align=justify></p>
<strong>
<p align=justify>Input</p>
</strong>
<p align=justify>The first line of the input contains the integer number <strong>N</strong> (<strong>1&lt;=N&lt;=16 000</strong>). The next <strong>N-1</strong> lines will contain two integers, <strong>a</strong> and <strong>b</strong>, separated by blanks, meaning that there exists an edge between vertex <strong>a</strong> and vertex <strong>b</strong>. </p>
<p align=justify></p>
<strong>
<p align=justify>Output</p>
</strong>
<p align=justify>You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order. </p>
<p align=justify></p>
<p align=justify>Sample Input</p>
<font face="Courier New">
<pre>7
1 2
2 3
2 4
1 5
5 6
6 7
</pre>
<p align=justify></p>
</font>
<p align=justify>Sample Output</p>
<font face="Courier New">
<pre>3 1
1</pre>
<pre>给一棵n个节点的树，求这棵树的重心。<br>树的重心就是是指删掉它以后，<br>剩下的最大的连通子树的节点数是最少的点。</pre>
<pre>因为树是保证连通的，所以从任何一个节点都可以把这棵树拎起来。</pre>
<pre>所以可以从任何一个点去DFS（返回子节点个数+1）</pre>
<pre>
<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: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;pk(x)&nbsp;push_back(x)</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">16010</span><span style="COLOR: #000000">;<br>vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">g[N];&nbsp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0x7fffffff</span><span style="COLOR: #000000">,num;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">n为边数，value为最小权值，num为重心个数&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;res[N],vis[N];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;u)</span><span style="COLOR: #008000">//<br></span><span style="COLOR: #000000">{<br>&nbsp;&nbsp;&nbsp;&nbsp;vis[u]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,tmp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">k是节点u的value.Sum是u的所有子节点的个数&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">g[u].size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;v</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">g[u][i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">vis[v]){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">dfs(v);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">tmp)&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">k(f[u])=max{sum[v]+1}sum[v]是v的所有子节点个数+v自己&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sum;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">k(f[u])=max{k,n-1-sum}</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">将该节点和该节点的所有子节点划去的剩余部分&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(k</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">tmp)&nbsp;k</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">k){</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求所有节点中最小的权值&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">k;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">发现更小的，置num为1&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">u;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(value</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">k){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res[</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">num]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">u;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;sum</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,a,b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</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">&lt;</span><span style="COLOR: #000000">n;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[a].pk(b);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[b].pk(a);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;dfs(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d&nbsp;%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,value,num);<br>&nbsp;&nbsp;&nbsp;&nbsp;sort(res</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,res</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">num</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(num</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,res[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">num;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,res[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>}<br></span></div>
</pre>
</font>
<img src ="http://www.cppblog.com/initiate/aggbug/112002.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/initiate/" target="_blank">Initiate</a> 2010-04-08 20:58 <a href="http://www.cppblog.com/initiate/archive/2010/04/08/112002.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>