Sergey and Subway
Sergey Semyonovich is a mayor of a county city N N 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 n 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 u u and v v v that were not connected with a direct tunnel but share a common neighbor, i.e. there exists such a station w w w that the original map has a tunnel between u u u and w w w and a tunnel between w w w and v v v. You are given a task to compute the sum of pairwise distances between all pairs of stations in the new map.
Input
The first line of the input contains a single integer n ( 2 ≤ n ≤ 200000 ) n (2 \leq n\leq 200000) n(2≤n≤200000) — the number of subway stations in the imaginary city drawn by mayor’s assistants. Each of the following n − 1 n−1 n−1 lines contains two integers u i u_i ui and v i ( 1 ≤ u i , v i ≤ n , u i ! = v i ) v_i (1\leq u_i,v_i\leq n, u_i!=v_i) 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 n n stations and n − 1 n-1 n−1 tunnels form a tree.
Output
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.
Examples
input
4
1 2
1 3
1 4
output
6
input
4
1 2
2 3
3 4
output
7
Note
In the first sample, in the new map all pairs of stations share a direct connection, so the sum of distances is 6 6 6.
In the second sample, the new map has a direct tunnel between all pairs of stations except for the pair ( 1 , 4 ) (1,4) (1,4). For these two stations the distance is 2 2 2.
-
题意
- 把一棵树上最短路径长度为2的所有点对 ( u , v ) (u,v) (u,v)连边,求所有点对最短路径长度之和,即 ∑ i = 1 n ∑ j = i + 1 n m i n _ d i s ( i , j ) \sum_{i=1}^{n}\sum_{j=i+1}^{n}min\_dis(i,j) i=1∑nj=i+1∑nmin_dis(i,j)
-
题解
- 首先分析这样连边会带来哪些变化,不难想出所有的连边可以分为如下两种情况
- 两个点在同一条单链上「图中红色加粗边表示新加的边,蓝色边和灰色边只是为了说明路径,并不存在」
这种情况下对于当前节点的所有子孙而言,如果在原来的树中某个子孙到当前节点的距离是偶数「比如图中的节点8」 由于每次可以跳一个节点走,所以其到当前节点的最短距离变为原来的一半,而原来的距离是奇数,「比如节点7」那么在新图中虽然能跳着来,单最后一步不能跳,所以其长度为原来的距离的一半再加1,同理这样的规律对于当前节点的祖先到当前节点的距离来说是一致的
- 两个点是同一个父亲的儿子
这种情况下就红色边带来的影响就会难分析一点,我们讨论左边到右边的新的距离规律,在原来的树中到节点4的距离为奇数的子孙节点「比如5这个节点,考虑到3的距离」如果从红边走到3,那么长度为3,而如果先走到1的话,在再走到3这样路径长度为2,显然更优,同理你可以分析到2的距离,走先走到父亲节点1不会变差,而对于 v在原来的树上到4的距离为偶数的子孙节点「比如节点6」这时候还先走到1的话就会很蠢,很容易发现走新加入的红边显然更优
- 两个点在同一条单链上「图中红色加粗边表示新加的边,蓝色边和灰色边只是为了说明路径,并不存在」
- 有了这个规律就好办了,就如同我的那篇博客所讲的那样,先DFS一遍子树获得子树的答案,然后再DFS一遍获得每个节点以上的节点的答案即可,两个加起来就是以当前节点为终点的所有路径长度答案,最后除以2,因为每对路径统计了两次
- 然而写完发现大佬们的代码都超级短 233 233 233
- 首先分析这样连边会带来哪些变化,不难想出所有的连边可以分为如下两种情况
-
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
typedef long long ll;
int n,u,v,sum[maxn][2][2];
ll dp[maxn][2][2]; //第二维表示当前节点的上还是下,第三维表示路径长度是奇数还是偶数
vector<int> vec[maxn];
void dfs(int cur,int fa)
{
for(int i=0;i<vec[cur].size();i++){
if(vec[cur][i]!=fa){
dfs(vec[cur][i],cur);
sum[cur][0][0]+=sum[vec[cur][i]][0][1];
sum[cur][0][1]+=sum[vec[cur][i]][0][0]+1;
dp[cur][0][0]+=dp[vec[cur][i]][0][1];
dp[cur][0][1]+=dp[vec[cur][i]][0][0]+sum[vec[cur][i]][0][0]+1;
}
}
}
ll solve(int cur,int fa)
{
ll res=dp[cur][0][0]+dp[cur][0][1]+dp[cur][1][0]+dp[cur][1][1],sum0=0,sum1=0,tot0=0,tot1=0;int son=0;
for(int i=0;i<vec[cur].size();i++) if(vec[cur][i]!=fa) {
sum0+=dp[vec[cur][i]][0][0];
sum1+=dp[vec[cur][i]][0][1];
tot0+=sum[vec[cur][i]][0][0];
tot1+=sum[vec[cur][i]][0][1];
son++;
}
for(int i=0;i<vec[cur].size();i++) if(vec[cur][i]!=fa){
dp[vec[cur][i]][1][0]+=dp[cur][1][1];
dp[vec[cur][i]][1][1]+=dp[cur][1][0]+sum[cur][1][0]+1;
dp[vec[cur][i]][1][0]+=sum0-dp[vec[cur][i]][0][0]+tot0-sum[vec[cur][i]][0][0]+(son-1);
dp[vec[cur][i]][1][1]+=sum1-dp[vec[cur][i]][0][1]+tot1-sum[vec[cur][i]][0][1];
sum[vec[cur][i]][1][0]+=sum[cur][1][1];
sum[vec[cur][i]][1][1]+=sum[cur][1][0]+1;
sum[vec[cur][i]][1][0]+=tot0-sum[vec[cur][i]][0][0]+son-1;
sum[vec[cur][i]][1][1]+=tot1-sum[vec[cur][i]][0][1];
res+=solve(vec[cur][i],cur);
}
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d %d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1,0);
printf("%lld\n",solve(1,0)/2);
}