F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Cotree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2738    Accepted Submission(s): 862


Problem Description
Avin has two trees which are not connected. He asks you to add an edge between them to make them connected while minimizing the function $\sum_{i=1}^{n} \sum_{j=i+1}^{n} dis(i,j)$, where $dis(i,j)$ represents the number of edges of the path from $i$ to $j$. He is happy with only the function value.
 

Input
The first line contains a number $n \ (2<=n<=100000)$. In each of the following $n-2$ lines, there are two numbers $u$ and $v$, meaning that there is an edge between $u$ and $v$. The input is guaranteed to contain exactly two trees.
 

Output
Just print the minimum function value.
 

Sample Input
3 1 2
 

Sample Output
4
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 07:57:49, Gzip enabled