猴猴的比赛 dfs序

两颗\(n\)节点的树,不相同,问多少点对\((u,v)\)在两棵树上均满足路径\(v\)在\(u\)子树中

\(n\le 10^5\)

暴力:

\(n^2\)暴力枚举点对用\(dfs\)序\(O(1)\)判断是非满足条件,或者用欧拉序\(O(1)\)求lca

正解:

先跑第一棵树,求出其\(dfs\)序,记录下节点\(i\)的\(dfs\)序开始与结束位置。

然后跑第二棵树,维护一个下标为\(dfs\)序的树状数组,每次第一次遍历到节点\(i\)时,我们统计在当前节点的\(dfs\)序之前(即满足在第一棵树上节点\(i\)在\(j\)的子树中)且在当前这第二棵树上已经遍历过的节点(即满足在第二棵树上节点\(i\)在\(j\)的子树中)的个数,加入到答案。这个过程相当于统计每个\((u,v)\)中的\(v\)。

具体看代码实现吧。

#include <cstdio>
#define MAXN 100001
using namespace std;
inline int read(){
char ch=getchar();int s=0;
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+(ch^'0'), ch=getchar();
return s;
}
int n;
int tre[MAXN];
void add(int x, int val){
while(x<=n)
tre[x]+=val,x+=x&(-x);
}
int get_sum(int x){
int res=0;
while(x>0)
res+=tre[x],x-=x&(-x);
return res;
}
int dfn[MAXN],dfn_out[MAXN],cnt;
int ans[MAXN];
namespace tre1 {
int head[MAXN],nxt[MAXN*2],vv[MAXN*2],tot;
inline void add_edge(int u, int v){
vv[++tot]=v;
nxt[tot]=head[u];
head[u]=tot;
}
void dfs(int u, int fa){
dfn[u]=++cnt;
for(int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(v==fa) continue;
dfs(v, u);
}
dfn_out[u]=cnt;
}
}
namespace tre2 {
int head[MAXN],nxt[MAXN*2],vv[MAXN*2],tot;
inline void add_edge(int u, int v){
vv[++tot]=v;
nxt[tot]=head[u];
head[u]=tot;
}
void solve(int u, int fa){
ans[u]=get_sum(dfn[u]-1);
add(dfn[u], 1);
add(dfn_out[u], -1);
for(int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(v==fa) continue;
solve(v, u);
}
add(dfn[u], -1);
add(dfn_out[u], 1);
}
}
int main(){
//freopen("climb.in", "r", stdin);
//freopen("climb.out", "w", stdout);
n=read();
for(int i=1;i<n;++i){
int u=read(),v=read();
tre1::add_edge(u, v);
tre1::add_edge(v, u);
}
for(int i=1;i<n;++i){
int u=read(),v=read();
tre2::add_edge(u, v);
tre2::add_edge(v, u);
}
tre1::dfs(1, 1);
tre2::solve(1, 1);
long long sum=0;
for(int i=1;i<=n;++i) sum+=ans[i];
printf("%lld", sum);
return 0;
}

猴猴的比赛 dfs序的更多相关文章

  1. HDU 5877 [dfs序][线段树][序]

    /* 题意: n个点的树,每个点给定一个权值,给定一个k,求任意一点的子树中,权值小于k/该点权值的点共有多少个. 思路: 1.很明显的子树的操作,应用dfs序. 2.比赛的时候傻逼了,一直在调划分树 ...

  2. 蓝皮书:异象石 【dfs序+lca】

    题目详见蓝皮书[算法竞赛:进阶指南]. 题目大意: 就是给你一颗树,然后我们要在上面进行三种操作:  1.标记某个点  或者  2.撤销某个点的标记  以及   3.询问标记点在树上连通所需的最短总边 ...

  3. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  4. 沈阳网络赛J-Ka Chang【分块】【树状数组】【dfs序】

    Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, ...

  5. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  6. 树的dfs序 && 系统栈 && c++ rope

    利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...

  7. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  8. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  9. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

随机推荐

  1. Online Hard Example Mining 理解

    Definition: Online Hard Example Mining (OHEM) is a way to pick hard examples with reduced computatio ...

  2. jdbcUrl is required with driverClassName

    https://blog.csdn.net/newbie_907486852/article/details/81391525 springboot2.0配置多数据源: spring.datasour ...

  3. .net core使用ocelot---第五篇 服务质量

    简介 .net core使用ocelot---第一篇 简单使用  .net core使用ocelot---第二篇 身份验证使用 .net core使用ocelot---第三篇 日志记录  .net c ...

  4. 转 js一个简单实用的弹出层

      关闭 点击查看 >> <html> <head> <title>新文件标题</title> <script type=" ...

  5. iOS - 小功能 跳转到淘宝或天猫的商品展示详情页

    最近做iOS项目的时候遇到一个需求,点击广告页,跳转到淘宝或天猫的商品详情页. 具体需要是这样: 1)安装了淘宝:跳转到淘宝详情页. 2)没装淘宝,装了天猫:跳转到天猫详情页 3)淘宝.天猫都没装:跳 ...

  6. flutter报错--ProcessException: Process... gradlew.bat ...exited abnormally

    在 VScode 中 debug flutter 是遇到如下问题: ProcessException: Process "G:\demo\flutter\hello_word\android ...

  7. 解决此报错:Cannot create Woodstox XMLInputFactory: java.lang.NoClassDefFoundError: com/ctc/wstx/stax/WstxInput

    最近在研究webservice服务的技术,可是写完webservice例子后,项目正常启动,但是在输入url地址后报如下的错误: Cannot create Woodstox XMLInputFact ...

  8. HDFS读流程

    客户端先与NameNode通信,获取block位置信息,之后线性地先取第一个块,然后接二连三地获取,取回一个块时会进行MD5验证,验证通过后会使read顺利进行完,当最终读完所有的block块之后,拼 ...

  9. Linux记录history命令

    对后面的(1)方法的提取文件改写法(强烈推荐) 记录存储位置: mkdir -p /usr/local/records chmod 777 /usr/local/records/chmod +t /u ...

  10. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...