题意

题目链接

Sol

很套路的题

直接考虑每个边的贡献,最后再把奇数点的贡献算上

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define rg register
#define pt(x) printf("%d ", x);
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
#define chmin(x, y) (x = x < y ? x : y)
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e18 + 10, mod = 1e9 + 7;
const double eps = 1e-9;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, dep[MAXN], num[MAXN], siz[MAXN], ans, a2;
vector<int> v[MAXN];
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
num[dep[x] & 1]++;
siz[x] = 1;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i]; if(to == fa) continue;
dfs(to, x);
siz[x] += siz[to]; }
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i]; if(to == fa) continue;
ans += siz[to] * (N - siz[to]);
} }
main() {
N = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(1, 0);
for(int i = 1; i <= N; i++) a2 += num[(dep[i] & 1) ^ 1];
printf("%lld", (ans + a2 / 2) / 2);
return 0;
}

cf1060E. Sergey and Subway(树形dp)的更多相关文章

  1. [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 ...

  2. CF1060E Sergey and Subway(点分治)

    给出一颗$N$个节点的树,现在我们**在原图中**每个不直接连边但是中间只间隔一个点的两个点之间连一条边. 比如**在原图中**$u$与$v$连边,$v$与$w$连边,但是$u$与$w$不连边,这时候 ...

  3. CF1060E Sergey and Subway 思维

    分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...

  4. CF上部分树形DP练习题

    本次 5 道题均来自Codeforce 关于树形DP的算法讲解:Here 791D. Bear and Tree Jumps 如果小熊每次能跳跃的距离为1,那么问题变为求树上任意两点之间距离之和. 对 ...

  5. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  6. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  7. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  8. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  9. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

随机推荐

  1. Spring Boot 入门系列

    本系列博文版权 简书 面皮大师 所有,转载请标明原文及出处: http://www.jianshu.com/u/062bd8f1299c 项目地址: https://github.com/daleiw ...

  2. java_对象序列化

    对象序列化(serializable) 序列化读:ObjectInputStream  ois=new ObjectInputStream(new FileInputStream("./gg ...

  3. 【算法笔记】B1052 卖个萌

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805273883951104 #include <math ...

  4. axiso 生产环境跨域配置(可用)

    1.npm install axios 后 在main.js中import import Axios from 'axios'Vue.prototype.$http = Axios 2.请求配置 th ...

  5. [性能测试]:ISO8583报文解析实例

    现在我们有ISO8583报文如下(十六进制表示法): 60 00 03 00 00 60 31 00 31 07 30 02 00 30 20 04 C0 20 C0 98 11 00 00 00 0 ...

  6. Service Discovery protocol(SDP)

    locating services provided by Volume 3 , Part C , section 8 2.1sdp client-server architecture 2.2 se ...

  7. Getway网关管理ZUUL

    1.ZUUL微服务网关 微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服 ...

  8. HTML5学习笔记:HTML5基于本地存储SQLite的每日工作任务清单程序.[只支持chrome]

    使用环境:Chrome 36.0...+ 技术:HTML5 目的:习练HTML5 功能概述:记录管理每天工作内容,便签清单 HTML5+CSS3呈现UI,JavaScript操作数据库,SQLite存 ...

  9. Linq to SharePoint与权限提升(转)

    转自http://www.cnblogs.com/kaneboy/archive/2012/01/25/2437086.html SharePoint 2010支持Linq to SharePoint ...

  10. 如何处理服务器SSL收到了一个弱临时Diffie-Hellman 密钥?

    当我们用火狐浏览器打开某个HTTPS网站时可能会失败,并且出现如下错误提示:         安全连接失败连接某个URL网址时发生错误. 在服务器密钥交换握手信息中 SSL 收到了一个弱临时 Diff ...