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 ...
随机推荐
- [BJOI2017]开车
[BJOI2017]开车 直接做要用栈 修改?难以直接维护 统计边的贡献! len*abs(pre)pre表示前缀car-stop 修改时候,整个区间的pre+1或者-1 分块,块内对pre排序并打标 ...
- JQuery未来元素事件监听写法
$(document).on('click','.div1',function(){ alert("abc"); }); 格式一致,第一个参数写事件,第二个参数给谁写事件(选择器) ...
- C# Winfrom 发送邮件验证码&Timer控件
邮件发送: //定义一个全局的string类型的验证码: string yzm = ""; //定义一个字符串,这里面包含所有需要的验证码的元素: string a = " ...
- A1138. Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- Vue+koa2开发一款全栈小程序(5.服务端环境搭建和项目初始化)
1.微信公众平台小程序关联腾讯云 腾讯云的开发环境是给免费的一个后台,但是只能够用于开发,如果用于生产是需要花钱的,我们先用开发环境吧 1.用小程序开发邮箱账号登录微信公众平台 2.[设置]→[开发者 ...
- IntelliJ IDEA Cannot resolve symbol ''
study from : https://www.cnblogs.com/linmengfei/p/7909196.html File->Invalidate Caches 点击File | I ...
- Day6--Python--小数据池和再谈编码
一.小数据池 参考 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建过多的对象 缓存: int, str, bool int: 缓存范围 -5~256 str: 1. 长度小于等于1,直接缓 ...
- bigdata learning unit two--Spark environment setting
1.下载 Spark安装之前的准备 文件的解压与改名 tar -zxvf spark-2.2.0-bin-hadoop2.7.tgz rm -rf spark-2.2.0-bin-hadoop2.7. ...
- (链表) leetcode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- jQuery_parent() parents() closest()区别
parent 是找当前元素的第一个父节点,不管匹不匹配都不继续往下找 parents 是找当前元素的所有父节点 closest 是找当前元素的所有父节点 ,直到找到第一个匹配的父节点 <%@ ...