CF1060E.Sergey and Subway
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Sergey Semyonovich is a mayor of a county city N and he used to spend his days and nights in thoughts of further improvements of Nkers' lives. Unfortunately for him, anything and everything has been done already, and there are no more possible improvements he can think of during the day (he now prefers to sleep at night). However, his assistants have found a solution and they now draw an imaginary city on a paper sheet and suggest the mayor can propose its improvements.
Right now he has a map of some imaginary city with n subway stations. Some stations are directly connected with tunnels in such a way that the whole map is a tree (assistants were short on time and enthusiasm). It means that there exists exactly one simple path between each pair of station. We call a path simple if it uses each tunnel no more than once.
One of Sergey Semyonovich's favorite quality objectives is the sum of all pairwise distances between every pair of stations. The distance between two stations is the minimum possible number of tunnels on a path between them.
Sergey Semyonovich decided to add new tunnels to the subway map. In particular, he connected any two stations u and v that were not connected with a direct tunnel but share a common neighbor, i.e. there exists such a station w that the original map has a tunnel between u and w and a tunnel between w and v. You are given a task to compute the sum of pairwise distances between all pairs of stations in the new map.
谢尔盖·谢苗诺维奇是某县城市 N 的市长,他过去常常日夜思索如何进一步改善市民 Nkers 的生活。然而,对他而言不幸的是,所有能想到的改进措施都已付诸实施,白天他再也想不出任何新的改进方案(如今他更愿意晚上睡觉)。不过,他的助手们找到了一个解决方案:他们现在在一张纸上画出一座虚构的城市,并建议市长可以对这座虚构城市提出改进意见。
目前,他拥有一张包含 n 个地铁站的虚构城市地图。某些车站之间通过隧道直接相连,整张地图构成一棵树(助手们当时时间与热情都不足)。这意味着任意两个车站之间恰好存在一条简单路径。我们称一条路径为“简单路径”,当且仅当它至多经过每条隧道一次。
谢尔盖·谢苗诺维奇最钟爱的质量目标之一,便是所有车站对之间的两两距离之和。两个车站之间的距离定义为连接它们的路径上所需经过的最少隧道数量。
谢尔盖·谢苗诺维奇决定向地铁地图中添加新的隧道。具体来说,他对所有未被直接隧道连接、但拥有一个共同邻接站的车站对 u 和 v 添加一条新隧道;即:存在某个车站 w,使得原地图中已存在 u 与 w 之间的隧道,以及 w 与 v 之间的隧道。你的任务是计算新地图中所有车站对之间的两两距离之和。
输入格式
The first line of the input contains a single integer n (2≤n≤200000) — the number of subway stations in the imaginary city drawn by mayor's assistants. Each of the following n−1 lines contains two integers ui and vi (1≤ui,vi≤n, ui=vi), meaning the station with these indices are connected with a direct tunnel.
It is guaranteed that these n stations and n−1 tunnels form a tree.
输入的第一行包含一个整数 n(2≤n≤200000),表示市长助手所绘制的虚构城市中的地铁站数量。接下来的 n−1 行中,每行包含两个整数 ui 和 vi(1≤ui,vi≤n,且 ui=vi),表示编号为 ui 和 vi 的地铁站之间有一条直接隧道相连。
保证这 n 个地铁站与 n−1 条隧道构成一棵树。
输出格式
Print one integer that is equal to the sum of distances between all pairs of stations after Sergey Semyonovich draws new tunnels between all pairs of stations that share a common neighbor in the original map.
输出一个整数,该整数等于在谢尔盖·谢苗诺维奇根据原始地图中所有具有公共邻居的车站对之间新建隧道后,所有车站对之间的距离之和。
输入输出样例
输入#1
4 1 2 1 3 1 4
输出#1
6
输入#2
4 1 2 2 3 3 4
输出#2
7
说明/提示
In the first sample, in the new map all pairs of stations share a direct connection, so the sum of distances is 6.
In the second sample, the new map has a direct tunnel between all pairs of stations except for the pair (1,4). For these two stations the distance is 2.
在第一个样例中,新地图中所有车站对之间都存在直达连接,因此距离之和为 6。
在第二个样例中,新地图中除车站对 (1,4) 外,其余所有车站对之间均存在直达隧道;而车站 1 与车站 4 之间的距离为 2。
输入解题思路,AI测评打分。不知道怎么写?