<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<!-- DOMjudge version 3.3.2 -->
<title>Source: s1324</title>
<link rel="shortcut icon" href="../images/favicon.png" type="image/png" />
<link rel="stylesheet" href="../style.css" type="text/css" />
<link rel="stylesheet" href="style_jury.css" type="text/css" />
<script type="text/javascript" src="../js/sorttable.js"></script>
<script type="text/javascript" src="../js/domjudge.js"></script>
</head>
<body>

<h2>Source code for submission s1324</h2>

<p><a href="#diff">Go to diff to previous submission</a></p>

<div class="tabber"><div class="tabbertab"><h2 class="filename"><a name="source0"></a>spider.cpp</h2> 

<pre class="cpp" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;algorithm&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;cctype&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;cmath&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;complex&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;cstdio&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;cstring&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;iomanip&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;iostream&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;list&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;map&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;queue&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;set&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;sstream&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;stack&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;string&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;utility&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#include &lt;vector&gt;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define DEBUG(x) cerr &lt;&lt; &quot;&gt;&gt;&gt; &quot; &lt;&lt; #x &lt;&lt; &quot; : &quot; &lt;&lt; x &lt;&lt; endl;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define REP(i,a) for (int i = 0; i &lt; (a); ++i)</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define FOR(i,a,b) for (int i = (a); i &lt;= (b); ++i)</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define FORD(i,a,b) for (int i = (a); i &gt;= (b); --i)</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">bool</span> EQ<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> a, <span style="color: #0000ff;">double</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">fabs</span><span style="color: #008000;">&#40;</span>a<span style="color: #000040;">-</span>b<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color:#800080;">1e-9</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> INF <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">29</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> ll<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666;">//////////////////////////////////</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define MAXN 1111111</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> N, M<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> eA<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span>, eB<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span>, eC<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span>, e<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> father<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span>, rank<span style="color: #008000;">&#91;</span>MAXN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> root<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>father<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> v<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>father<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> root<span style="color: #008000;">&#40;</span>father<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339900;">#define MAX 2222</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> graph<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span>, cost<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> T<span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> DFU<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> edge<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">int</span> a <span style="color: #000080;">=</span> eA<span style="color: #008000;">&#91;</span>edge<span style="color: #008000;">&#93;</span>, b <span style="color: #000080;">=</span> eB<span style="color: #008000;">&#91;</span>edge<span style="color: #008000;">&#93;</span>, c <span style="color: #000080;">=</span> eC<span style="color: #008000;">&#91;</span>edge<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">int</span> ca <span style="color: #000080;">=</span> root<span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span>, cb <span style="color: #000080;">=</span> root<span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666;">//DEBUG(a); DEBUG(b); DEBUG(ca); DEBUG(cb);</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>ca <span style="color: #000080;">==</span> cb<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>rank<span style="color: #008000;">&#91;</span>ca<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> rank<span style="color: #008000;">&#91;</span>cb<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> father<span style="color: #008000;">&#91;</span>ca<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> cb<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>rank<span style="color: #008000;">&#91;</span>ca<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> rank<span style="color: #008000;">&#91;</span>cb<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> father<span style="color: #008000;">&#91;</span>cb<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> ca<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">else</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    father<span style="color: #008000;">&#91;</span>ca<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> cb<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    rank<span style="color: #008000;">&#91;</span>cb<span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  graph<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  graph<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  cost<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  cost<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  T<span style="color: #000040;">++</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> c<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">bool</span> cmp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> eC<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> eC<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> l<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span>, m<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span>, depth<span style="color: #008000;">&#91;</span>MAX<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">void</span> dfs<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v, <span style="color: #0000ff;">int</span> father, <span style="color: #0000ff;">int</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  l<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> father<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  depth<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> d<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  REP<span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">19</span><span style="color: #008000;">&#41;</span> l<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>l<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  REP<span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">19</span><span style="color: #008000;">&#41;</span> m<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> min<span style="color: #008000;">&#40;</span>m<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, m<span style="color: #008000;">&#91;</span>m<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  REP<span style="color: #008000;">&#40;</span>i, graph<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>graph<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> father<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    m<span style="color: #008000;">&#91;</span>graph<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> cost<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    dfs<span style="color: #008000;">&#40;</span>graph<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, v, d<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> up<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v, <span style="color: #0000ff;">int</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  REP<span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">19</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>d <span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> v <span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> v<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> min_up<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> v, <span style="color: #0000ff;">int</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">int</span> ans <span style="color: #000080;">=</span> INF<span style="color: #008080;">;</span> </div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  REP<span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">19</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>d <span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> ans <span style="color: #000080;">=</span> min<span style="color: #008000;">&#40;</span>ans, m<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> v <span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>v<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> ans<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">void</span> build_tree<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  dfs<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> lca<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  FORD<span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">19</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>l<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> a <span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> b <span style="color: #000080;">=</span> l<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>a <span style="color: #000040;">!</span><span style="color: #000080;">=</span> b<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> l<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> get<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>depth<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;</span> depth<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> swap<span style="color: #008000;">&#40;</span>a, b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">int</span> ans <span style="color: #000080;">=</span> min_up<span style="color: #008000;">&#40;</span>a, depth<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> depth<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  a <span style="color: #000080;">=</span> up<span style="color: #008000;">&#40;</span>a, depth<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> depth<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">int</span> l <span style="color: #000080;">=</span> lca<span style="color: #008000;">&#40;</span>a, b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> min<span style="color: #008000;">&#40;</span>ans, min<span style="color: #008000;">&#40;</span>min_up<span style="color: #008000;">&#40;</span>a, depth<span style="color: #008000;">&#91;</span>a<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> depth<span style="color: #008000;">&#91;</span>l<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>, min_up<span style="color: #008000;">&#40;</span>b, depth<span style="color: #008000;">&#91;</span>b<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> depth<span style="color: #008000;">&#91;</span>l<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d%d&quot;</span>, <span style="color: #000040;">&amp;</span>N, <span style="color: #000040;">&amp;</span>M<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">EOF</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    REP<span style="color: #008000;">&#40;</span>i, M<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      <span style="color: #0000ff;">int</span> a, b, c<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      <span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d%d%d&quot;</span>, <span style="color: #000040;">&amp;</span>a, <span style="color: #000040;">&amp;</span>b, <span style="color: #000040;">&amp;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      a<span style="color: #000040;">--</span><span style="color: #008080;">;</span> b<span style="color: #000040;">--</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      eA<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      eB<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> b<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      eC<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> c<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666;">//      graph[a].push_back(b);</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666;">//      graph[b].push_back(a);</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666;">//      cost[a].push_back(c);</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666;">//      cost[b].push_back(c);</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      e<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    REP<span style="color: #008000;">&#40;</span>i, N<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      father<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      rank<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    sort<span style="color: #008000;">&#40;</span>e, e <span style="color: #000040;">+</span> M, cmp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    T <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    REP<span style="color: #008000;">&#40;</span>i, M<span style="color: #008000;">&#41;</span> sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> DFU<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #666666;">//printf(&quot;used: %d\n&quot;, used);</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>T <span style="color: #000040;">!</span><span style="color: #000080;">=</span> N<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;disconnected<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    build_tree<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #0000ff;">int</span> ans <span style="color: #000080;">=</span> sum<span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    REP<span style="color: #008000;">&#40;</span>i, M<span style="color: #008000;">&#41;</span> ans <span style="color: #000080;">=</span> min<span style="color: #008000;">&#40;</span>ans, sum <span style="color: #000040;">-</span> get<span style="color: #008000;">&#40;</span>eA<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, eB<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> eC<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ans<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #008000;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">&#125;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre></div></div><h2><a name="diff"></a>Diff to submission <a href="1243">s1243</a></h2>
<div class="tabber"><div class="tabbertab"><h2 class="filename"><a name="diff"></a>spider.cpp</h2>

<pre class="output_text"><span class='diff-del'>--- c4.s1243.cteam002.spider.cpp.0.spider.cpp</span>
<span class='diff-add'>+++ c4.s1324.cteam002.spider.cpp.0.spider.cpp</span>
@@ -30,5 +30,4 @@
 #define MAXN 1111111
 int N, M;
<span class='diff-del'>-//vector&lt;int&gt; graph[MAXN], cost[MAXN];</span>
 int eA[MAXN], eB[MAXN], eC[MAXN], e[MAXN];
 
@@ -40,5 +39,8 @@
 }
 
<span class='diff-del'>-int used;</span>
<span class='diff-add'>+#define MAX 2222</span>
<span class='diff-add'>+vector&lt;int&gt; graph[MAX], cost[MAX];</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+int T;</span>
 int DFU(int edge){
   int a = eA[edge], b = eB[edge], c = eC[edge];
@@ -52,5 +54,9 @@
     rank[cb]++;
   }
<span class='diff-del'>-  used++;</span>
<span class='diff-add'>+  graph[a].push_back(b);</span>
<span class='diff-add'>+  graph[b].push_back(a);</span>
<span class='diff-add'>+  cost[a].push_back(c);</span>
<span class='diff-add'>+  cost[b].push_back(c);</span>
<span class='diff-add'>+  T++;</span>
   return c;
 }
@@ -60,7 +66,47 @@
 }
 
<span class='diff-add'>+int l[MAX][20], m[MAX][20], depth[MAX];</span>
<span class='diff-add'>+void dfs(int v, int father, int d){</span>
<span class='diff-add'>+  l[v][0] = father;</span>
<span class='diff-add'>+  depth[v] = d;</span>
<span class='diff-add'>+  REP(i, 19) l[v][i+1] = l[l[v][i]][i];</span>
<span class='diff-add'>+  REP(i, 19) m[v][i+1] = min(m[v][i], m[m[v][i]][i]);</span>
<span class='diff-add'>+  REP(i, graph[v].size()) if(graph[v][i] != father){</span>
<span class='diff-add'>+    m[graph[v][i]][0] = cost[v][i];</span>
<span class='diff-add'>+    dfs(graph[v][i], v, d+1);</span>
<span class='diff-add'>+  }</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+int up(int v, int d){</span>
<span class='diff-add'>+  REP(i, 19) if(d &amp; (1 &lt;&lt; i)) v = l[v][i];</span>
<span class='diff-add'>+  return v;</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+int min_up(int v, int d){</span>
<span class='diff-add'>+  int ans = INF; </span>
<span class='diff-add'>+  REP(i, 19) if(d &amp; (1 &lt;&lt; i)){ ans = min(ans, m[v][i]); v = l[v][i]; }</span>
<span class='diff-add'>+  return ans;</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+void build_tree(){</span>
<span class='diff-add'>+  dfs(0, 0, 0);</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+int lca(int a, int b){</span>
<span class='diff-add'>+  FORD(i, 19, 0) if(l[a][i] != l[b][i]){ a = l[a][i]; b = l[b][i]; }</span>
<span class='diff-add'>+  if(a != b) return l[a][0];</span>
<span class='diff-add'>+  return a;</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
<span class='diff-add'>+int get(int a, int b){</span>
<span class='diff-add'>+  if(depth[a] &lt; depth[b]) swap(a, b);</span>
<span class='diff-add'>+  int ans = min_up(a, depth[a] - depth[b]);</span>
<span class='diff-add'>+  a = up(a, depth[a] - depth[b]);</span>
<span class='diff-add'>+  int l = lca(a, b);</span>
<span class='diff-add'>+  return min(ans, min(min_up(a, depth[a] - depth[l]), min_up(b, depth[b] - depth[l])));</span>
<span class='diff-add'>+}</span>
<span class='diff-add'>+</span>
 int main() {
   while(scanf(&quot;%d%d&quot;, &amp;N, &amp;M) != EOF){
<span class='diff-del'>-    int m = 0;</span>
     REP(i, M){
       int a, b, c;
@@ -76,5 +122,4 @@
 //      cost[b].push_back(c);
       e[i] = i;
<span class='diff-del'>-      if(eC[m] &lt; eC[i]) m = i;</span>
     }
     REP(i, N){
@@ -82,12 +127,14 @@
       rank[i] = 0;
     }
<span class='diff-del'>-    eC[m] *= -1;</span>
     sort(e, e + M, cmp);
     int sum = 0;
<span class='diff-del'>-    used = 0;</span>
<span class='diff-add'>+    T = 0;</span>
     REP(i, M) sum += DFU(e[i]);
     //printf(&quot;used: %d\n&quot;, used);
<span class='diff-del'>-    if(used == N-1) printf(&quot;%d\n&quot;, sum);</span>
<span class='diff-del'>-    else printf(&quot;disconnected\n&quot;);</span>
<span class='diff-add'>+    if(T != N-1){ printf(&quot;disconnected\n&quot;); continue; }</span>
<span class='diff-add'>+    build_tree();</span>
<span class='diff-add'>+    int ans = sum;</span>
<span class='diff-add'>+    REP(i, M) ans = min(ans, sum - get(eA[i], eB[i]) - eC[i]);</span>
<span class='diff-add'>+    printf(&quot;%d\n&quot;, ans);</span>
   }
   return 0;
</pre>

</div></div>
</body>
</html>
