Description

有一颗 \(n\) 个节点的树,\(k\) 次旅行,问每一条边被走过的次数。

Input

第一行一个整数 \(n\) (\(2\leq n\leq 10^5\))。

接下来 \(n-1\) 行,每行两个正整数 \(x,y\) (\(1\leq x,y\leq n,x\neq y\)),表示 \(x\)与 \(y\) 之间有一条连边。

接下来一个整数 \(k\) (\(0\leq k\leq 10^5\) )。

接下来 \(k\) 行,每行两个正整数 \(x,y\) (\(1\leq x,y\leq n\) ),表示有一个从 \(x\) 到 \(y\) 的旅行。

Output

共\(n-1\)个整数,中间由一个空格隔开。

这不是一道裸的边差分 emmm.

不过是输出一下每条边的被经过次数罢了。

不了解树上差分的来这里

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
#define R register using namespace std; const int gz=1e5+8; inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
} int head[gz],tot,ans[gz],n,k; struct cod{int u,v;}edge[gz<<1]; inline void add(R int x,R int y)
{
edge[++tot].u=head[x];
edge[tot].v=y;
head[x]=tot;
} int depth[gz],f[gz][21],cnt[gz]; void dfs(R int u,R int fa)
{
f[u][0]=fa;depth[u]=depth[fa]+1;
for(R int i=1;(1<<i)<=depth[u];i++)
f[u][i]=f[f[u][i-1]][i-1];
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs(edge[i].v,u);
}
} inline int lca(R int x,R int y)
{
if(depth[x]>depth[y])swap(x,y);
for(R int i=20;i>=0;i--)
if(depth[x]+(1<<i)<=depth[y])
y=f[y][i];
if(x==y)return y;
for(R int i=20;i>=0;i--)
{
if(f[x][i]==f[y][i])continue;
x=f[x][i],y=f[y][i];
}
return f[x][0];
} void dfs2(R int u,R int fa)
{
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs2(edge[i].v,u);
cnt[u]+=cnt[edge[i].v];
ans[(i+1)>>1]=cnt[edge[i].v];
}
} signed main()
{
in(n);
for(R int i=1,x,y;i<n;i++)
{
in(x),in(y);
add(x,y);add(y,x);
}
dfs(1,0);
in(k);
for(R int i=1,x,y;i<=k;i++)
{
in(x),in(y);
R int la=lca(x,y);
cnt[x]++,cnt[y]++,cnt[la]-=2;
}
dfs2(1,0);
for(R int i=1;i<n;i++)printf("%lld ",ans[i]);
}

LCA+差分【CF191C】Fools and Roads的更多相关文章

  1. CF191C Fools and Roads - 树剖解法

    Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...

  2. [CF191C]Fools and Roads

    题目大意:有一颗$n$个节点的树,$k$次旅行,问每一条被走过的次数. 题解:树上差分,$num_x$表示连接$x$和$fa_x$的边被走过的次数,一条路径$u->v$,$num_u+1,num ...

  3. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

  4. Fools and Roads CodeForces - 191C

    Fools and Roads CodeForces - 191C 题意:给出一棵n个节点的树,还有树上的k条简单路径(用路径的两个端点u和v表示),对于树上每一条边,求出其被多少条简单路径经过. 方 ...

  5. NOIP2015 运输计划(二分+LCA+差分)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 308  Solved: 208[Submit][Status] ...

  6. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  7. [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]

    参考: 1. 郭华阳 - 算法合集之<RMQ与LCA问题>. 讲得很清楚! 2. http://www.cnblogs.com/lazycal/archive/2012/08/11/263 ...

  8. 题解 CF191C 【Fools and Roads】

    树上差分半裸题 常规思路是进行三次DFS,然后常规运算即可 这里提供两次dfs的思路(wyz tql orz) 我们以样例2为例 我们考虑任意一条路径,令其起点为u终点为v,每走一次当前路径则v的访问 ...

  9. bzoj3631[JLOI2014 松鼠的新家 倍增lca+差分

    裸的树上差分+倍增lca 每次从起点到终点左闭右开,这就有一个小技巧,要找到右端点向左端点走的第一步,然后差分就好了 #include<cstdio> #include<cstrin ...

随机推荐

  1. 2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)

    题解: (并查集处理往上跳的时候,一定要先让u,v往上跳到并查集的祖先,不然会wa掉) 代码如下: #include <iostream> #include <algorithm&g ...

  2. SRM709 div1 Xscoregame(状压dp)

    题目大意: 给定一个序列a,包含n个数(n<=15),每个数的大小小于等于50 初始时x = 0,让你每次选a中的一个数y,使得x = x + x^y 问如何安排选择的次序,使得最终结果最大. ...

  3. [bzoj2893] 集合计数

    Description 一个有N个元素的集合有2^N 个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取法的方案数,答案模100000000 ...

  4. git使用笔记(三)文件忽略

    By francis_hao    Nov 19,2016 注:此条所有内容均来自$ git help gitignore,细节请参考之   有时候在仓库里有一些文件我们并不想提交,git提供了指定屏 ...

  5. Mybatis LIKE模糊查询

    1.在代码中拼接好字符串后传入进来 2.使用CONCAT在xml中拼接字符串: <if test="queryParam.keyword != null"> AND b ...

  6. [bzoj 1208]STL水过

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 看网上的题解都用的手写数据结构……然而直接用set的lower_bound就水过去了 ...

  7. AnnotationConfigApplicationContext.的用法的核心代码

    public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationCont ...

  8. TCP ------ TCP创建服务器中出现的套接字

    在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求.这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回 ...

  9. 取消eslint对指定代码进行代码检测

    eslint配置了不允许使用alert,但是有个需求需要用到. //eslint-disable-next-line alert('测试'); 如上,即可跳过当前行代码检查了

  10. jQuery知识点:attr与prop的区别

    做项目时遇到个莫名的问题,全选的时候仅第一次有效,再次点击全选按钮是无效了,查了查原因,看到篇很不错的文章,问题出在jquery中的attr属性上,这里做下笔记. 原文链接:http://www.cn ...