E. Sergey and Subway
比赛时候写复杂了……
我写的是 计算每个节点树内所有点到某个点的距离和。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
vector<int> g[maxn];
int son[maxn];
ll d[maxn]; ///树内所有点到某个点的距离和
int odd[maxn];
int even[maxn];
void dfs1(int u, int fa)
{
d[u] = ;
son[u] = ;
son[u]++;
even[u] = ;
for(int i = ; i < (int)g[u].size(); i++)
{
int v = g[u][i];
if(v == fa) continue;
dfs1(v, u);
son[u] += son[v];
even[u] += odd[v];
d[u] += d[v] + son[v];
}
odd[u] = son[u] - even[u];
// printf("%d %I64d\n", u, d[u]);
}
ll ans = ;
void dfs2(int u, int fa)
{
for(int i = ; i < (int)g[u].size(); i++)
{
int v = g[u][i];
if(v == fa) continue;
d[v] = d[v] + (d[u] - d[v] - (ll)son[v]) + (ll)(son[u] - son[v]);
odd[v] += (son[u] - son[v] - odd[u] + even[v]);
son[v] = son[u];
ans += ((d[v] + odd[v]) / 2LL);
dfs2(v, u);
}
}
int main()
{
int n; scanf("%d", &n);
for(int i = ; i <= n - ; i++)
{
int u, v; scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(, );
dfs2(, );
ans += (d[] + odd[]) / 2LL;
printf("%lld\n", ans / 2LL);
return ;
}
Code
实际上这题只需要计算树上任意两点的距离和,枚举每条边的贡献就行了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
vector<int> g[maxn];
int n;
ll ans, odd, even;
int dfs(int u, int fa, int dep)
{
if(dep % ) odd++;
else even++;
int son = ; ///统计子树大小
for(int i = ; i < (int)g[u].size(); i++)
{
int v = g[u][i];
if(v == fa) continue;
int sing = dfs(v, u, dep + );
ans += (ll)sing * (n - sing); ///枚举每条边的贡献
son += sing;
}
return son;
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n - ; i++)
{
int u, v; scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(, , );
ans = (ans + (ll)odd * even) / ;
printf("%lld\n", ans);
return ;
}
Code
E. Sergey and Subway的更多相关文章
- 1060E Sergey and Subway(思维题,dfs)
题意:给出一颗树,现在,给哪些距离为2的点对,加上一条边,问所有点对的距离和 题解:如果没有加入新的边,距离和就会等于每条边的贡献,由于是树,我们用点来代表点上面的边,对于每条边,它的贡献将是(子树大 ...
- CF 1060E. Sergey and Subway
题目链接 题意:给你一棵树,然后连接两个有公共邻居的点,问你连完后,任意两点的距离之和. 一开始看这种题,还不怎么会做,借鉴了这位大佬的博客,get到了新技能,当我们求树上任意俩点的距离之时,可以转化 ...
- [CF1060E]Sergey and Subway[树dp]
题意 给出 \(n\) 个点的树,求 \(\sum_{i=1}^n{\sum_{j=i}^n{\lceil \frac{dis(i,j)}{2} \rceil}}\) . \(n\leq 2 \tim ...
- CF1060E Sergey and Subway 思维
分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...
- cf1060E. Sergey and Subway(树形dp)
题意 题目链接 Sol 很套路的题 直接考虑每个边的贡献,最后再把奇数点的贡献算上 #include<bits/stdc++.h> #define Pair pair<int, in ...
- CF1060E Sergey and Subway(点分治)
给出一颗$N$个节点的树,现在我们**在原图中**每个不直接连边但是中间只间隔一个点的两个点之间连一条边. 比如**在原图中**$u$与$v$连边,$v$与$w$连边,但是$u$与$w$不连边,这时候 ...
- 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】
学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...
- Codeforces Round #513 游记
Codeforces Round #513 游记 A - Phone Numbers 题目大意: 电话号码是8开头的\(1\)位数字.告诉你\(n(n\le100)\)个数字,每个数字至多使用一次.问 ...
- Codeforces Round #513 by Barcelona Bootcamp
A. Phone Numbers 签. #include <bits/stdc++.h> using namespace std; #define N 110 char s[N]; ], ...
随机推荐
- 配置基于Vim的Python开发环境
配置基于Vim的Python开发环境插件 Vundle YouCompleteMe NERDTree Vim-Jinja2-Syntax set nocompatible " be iMpr ...
- 关于host,nslookup,dig 的安装
host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...
- debian安装中文字体
debian刚安装完成之后,因为没有中文字体,会出现方框. 安装中文字体: $ su # apt-get install fonts-arphic-bkai00mp fonts-arphic-bsmi ...
- Linux学习-以最新核心版本编译 CentOS 7.x 的核心
为了某些缘故需要最新的 4.x.y 的核心版本来实作某些特定的功能时,那该 如何是好?没办法,只好使用最新的核心版本来编译你可以依照上面的程序来一个一个处理, 没有问题~不过,你也可以根据 ELRep ...
- Python虚拟机函数机制之位置参数的默认值(五)
位置参数的默认值 在Python中,允许函数的参数有默认值.假如函数f的参数value的默认值是1,在我们调用函数时,如果传递了value参数,那么f调用时value的值即为我们传递的值,如果调用时没 ...
- JAVA 基础--开发环境 vscode 搭建
对于使用 Visual Studio Code 的 Java 开发者来说,Language Support for Java(TM) by Red Hat 扩展提供了非常好的语言特性支持,比如智能感知 ...
- iOS 9下支持的键盘类型:
http://blog.csdn.net/cloudox_/article/details/50532124
- js常见的方法
Ajax请求 jquery ajax函数 我自己封装了一个ajax的函数,代码如下: var Ajax = function(url, type success, error) { $.ajax({ ...
- 使用PYTHON创建XML文档_python
当用GOOGLE查的时候,内容几乎都是一样的.但是你想要的东西,一个也没有.例如,我就找不到中国人写的如何使用PYTHON来创建一个XML文件.当然,直接用文件写的方式也能够达到同样的效果,但是毕竟容 ...
- AtCoder Regular Contest 080
手贱去开了abc,这么无聊.直接arc啊 C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Prob ...