CF 1060E. Sergey and Subway
题目链接
题意:给你一棵树,然后连接两个有公共邻居的点,问你连完后,任意两点的距离之和。
一开始看这种题,还不怎么会做,借鉴了这位大佬的博客,get到了新技能,当我们求树上任意俩点的距离之时,可以转化问题,不看点,而看边,每条边的使用次数是固定的,每条边使用的次数为:这条边左边的顶点数*右边的顶点数,而由于我们可以将相隔一个点的两个点连起来,所以,如果是偶数的距离,我们可以2个2个跳,就是距离的一半,奇数呢就是(距离+1)/2,而奇数的距离只能在偶数和奇数层产生,所以用dp[now][2]dp[now][2]dp[now][2]记录当前节点nownownow的层数,dp[now][1]dp[now][1]dp[now][1]表示由上一个节点pre出发,要经过nownownow才能到的点数(包括自己),那么(n−dp[now][1])∗dp[now][1](n-dp[now][1])*dp[now][1](n−dp[now][1])∗dp[now][1]就表示pre−>nowpre->nowpre−>now这条边的使用次数。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const int N=2e5+11;
vector<int>v[N];
int n,dp[N][3];
void dfs(int now,int pre,int sta){
dp[now][1]++;
dp[now][2]=sta;
for(int k:v[now]){
if(k==pre)continue;
dfs(k,now,sta^1);
dp[now][1]+=dp[k][1];
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<n;i++){
int s,t;
cin>>s>>t;
v[s].pb(t);
v[t].pb(s);
}
dfs(1,0,0);
LL ans,res;
ans=res=0;
for(int i=1;i<=n;i++){
ans+=dp[i][1]*(n-dp[i][1]);
res+=dp[i][2];
}
ans+=res*(n-res);
cout<<ans/2;
return 0;
}
CF 1060E. Sergey and Subway的更多相关文章
- 1060E Sergey and Subway(思维题,dfs)
题意:给出一颗树,现在,给哪些距离为2的点对,加上一条边,问所有点对的距离和 题解:如果没有加入新的边,距离和就会等于每条边的贡献,由于是树,我们用点来代表点上面的边,对于每条边,它的贡献将是(子树大 ...
- 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】
学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...
- [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 ...
- E. Sergey and Subway
比赛时候写复杂了…… 我写的是 计算每个节点树内所有点到某个点的距离和. #include <bits/stdc++.h> using namespace std; typedef lon ...
- CF1060E Sergey and Subway(点分治)
给出一颗$N$个节点的树,现在我们**在原图中**每个不直接连边但是中间只间隔一个点的两个点之间连一条边. 比如**在原图中**$u$与$v$连边,$v$与$w$连边,但是$u$与$w$不连边,这时候 ...
- CF上部分树形DP练习题
本次 5 道题均来自Codeforce 关于树形DP的算法讲解:Here 791D. Bear and Tree Jumps 如果小熊每次能跳跃的距离为1,那么问题变为求树上任意两点之间距离之和. 对 ...
- [CF]Round513
A Phone Numbers 题意:定义"电话号码"为开头为'8',长度为11的字符串.给定一些字符,每个字符只能用一次,求可以拼出多少个电话号码(可以重复). 直接min(st ...
随机推荐
- A1030. Travel Plan
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- SourceInsight快捷键
下载安装与设置 https://blog.csdn.net/k346k346/article/details/77412413 常用设置总结的还是比较全面的 问题: SourceInsight4.0中 ...
- k短路(A*)
http://poj.org/problem?id=2449 #include <cstdio> #include <cstdlib> #include <cstring ...
- R语音:解决cor.test报错的 'y'必需是数值矢量
'y'必需是数值矢量,产生该类报错可能是含有NA值. 只需要在该数值上加入as.double函数即可.见下命令: ##先测试是不是数值型 is.numeric(data[,2]) #[1] FALSE ...
- Nginx上部署HTTPS + HTTP2
Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev(或者OpenSSL),且ln -s /usr/lib/x86_64-linux-gnu/libssl.so ...
- Spring 学习笔记一
1.IOC,DI. 2.装配bean基于xml(实例化,声明周期,后处理bean,属性注入).3.装配bean基于注解 1 spring框架概述 1.1 什么是spring l Sp ...
- eclipse导出svn源码,如何转化为项目
1.先导出 2.点击项目右键,选“属性” 3.选择project facets 4.添加对应的支持 5.可进行进一步配置,设置name,然后点击确定等待完成
- 5.django学习模型
##Django Models ##编写models 1.在应用根目录下创建models.py并引入models模块,创建类,继承models.Model该类即是一张数据表 2.字段创建 ##映射数据 ...
- mybatis下载地址(所有版本)
https://github.com/mybatis/mybatis-3/releases,这个github里面几乎包含了所有的没有batis
- 保存指定目录及其子目录的jpg文件
import os txt_path = 't1.txt' f = open(txt_path, mode='a', encoding='utf-8') def all_path(dirname): ...