CodeForces - 1060E Sergey and Subway —— 树上任意两点之间的距离和

82 篇文章 1 订阅

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.

Input
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.

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
inputCopy
4
1 2
1 3
1 4
outputCopy
6
inputCopy
4
1 2
2 3
3 4
outputCopy
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.

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.

题意:

给你一棵树,所有共享一个邻居的两个点可以新建一条边,所有边的长度为1,问你任意两点之间的距离的和最小是多少。

题解:

我一开始是想着用以前那种排列路径和来做来着,但是这道题有点不一样,不能直接看孩子与祖父节点的两边有多少点,因为并不知道有多少点是用这条路的,若是解决这个问题会变得很麻烦,所以可以直接将所有边两端的点数相乘再加起来除二,但是有一种情况是需要用到原来的边,就是深度不同奇偶的情况,那么这种就是(边数+1)/2,所以只需要再加上奇偶数量相乘再除二即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll son[N];
int fa[N],odd;
struct node
{
    int to,next;
}e[N*2];
int cnt,head[N];
void add(int x,int y)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt++;
}
int n;
ll ans;
void dfs(int x,int f,int od)
{
    odd+=od;
    for(int i=head[x];~i;i=e[i].next)
    {
        int ne=e[i].to;
        if(ne==f)
            continue;
        dfs(ne,x,od^1);
        son[x]+=son[ne]+1;
        ans+=(ll)(son[ne]+1)*(n-son[ne]-1);
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<n;i++)
        scanf("%d%d",&x,&y),add(x,y),add(y,x),fa[y]=x;
    dfs(1,0,1);
    ans+=(ll)(n-odd)*odd;
    printf("%lld\n",ans/2);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值