主题链接~~>

做题情绪:做了HDU 5044后就感觉非常easy了。

解题思路:

先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作。经过树链剖分后,就会形成很多链,可是每条边都有编号,相当于一个数组进行线性操作,这样。如果在 u  ~ v 去都添加 1 。那么能够让 sum [ u ] += 1 ; sum [ v + 1 ] -= 1 ; 这里如果 v 的编号大。

最后的时候仅仅要从后往前遍历一次就能够了。得到全部的结果。明确这点后再加上树链剖分的思想就能够攻克了。

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT __int64
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1e9 + 7 ;
const int MY = 1400 + 5 ;
const int MX = 100000 + 5 ;
int n ,num ,idx ,m ;
int head[MX] ,sum[MX] ,ans[MX] ,P[MX] ,ti[MX] ,dep[MX] ,top[MX] ,siz[MX] ,son[MX] ,father[MX] ;
struct NODE
{
int u ,v ;
}e[MX] ;
struct node
{
int v ,next ;
}E[MX*2] ;
void addedge(int u ,int v)
{
E[num].v = v ; E[num].next = head[u] ; head[u] = num++ ;
E[num].v = u ; E[num].next = head[v] ; head[v] = num++ ;
}
void dfs_find(int u ,int fa)
{
dep[u] = dep[fa] + 1 ;
siz[u] = 1 ;
son[u] = 0 ;
father[u] = fa ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == fa) continue ;
dfs_find(v ,u) ;
siz[u] += siz[v] ;
if(siz[son[u]] < siz[v]) son[u] = v ;
}
}
void dfs_time(int u ,int fa)
{
ti[u] = idx++ ;
top[u] = fa ;
if(son[u]) dfs_time(son[u] ,top[u]) ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == father[u] || v == son[u]) continue ;
dfs_time(v ,v) ;
}
}
void LCA(int u ,int v)
{
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
swap(u ,v) ;
sum[ti[u]+1] -= 1 ;
sum[ti[top[u]]] += 1 ;
u = father[top[u]] ;
}
if(dep[u] > dep[v])
swap(u ,v) ;
if(u != v)
{
sum[ti[son[u]]] += 1 ;
sum[ti[v]+1] -= 1 ;
}
}
int main()
{
//freopen("input.txt" ,"r" ,stdin) ;
int u ,v ;
while(~scanf("%d" ,&n))
{
num = 0 ;
memset(head ,-1 ,sizeof(head)) ;
memset(sum ,0 ,sizeof(sum)) ;
for(int i = 1 ;i < n ; ++i)
{
scanf("%d%d" ,&e[i].u ,&e[i].v) ;
addedge(e[i].u ,e[i].v) ;
}
dep[1] = siz[0] = 0 ;
dfs_find(1 ,1) ;
idx = 1 ;
dfs_time(1 ,1) ;
scanf("%d" ,&m) ;
for(int i = 0 ;i < m ; ++i)
{
scanf("%d%d" ,&u ,&v) ;
LCA(u ,v) ;
}
for(int i = 1 ;i <= n ; ++i) // 第几条边
{
if(dep[e[i].u] < dep[e[i].v])
swap(e[i].u ,e[i].v) ;
P[ti[e[i].u]] = i ;
}
for(int i = 1 ;i <= n ; ++i) // 在剖分中的编号边的编号
{
sum[i] += sum[i-1] ;
ans[P[i]] = sum[i] ;
}
printf("%d" ,ans[1]) ;
for(int i = 2 ;i < n ; ++i)
printf(" %d" ,ans[i]) ;
puts("") ;
}
return 0 ;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces 191 C Fools and Roads (树链拆分)的更多相关文章

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

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

  2. hdu5044 Tree 树链拆分,点细分,刚,非递归版本

    hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...

  3. CF191C Fools and Roads - 树剖解法

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

  4. 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)

    题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...

  5. Codeforces 856D - Masha and Cactus(树链剖分优化 dp)

    题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...

  6. Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间

    一看就知道 可以LCA判断做 也可以树链剖分拿头暴力 然而快速读入和线段树维护区间会T70 于是只能LCA? 线段树的常数不小 于是需要另外一种办法来进行区间加减和查询区间和 就是使用树状数组 这个题 ...

  7. Codeforces 696E ...Wait for it...(树链剖分)

    题目链接  ...Wait for it... 考虑树链剖分. 对于树上的每个点开一个set,记录当前该节点上所有的girls. 每个节点初始的权值为set中的最小值. 询问的时候每次在路径上寻找最小 ...

  8. HYSBZ 2243 染色 (树链拆分)

    主题链接~~> 做题情绪:这题思路好想.调试代码调试了好久.第一次写线段树区间合并. 解题思路: 树链剖分 + 线段树区间合并 线段树的端点记录左右区间的颜色.颜色数目.合并的时候就用区间合并的 ...

  9. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

随机推荐

  1. Bigcommerce:安装的出错解决方案

    我们在本地安装时报错了,具体如下: 1. The database details you entered are incorrect: You have an error in your SQL s ...

  2. CImageList使用简要说明

    CImageList ImageList;//创建一个包含3个24位色32x32图片的ImageList,ILC_MASK的意思是同时创建一个mask,这样在下面指定了背景颜色以后ImageList就 ...

  3. VC,一条会被鼠标移动的直线

    对话框中的小红线可以被移动的 一.类名是 CBinarizationDlg 二.定义两个变量BOOL m_flag;int nPos;在构造函数初始化m_flag = false;nPos=256;三 ...

  4. mysql 查询优化案例

    mysql> explain SELECT c.`sn` clientSn,asm.`clientManagerSn`,pry.`productSn`,1 TYPE,pr.`capitalBal ...

  5. IDL 自己定义功能

    function add,x,y return, x+y end pro sum x=1 y=2 print,add(x,y) end 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  6. Swift - 滑块(UISlider)的用法

    1,滑块的创建 1 2 3 4 5 6 var slider=UISlider(frame:CGRectMake(0,0,300,50)) slider.center=self.view.center ...

  7. Mysql InnoDB 是IOT表 锁基于索引

    </pre>Mysql InnoDB 是IOT表 锁基于索引<pre>

  8. SAE php 研究(2)

    1.在SAE新建项目打印出phpinfo <?php  print phpinfo(); ?> 2. 可见:PHP Version 5.3.8 [使用的是php5.3.8编译的] 3. 可 ...

  9. [每日一题] 11gOCP 1z0-052 :2013-09-5 runInstaller oracle of no swap

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11186995 正确答案:A 我们先来看下面这张截图,这是我在安装Oracle 11.2.0 ...

  10. iOS视图控制器之间delegate传值教程

    之前在StackOverFlow上看到一篇讲传值(segue传值和delegate传值)的文章,感觉讲的非常清晰,就将delegate部分翻译了一下.有兴趣能够看看. 原文: http://stack ...