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 ...
随机推荐
- macOS在virtualenv中使用wxPython报错
在虚拟的Python环境中运行GUI的软件报错 This program needs access to the screen. Please run with a Framework build o ...
- vue router获取整条路径参数
$route.path 当前路由对象的路径,如'/vi$route.query 请求参数,如/foo?user=1获取到query.user = 1$route.router 所属路由器以及所属组件信 ...
- (转)从一道面试题彻底搞懂hashCode与equals的作用与区别及应当注意的细节
背景:学习java的基础知识,每次回顾,总会有不同的认识.该文系转载 最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的, ...
- vue学习(3)
回顾昨天内容 1.let和const 2.模板字符串 `` 插变量${变量名} 3.箭头函数 function(){} == ()=>{} 1.this的指向问题 2.arguments不能使用 ...
- JS 判断各种设备,各种浏览器
话不多说,直接看代码 1.区分Android.iphone.ipad: var ua = navigator.userAgent.toLowerCase(); if (/android|adr/gi. ...
- 数位DP+其他
参考资料: [1]:数位dp总结 之 从入门到模板 [2]:浅谈数位DP 题目一览表 来源 考察知识点 A 4352 "XHXJ's LIS" hdu 数位DP+状压DP+LIS ...
- Good Bye 2018
Good Bye 2018 2018年最后一场CF,OVER! 弱弱的我只能做出3道A,B,D~~~~ 最后几分钟,感觉找到了C题的规律,结束的那一刻,提交了一发 "Wrong answer ...
- diff和patch
diff -u:the unified format会将不同的地方放在一起,紧凑易读 . diff original.txt updated.txt c表示在original文件中的m,n行的内容将要 ...
- systemd 编写
转载文章:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html https://wizardforcel.gitb ...
- python机器学习-sklearn挖掘乳腺癌细胞(三)
python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...