将树映射在线段上进行操作 然后每个 重链变成一个连续的区间
#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
#pragma comment(linker,"/STACk:10240000,10240000")
using namespace std;
const int maxn=;
vector<int>F[maxn];
int son[maxn],num[maxn],fa[maxn],top[maxn],p[maxn],fp[maxn],pos,depth[maxn],ans[maxn];
vector<int>P[maxn];
int loc,v;
struct SegmentItree{
int num[maxn*],id[maxn*];
void build(int o,int L, int R){
num[o]=;
id[o]=;
if(L>=R){
num[o]=; id[o]=L;
return ;
}
int mid = (L+R)/;
build(o*,L,mid );
build(o*+,mid+,R);
}
void maintain(int o){
num[o]=;
id[o]=;
if(num[o*]==&&num[o*+]==)return;
if( num[o*] >= num[o*+] ){
num[o]=num[o*];
id[o]=id[o*];
} else{
num[o] =num[o*+];
id[o]=id[o*+];
}
}
void add(int o,int L, int R){
if(L>=R){
num[o]+=v;
return ;
}
int mid = (L+R)>>;
if(loc<=mid){
add(o*,L,mid);
}else{
add(o*+, mid+,R);
}
maintain(o);
}
}T;
void inti(int n){
for(int i=; i<=n+; ++i)
F[i].clear(),P[i].clear();
pos=;
}
void dfs(int cur, int per,int dep){
depth[cur]=dep;
son[cur]=-;
fa[cur]=per;
num[cur]=;
int Len= F[cur].size();
for(int i=; i<Len; ++i){
int to = F[cur][i];
if(to==per) continue;
dfs(to,cur,dep+);
num[cur]+=num[to];
if( son[ cur ] == - || num[ son[cur] ]< num[to] ) son[cur]=to;
}
}
void finde(int cur , int per, int xx){
top[cur]=xx;
pos++;
p[cur]=pos;
fp[pos]=cur;
if(son[cur]!=-)
finde(son[cur],cur,xx);
int len = F[cur].size();
for(int i=; i<len ;++i ){
int to= F[cur][i];
if(to==per||to==son[cur])continue;
finde(to,cur,to);
}
}
void solve(int x, int y,int d){
int f1=top[x],f2=top[y];
while(f1!=f2){
if(depth[f1]<depth[f2]){
int temp = x; x=y; y= temp;
temp=f1; f1=f2; f2=temp;
}
P[ p[f1] ].push_back(d);
P[ p[x]+ ].push_back(-d);
x=fa[f1];
f1=top[x];
}
if(depth[x]>depth[y]){
int temp = x; x = y ; y= temp;
}
P[p[x]].push_back(d);
P[p[y]+].push_back(-d);
}
int main()
{
int n,m;
for(;;){
scanf("%d%d",&n,&m);
if(n==&&m==)break;
inti(n);
for(int i=; i<n ;++i ){
int a,b;
scanf("%d%d",&a,&b);
F[a].push_back(b);
F[b].push_back(a);
}
dfs(,,);
finde(,,);
int N=;
for(int i=; i<m; ++i){
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
solve(a,b,d);
N=max(d,N);
}
memset(ans,,sizeof(ans));
if(N!=){
T.build(,,N);
for(int i=; i<=n; ++i){
int L = P[i].size();
for(int j=; j<L; ++j){
int to= P[i][j];
if(to>){
v=;
loc=to;
}else{
v=-;
loc=-to;
}
T.add(,,N);
}
if(T.num[]!=)
ans[fp[i]]=T.id[];
else ans[fp[i]]=;
}
}
for(int i=; i<=n; ++i)
printf("%d\n",ans[i]);
}
return ;
}

hdu5029 树链剖分 + 线段树的更多相关文章

  1. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  2. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  3. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  4. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  5. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  6. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  7. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  8. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  9. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  10. bzoj2243[SDOI2011]染色 树链剖分+线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 9012  Solved: 3375[Submit][Status ...

随机推荐

  1. 内部排序->归并排序->2-路归并排序

    文字描述 假设初始序列有n个记录,则可看成是n个有序的字序列,每个字序列的长度为1,然后两两归并,得到[n/2]个长度为2或1的有序子序列:再两两归并,…, 如此重复,直到得到一个长度为n的有序序列为 ...

  2. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  3. scala-高阶函数

    //1类似于lambda表达式的函数直接量====================== var get = (name: String) => { println(123 + name) } g ...

  4. kubernetes的应用数据持久化

    1.无状态应用与有状态应用 应用的有状态和无状态是根据应用是否有持久化保存数据的需求而言的,即持久化保存数据的应用为有状态的应用,反之则为无状态的应用.常见的系统往往是有状态的应用,比如对于微博和微信 ...

  5. vue router拦截器的简单使用

    之前,为了实现router跳转的每个页面的url上都带上addressCode,然后用了一下router拦截器,很好用,当然也可以专门封装一个方法来实现(跳转的页面上带有addressCode),不过 ...

  6. python 去除微软的BOM

    傻逼微软会给文件前面加上efbbbf, 导致开发人员浪费很多时间在排错上,下面通过python代码来实现去除微软BOM的功能 用法很简单,指定可能含有BOM开头的文件,并且将微软的\r\n 换成lin ...

  7. SpringBoot-区分不同环境配置文件

    spring.profiles.active=pre application-dev.properties:开发环境 application-test.properties:测试环境 applicat ...

  8. jquery实现简单的弹出框

    弹出框本身是一个div,默认是隐藏不展示的,在需要弹框的时候使其显示,并浮在当前页面之上 弹框样式: .tanchuang { width: 100%; height: 100%; display: ...

  9. Error, some other host already uses address 192.168.0.202错误解决方法

    Error, some other host already uses address 192.168.0.202错误解决方法 今天配置虚拟机网卡的时候遇到错误:Error, some other h ...

  10. 细细探究MySQL Group Replicaiton — 配置维护故障处理全集(转)

    如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持!