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]; ], ...
随机推荐
- JS正则表达式学习总结
JS正则:java RegExp对象,它是对字符串执行模式匹配的强大工具.运用最多的就是在输入处验证输入的字符串是否合法,指定用户输入字符串的格式. 定义方法: 1:直接量语法:var re=/pat ...
- graph-Dijkstra's shortest-path alogorithm
直接贴代码吧,简明易懂. 后面自己写了测试,输入数据为: a b c d e 0 1 4 0 2 2 1 2 3 1 3 2 1 4 3 2 1 1 2 3 4 2 4 5 4 3 1 也就是课本上1 ...
- 转:跟我一起写Makefile (PDF重制版)
原文地址:http://seisman.info/how-to-write-makefile.html 其它一些问题 不妨看一下:http://blog.csdn.net/huyansoft/art ...
- idea Live Template 快速使用
善用LiveTemplates,好用到没朋友,我凑揍 , 尊重原创,原文链接: https://blog.csdn.net/u012721933/article/details/52461103#co ...
- webdriver高级应用- 右键另存为下载文件
1.要使用右键另存,需要先按照第三方工具AutoIt: 链接: https://pan.baidu.com/s/12aBBhOOTmyQpH9hukt0XGA 密码: fcdk 2.创建一个名为loa ...
- day03_03 第一个Python程序
Python的安装 之前安装了python2.7.11,接下来的课程因为是python3的,需要再安装一个python3版本...... 1.进入python.org进行选择安装 2.或者选择课件里的 ...
- Leetcode 433.最小基因变化
最小基因变化 一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任 ...
- [python学习篇][书籍学习][python standrad library][内建函数]之[all,any,basestring,isinstance,bin,bool,@classmethod,@staticmethod,cmp,enumerate
Python 解释器内置了一些函数,它们总是可用的.这里将它们按字母表顺序列出. Built-in Functions abs() divmod() input() open() st ...
- [python学习篇] [os模块] [2]删除文件夹
def deleteDirectory(self,current_path): if not os.path.exists(current_path): self.logger.info(curren ...
- TOJ4537: n阶行列式
4537: n阶行列式 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 28 ...