Codeforces Round #121 (Div. 1) C. Fools and Roads

time limit per test :2 seconds

memory limit per test : 256 megabytes

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 u**i, v**i (1 ≤ u**i, v**i ≤ n, u**i ≠ v**i), that means that there is a road connecting cities u**i and v**i.

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 a**i, b**i (1 ≤ a**i, b**i ≤ n). That means that the fool number 2i - 1 lives in city a**i and visits the fool number 2i, who lives in city b**i. 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.

Examples

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.

题目大意

给你一棵树,然后给你k个操作,每次操作输入两个整数a b

表示从a 走到b的边的权值都加1

一开始所有权值都为0

最后输出每条边的权值, 按照边输入的顺序

n <= 10^5

Solution

显然树链剖分可做

把边权该做下面的(dep深)的点权。

这样就处理好了

然后在跑一下树链剖分,注意公共祖先不能赋值。

在做的过程中注意边的编号要记录

然后就好了

code

#include<bits/stdc++.h>
#define DEBUG cerr << "Call out at function: " << __func__ << ", In line: " << __LINE__ << " --- "
using namespace std;
vector <int> f[110000];
vector <int> g[110000];
int n;
int w[110000];
int son[110000];
int seg[110000];int pl;
int rev[110000];
int dep[110000];
int top[110000];
int fa[110000];
int id[110000]; long long C[110000]; inline int lowbit(int x){
return x & (-x);
} void add(int x,long long v){
while (x > 0) C[x] += v, x -= lowbit(x);
} long long query(int x){
long long ret = 0;
while (x <= n) ret += C[x], x += lowbit(x);
return ret;
} int DFS1(int fat,int x)
{
fa[x] = fat;
w[x] = 1;
dep[x] = dep[fat] + 1;
int MAX = 0;
for (int i=0;i<f[x].size();i++)
if (f[x][i] != fat){
id[g[x][i]] = f[x][i];
int tmp = DFS1(x,f[x][i]);
w[x] += tmp;
if (tmp > MAX)
son[x] = f[x][i], MAX = tmp;
}
return w[x];
} void DFS2(int x){
seg[x] = ++pl;
rev[pl] = x;
if (son[x] == 0) return;
top[son[x]] = top[x];
DFS2(son[x]);
for (int i=0;i<f[x].size();i++){
if (f[x][i] != son[x] && f[x][i] != fa[x])
top[f[x][i]] = f[x][i], DFS2(f[x][i]);
}
} int add(int x,int y,long long val){
while (top[x] != top[y]){
if (dep[top[x]] < dep[top[y]])
swap(x,y);
add(seg[x],1);
add(seg[top[x]]-1,-1);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x,y);
add(seg[y],1);
add(seg[x],-1);
} int main()
{
cin >> n;
for (int i=1;i<n;i++){
int tp1,tp2;
cin >> tp1 >> tp2;
f[tp1].push_back(tp2);
f[tp2].push_back(tp1);
g[tp1].push_back(i);
g[tp2].push_back(i);
}
DFS1(-1,1);
top[1] = 1,DFS2(1);
int m;
cin >> m;
for (int i=1;i<=m;i++){
int tp1,tp2;
cin >> tp1 >> tp2;
add(tp1,tp2,1);
}
for (int i=1;i<=n-1;i++)
cout << query(seg[id[i]]) << ' ';
}

CF191C Fools and Roads - 树剖解法的更多相关文章

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

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

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

    主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...

  3. [CF191C]Fools and Roads

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

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

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

  5. [CTSC2008]网络管理(整体二分+树剖+树状数组)

    一道经典的带修改树链第 \(k\) 大的问题. 我只想出三个 \(\log\) 的解法... 整体二分+树剖+树状数组. 那不是暴力随便踩的吗??? 不过跑得挺快的. \(Code\ Below:\) ...

  6. 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...

  7. HDU 6162 Ch’s gift (树剖 + 离线线段树)

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  9. POJ2763 Housewife Wind(树剖+线段树)

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy lif ...

随机推荐

  1. 搭建属于自己的图床(基于阿里云OSS,成本9元+20分钟)

    之前的笔记都存在有道云笔记上,慢慢转化为Markdown格式分享出来,这中间遇到了一个图片问题,找了好久,了解到图床,然后找了谷歌插件中微博图床插件,用了一段时间发现,每次都需要登录微博,然后我又是一 ...

  2. Maven - 配置setting.xml

    1.配置本地库路径 <localRepository>C:/fyliu/mvn/repo</localRepository> 2.配置中央仓库 <mirror> & ...

  3. 干货 | 深入分析 string.intern() 方法

    首先我们来看一段代码: public class InternTest {      public static void main(String[] args) {     String str1 ...

  4. Java作业 题目:16版.情人节送玫瑰花

    题目:16版.情人节送玫瑰花 题干: 1.实验要求 本实验要求:以情人节送花为业务背景,体验自定义异常以及异常处理机制. 1-1. 业务说明: 1-1.1. 本实验以情人节送花为业务背景,女方提出送花 ...

  5. C++ Primer: 1. 初识输入和输出

    C++没有定义任何的输入和输出语句,而是使用了 标准库来提供IO机制---iostream; 标准库iostream定义了4种不同的IO对象: cin:     标准输入对象:instream类型的对 ...

  6. ctrNet库介绍

    一个神秘网友写的代码库,膜拜,附上下载链接:https://github.com/guoday/ctrNet-tool 似乎是专门为点击率预估写的库??? 收藏,日后慢慢研究

  7. laravel5.5学习2-路由系统

    一.初识路由 路由系统是所有 PHP 框架的核心,路由承载的是 URL 到代码片段的映射,不同的框架所附带的路由系统是这个框架本质最真实的写照,一丝不挂,一览无余.Laravel 路由中文文档:htt ...

  8. phpstorm配置phpunit进行单元测试

    1.配置单元测试目录: (1)autoload.php <?php function autoloader($dir){ spl_autoload_register(function($name ...

  9. python基础预习小结

    一.执行python程序的两种方式 1.1 交互式 在终端内输入python3,然后输入python代码 1.2 命令式 在终端内输入python3文本文件路径 二.执行python的两种IDE 2. ...

  10. form表单提交被拦截

    最近做的一个项目,利用form表单(可以避免跨域问题)提交,在chrome可以正常进行跳转,但是在手机端,以及一些pc端浏览器却无法正常跳转.通过检查后台日志,没有正常跳转的都是因为后天直接没有收到该 ...