题意比较难理解,就是给你n个点的树,然后给你m个修改操作,每一次修改包括一个点对(x, y),意味着将x到y所有的点权值加一,最后问你整个树上的点权最大是多少。

  

  比较裸的树链剖分了,感谢Haild的讲解。

  首先第一遍dfs预处理出size,son(重儿子)。

  第二遍dfs重编号。

  然后线段树就可以了。

  感觉就是把一棵树弄成一条一条的链,新奇的hash方法。

 #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//******************************* template <typename T> void MAX(T &a, T &b) { if (a < b) a = b; }
template <typename T> void MIN(T &a, T &b) { if (a > b) a = b; } const int maxn = ; struct Ed {
int u, v, nx; Ed() {}
Ed(int _u, int _v, int _nx) :
u(_u), v(_v), nx(_nx) {}
} E[maxn << ];
int G[maxn], edtot;
void addedge(int u, int v) {
E[++edtot] = Ed(u, v, G[u]);
G[u] = edtot;
E[++edtot] = Ed(v, u, G[v]);
G[v] = edtot;
} struct Seg_Tree {
int lazy[maxn << ], Max[maxn << ];
void Push_down(int o) {
if (!lazy[o]) return;
lazy[o << ] += lazy[o], lazy[o << | ] += lazy[o];
Max[o << ] += lazy[o], Max[o << | ] += lazy[o];
lazy[o] = ;
}
void Push_up(int o) { Max[o] = max(Max[o << ], Max[o << | ]); }
void update(int o, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
lazy[o] += v;
Max[o] += v;
return;
}
Push_down(o);
int mid = l + r >> ;
if (ql <= mid) update(o << , l, mid, ql, qr, v);
if (qr > mid) update(o << | , mid + , r, ql, qr, v);
Push_up(o);
}
} T; int size[maxn], pre[maxn], son[maxn], dep[maxn];
int dfs_size(int x, int fa) {
size[x] = ; int haha = -inf; pre[x] = fa; dep[x] = dep[fa] + ;
for (int i = G[x]; i; i = E[i].nx) if (E[i].v != fa) {
size[x] += dfs_size(E[i].v, x);
if (size[E[i].v] > haha) haha = size[E[i].v], son[x] = E[i].v;
}
return size[x];
}
int ndtot, pos[maxn], top[maxn];
void repos(int x, int tp) {
pos[x] = ++ndtot;
top[x] = tp;
if (son[x]) repos(son[x], tp);
for (int i = G[x]; i; i = E[i].nx) if (E[i].v != pre[x] && E[i].v != son[x]) repos(E[i].v, E[i].v);
} int n;
void update(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
T.update(, , n, pos[top[x]], pos[x], ); x = pre[top[x]];
}
T.update(, , n, min(pos[x], pos[y]), max(pos[x], pos[y]), );
} int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int m; scanf("%d%d", &n, &m);
REP(i, , n) {
int x, y; scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs_size(, );
repos(, );
while (m--) {
int x, y; scanf("%d%d", &x, &y);
update(x, y);
}
printf("%d\n", T.Max[]);
return ;
}

USACO 2015 December Contest, Platinum Problem Max Flow【树链剖分】的更多相关文章

  1. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  2. USACO 2015 December Contest, Gold Problem 2. Fruit Feast

    Problem 2. Fruit Feast 很简单的智商题(因为碰巧脑出来了所以简单一,一 原题: Bessie has broken into Farmer John's house again! ...

  3. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  4. 【NOIP 2015 DAY2 T3】 运输计划 (树链剖分-LCA)

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  5. BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分

    4679: Hdu5331 Simple Problem 题意: 考场上,看到这道题就让我想起BZOJ4712洪水.然后思路就被带着飞起了,完全没去考虑一条链的情况,于是GG. 解法:先考虑一条链的做 ...

  6. [xsy1129] flow [树链剖分和线段树一起优化网络流][我也不知道这是什么鬼标签]

    题面 内部OJ 思路 考虑一个决策方案${x}$,$x_i$表示第$i$个点选不选,$f^k_i$表示点$i$的第$k$个父亲 那么可以得到总花费的表达式$ans=\sum V_i x_i - \su ...

  7. FZU Problem 2082 过路费 树链剖分

    Problem 2082 过路费    Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...

  8. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...

  9. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

随机推荐

  1. acm课程练习2--1005

    题目描述 Mr. West bought a new car! So he is travelling around the city.One day he comes to a vertical c ...

  2. Linux学习 -- 启动管理

    1 CentOS 6.x启动管理 系统运行级别 运行级别命令 #runlevel   查看级别 #init 运行级别  改变级别 系统默认运行级别 配置文件 /etc/inittab id:3:ini ...

  3. 转:LoadRunner常用函数列表

    Web相关函数 函    数 功 能 描 述 web_custom_request 用户可以通过该函数自行创建一个HTTP请求的函数 web_image 模拟用户单击图片操作的函数 web_link ...

  4. kill -QUIT <pid>

    On Solaris and Linux a thread dump is also printed if the J2SE process receives a QUIT signal. So ki ...

  5. 转 区别 getChildFragmentManager getSupportFragmentManager

    The definition of getChildFragmentManager() is: Return a private FragmentManager for placing and man ...

  6. HTTP 中 POST和GET的区别

    1.传送数据的方式不一样 get是将数据队列添加到URL中提交,用户可以看到(对URL反编码就行) post是将数据队列放到HTTP的报文的报头中提交,用户看不到所提交的数据: 2.服务器端获取变量的 ...

  7. 一个有意思的 hta 程序 (html application)

    哈哈,刚才同事给我讲了一个hta 程序,他自己说最近在学html5 开发坦克大战,不错,这种好奇心, 好学的精神值得我这个程序员学习,感觉他的视野面比我这个程序员还广,有点小惭愧. 什么是hta 呢? ...

  8. gen_grant_dml.sql

    set echo off feedback off verify off pagesize 0 linesize 120 define v_grantee                = & ...

  9. cc2530串口通信流程

    //串口发送接收流程 main: //主函数 ->osal_init_system(); //操作系统初始化 ->osalInitTasks(); //任务初始化 -->ZDApp_ ...

  10. BNUOJ 6038 - Reaux! Sham! Beaux!(模拟)

    这是一个水模拟,但是因为图片看不清,手打比较烧脑,我们错了好多次才过 #include<stdio.h> #include<iostream> #include<stri ...