They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost).

But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths.

A simple path is the path which goes through every Berland city not more than once.

The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it.

Note how the fools' paths are given in the input.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of cities.

Each of the next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ nui ≠ vi), that means that there is a road connecting cities ui and vi.

The next line contains integer k (0 ≤ k ≤ 105) — the number of pairs of fools who visit each other.

Next k lines contain two space-separated numbers. The i-th line (i > 0) contains numbers ai, bi (1 ≤ ai, bi ≤ n). That means that the fool number 2i - 1 lives in city ai and visits the fool number 2i, who lives in city bi. The given pairs describe simple paths, because between every pair of cities there is only one simple path.

Output

Print n - 1 integer. The integers should be separated by spaces. The i-th number should equal the number of fools who can go on the i-th road. The roads are numbered starting from one in the order, in which they occur in the input.

Sample test(s)
input
5
1 2
1 3
2 4
2 5
2
1 4
3 5
output
2 1 1 1 
input
5
3 4
4 5
1 4
2 4
3
2 3
1 3
3 5
output
3 1 1 1 
Note

In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.

In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1.

给出一棵树,树有边权,边权初始都为0,给出k个操作,每一个操作给出u v

表示u,v路径上的所有边权都加1

所有操作完后,按照边输入的顺序输出每一条边的边权

一看,树链剖分裸题

但是想想,其实是可以不用树链剖分做的

可以用lca

我们令root=1

dis[i]表示节点i到root的距离

对于每一个操作u v,相当于

dis[u]++

dis[v]++

dis[lca(u,v)]-=2

所有操作完后,我们再一次dfs(root),自底向上累加所有dis,就可以了

dis[u]+=dis[son(u)]

然后按照输入的顺序输出边权就好啦

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<set> #define LL long lon
#define pb push_back using namespace std; const int inf=0x3f3f3f3f;
const int maxn=1e5+; int dis[maxn];
int dep[maxn];
int dp[maxn][];
int e[maxn][]; struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot; void init_edge()
{
memset(head,-,sizeof head);
tot=;
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void solve(int ); int main()
{
init_edge();
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d %d",&e[i][],&e[i][]);
addedge(e[i][],e[i][]);
addedge(e[i][],e[i][]);
}
solve(n); return ;
} void dfs0(int u,int pre)
{
dep[u]=dep[pre]+;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
dp[v][]=u;
dfs0(v,u);
}
} void init_dp(int n)
{
for(int j=;(<<j)<=n;j++){
for(int i=;i<=n;i++){
if(dp[i][j-]!=-)
dp[i][j]=dp[dp[i][j-]][j-];
}
}
} int query_lca(int a,int b)
{
if(dep[a]<dep[b])
swap(a,b);
int cnt;
for(cnt=;(<<cnt)<=dep[a];cnt++)
;
cnt--;
for(int j=cnt;j>=;j--){
if(dep[a]-(<<j)>=dep[b])
a=dp[a][j];
}
if(a==b)
return a;
for(int j=cnt;j>=;j--){
if(dp[a][j]!=- && dp[a][j]!=dp[b][j]){
a=dp[a][j];
b=dp[b][j];
}
}
return dp[a][];
} void dfs1(int u)
{
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==dp[u][])
continue;
dfs1(v);
dis[u]+=dis[v];
}
} void solve(int n)
{
memset(dp,-,sizeof dp);
memset(dep,,sizeof dep);
memset(dis,,sizeof dis);
dfs0(,);
init_dp(n);
int q;
scanf("%d",&q);
for(int i=;i<=q;i++){
int u,v;
scanf("%d %d",&u,&v);
dis[u]++;
dis[v]++;
dis[query_lca(u,v)]-=;
}
dfs1(); for(int i=;i<n;i++){
int cur=e[i][];
if(dep[e[i][]]<dep[e[i][]])
cur=e[i][];
printf("%d",dis[cur]);
i==n-?puts(""):printf(" ");
}
return ;
}

CF 191C Fools and Roads lca 或者 树链剖分的更多相关文章

  1. [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 ...

  2. 【BZOJ3626】LCA(树链剖分,Link-Cut Tree)

    [BZOJ3626]LCA(树链剖分,Link-Cut Tree) 题面 Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. ...

  3. uva 12655 Trucks [LCA](树链剖分+MST)

    The Subtle Balloons Company (SBC) is the main balloon provider for programming contests; it hashuge ...

  4. LCA 倍增||树链剖分

    方法1:倍增 1498ms #include <iostream> #include <cstdio> #include <algorithm> #include ...

  5. 从lca到树链剖分 bestcoder round#45 1003

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  6. P3379 【模板】最近公共祖先(LCA)(树链剖分)版

    #include <bits/stdc++.h> #define read read() #define up(i,l,r) for(register int i = (l);i < ...

  7. 2018.09.16 bzoj3626: [LNOI2014]LCA(树链剖分)

    传送门 树链剖分好题. 对于每个点维护一个值vi" role="presentation" style="position: relative;"&g ...

  8. BZOJ 3626 LCA(离线+树链剖分)

    首先注意到这样一个事实. 树上两个点(u,v)的LCA的深度,可以转化为先将u到根路径点权都加1,然后求v到根路径上的总点权值. 并且该题支持离线.那么我们可以把一个区间询问拆成两个前缀和形式的询问. ...

  9. JZYZOJ1454 NOIP2015 D2T3_运输计划 二分 差分数组 lca tarjan 树链剖分

    http://172.20.6.3/Problem_Show.asp?id=1454 从这道题我充分认识到我的脑子里好多水orz. 如果知道了这个要用二分和差分写,就没什么思考上的难点了(屁咧你写了一 ...

随机推荐

  1. 网站后台的lnmp启动与重启

    网站建立时间很长了,经常挂掉,又没有其他技术人员带.只好自己摸索着修复. 到今天网站已经挂掉了一个礼拜.请求各路大神无果后决定自己修复. 首先出现的是502,网关错误. 1.上阿里云服务用户中心重新启 ...

  2. hdu5438(2015长春赛区网络赛1002)拓扑序+DFS

    题意:给出一张无向图,每个节点有各自的权值,问在点数为奇数的圈中的点的权值总和是多少. 通过拓扑序的做法标记出所有非圈上的点,做法就是加每条边的时候将两点的入度都加一,然后将所有度数为1的点入队,删去 ...

  3. Windows Phone使用总结(长期更新)

    [感受和经历] 1,型号,撸妹640XL: 2,经历,去银行办卡,当然各种潜规则要我装APP,然后小妹夺我手机要帮我安装,拿过去之后又还给我了--哈哈哈哈,不过乐极生悲,我以为能成功躲过去了,发现中国 ...

  4. PCA和Softmax分类比较—Mnist与人脸数据集

    PCA人脸识别中三种方法得到的正确率可达到100% 作为对比,单独使用Softmax回归对人脸40*10*92*112的数据分类正确率为97%. 用PCA对MNIST手写数字10*500*28*28识 ...

  5. 什么是Spring的命名空间及使用Spring 的命名空间p 装配属性

    这个就要从XML说了,Spring的配置管理可以利用XML方式进行配置,而XML里面就有命名空间这个概念..实际上就和标签的意思有点像 你给一个命名空间以后,这个XML文件里面就可以用那个命名空间上下 ...

  6. 虚拟化之vmware-截图解释

    故障检测和主机网络隔离 代理会相互通信,并监控群集内各台主机的活跃度.默认情况下,此操作通过每秒交换一次检测信号来完成.如 果15 秒过去后仍未收到检测信号,而且 ping 不到该主机,则系统会声明该 ...

  7. wikioi 1202 求和(求n个数的和)

    /*============================================================= 1202 求和 题目描述 Description 求n个数的和 输入描述 ...

  8. android绘画折线图二

    紧接着android绘画折线图一,下面来介绍第二种方法,使用该方法,首先需要一个Androidplot-core-0.4.3-release.jar,该jar包之后也包含在项目源码中 建立一个andr ...

  9. ES6 中的 Set、Map 和 WeakMap

    Set 是 ES6 新增的有序列表集合,它不会包含重复项. Set 支持 add(item) 方法,用来向 Set 添加任意类型的元素,如果已经添加过则自动忽略: has(item) 方法用来检测 S ...

  10. MySQL在一台db服务器上面如何启动多个实例 (转)

    安装过程省略过,源码安装请参考http://write.blog.csdn.net/postlist/1609043/all 整理自己的文档,发现以前做的例子,share下,欢迎大家提出改进意见. 一 ...