题目链接

题意

给一棵树,对于一个节点,与它相邻的结点可以连一条边,求所有点对间距离之和

思路

任意两点间的距离被优化为$\left \lceil \frac{s}{2} \right \rceil$,转化为任意两点间距加间距为奇数的路径数,即$\frac{\sum_{i\in G,j\in G,i<j}dis[i][j]+\sum_{i\in G,j\in G,i<j[dis[i][j]=1(mod 2)]}}{2}$,奇数路径可由深度奇偶性不同的点数相乘得到,即$\sum_{i\in G}dep[i]=1(mod 2)*\sum_{i\in G}dep[i]=0(mod2)$

代码

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm> #define IOS ios::sync_with_stdio(0),cin.tie(0);
#define DBG(x) cerr << #x << " = " << x << endl; using namespace std; typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL; const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps = 1e-8;
const double pi = acos(-1.0); void file(){
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
} const int maxn = 2e5+5;
const int maxm = 4e5+5; int n;
int head[maxn],tot;
int dep[maxn],vis[maxn];
LL cnt0,cnt1;
LL siz[maxn]; struct edgenode{
int to,next;
}edge[maxm]; void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int x,int dep){
siz[x]=1;
if(vis[x] == inf){
vis[x]=dep;
if(abs(dep)%2 == 0)cnt0++;
else cnt1++;
}
for(int i=head[x];i != -1;i=edge[i].next){
int v=edge[i].to;
if(vis[v] == inf){
dfs(v,dep+1);
siz[x]+=siz[v];
}
}
} namespace BakuretsuMahou{
void Explosion(){
memset(head,-1,sizeof head);
memset(vis,0x3f,sizeof vis);
scanf("%d",&n);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(1,0);
LL ans=cnt0*cnt1;
for(int i=1;i<=n;i++)
ans+=siz[i]*(1LL*n-siz[i]);
printf("%I64d\n",ans/2);
}
} int main(){
//IOS
//file();
BakuretsuMahou::Explosion();
return 0;
}

  

Codeforces 1060E(dfs计数)的更多相关文章

  1. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  2. Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree (dfs计数,树)

    题意:给你含有\(n\)个节点,\(n-1\)条边的树,以及\(m\)个质数和\(1\),你需要在这\(m\)个质数和一个\(1\)选择数(质数只能选一次,\(1\)可以多选)给\(n-1\)条边赋值 ...

  3. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

  4. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  5. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  6. CodeForces 510B DFS水题

    题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<c ...

  7. Tree Requests CodeForces - 570D (dfs水题)

    大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...

  8. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  9. Military Problem CodeForces - 1006E(dfs搜一下 标记一下)

    题意: 就是有一颗树  然后每次询问 父结点 的 第k个结点是不是他的子嗣...是的话就输出这个子嗣..不是 就输出-1 解析: 突然想到后缀数组的sa 和 x的用法..就是我们可以用一个id标记当前 ...

随机推荐

  1. ubuntu 16.04 LTS - 谷歌拼音输入法

    https://blog.csdn.net/chengyq116/article/details/78638249 1. installation1.1 汉语语言包 sudo apt-get inst ...

  2. java 服务端I/O非阻塞实现05

    /** * 非阻塞IO多线线程服务端 * 当一个任务进入多线程,这个任务线程需要处理接收信息.发送信息.因而发生I/O阻塞问题 * 利用selector可以实现异步 * */ public class ...

  3. 一款DMA性能优化记录:异步传输和指定实时信号做async IO

    关键词:DMA.sync.async.SIGIO.F_SETSIG. DMA本身用于减轻CPU负担,进行CPU off-load搬运工作. 在DMA驱动内部实现有同步和异步模式,异步模式使用dma_a ...

  4. 基于 HTML5 的 WebGL 楼宇自控 3D 可视化监控

    前言 智慧楼宇和人们的生活息息相关,楼宇智能化程度的提高,会极大程度的改善人们的生活品质,在当前工业互联网大背景下受到很大关注.目前智慧楼宇可视化监控的主要优点包括: 智慧化 -- 智慧楼宇是一个生态 ...

  5. You earned your Program Management Professional (PgMP)® Credential

    You earned your Program Management Professional (PgMP)® Credential. pasting

  6. Android自动化测试手段之Monkey(adb shell monkey)

    一. 什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序 ...

  7. 开发中的Date处理

    数据库中的日期格式有以下几种: date:年-月-日 time:时:分:秒 datatime:年-月-日 时:分:秒 timestrap: 例如,生日显示格式为'年-月-日',而创建/更新时间格式为' ...

  8. Github经理和员工开发

    Git简介 Git是目前世界上最先进的分布式版本控制系统 git的两大特点: 版本控制:可以解决多人同时开发的代码问题,也可以解决找回历史代码的问题 分布式:Git是分布式版本控制系统,同一个Git仓 ...

  9. ubuntu fiddler firefox http网页不能访问 Secure Connection Failed

    1. 给firefox导入fiddler的证书 1) fiddler:tools --> fiddler opthins --> https --> 勾选Capture HTTPS ...

  10. union的特性,去重与不去重

    转载:https://blog.csdn.net/kingmax54212008/article/details/33762921 union的特性,去重与不去重 集合操作有 并,交,差 3种运算. ...