题意比较难理解,就是给你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. 记一次坑die(误)的题目--HDU2553--(DFS)

    ,N皇后问题   Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. 破解SharpPlus Sqlite Develope[转]

    1.运行里输入regedit,打开注册表 2.编辑->查找,输入sqlite 查找结果如下 3.直接删除SqliteDev节点就可以了,重新打开Sqlite Developer就可以用了,当然还 ...

  3. HDU 1681 Frobenius(完全背包+标记装满)

    一个完全背包,数组两百万,暴力可过 #include<iostream> #include<cstdio> #include<cstring> using name ...

  4. ListView random IndexOutOfBoundsException on Froyo

    http://stackoverflow.com/questions/8431342/listview-random-indexoutofboundsexception-on-froyo 今天遇到个 ...

  5. springMVC的拦截器工作流程

    首先,springmvc的拦截器配置在这就不多说了.主要讲一下拦截器的三个方法的执行顺序. preHandle方法一定是最先执行的方法,如果它返回为false下面的方法均不执行. postHandle ...

  6. jQuery获取Select选中的Text和Value,根据Value值动态添加属性等

    语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var ch ...

  7. CentOS查询 杀死进程

    ps aux | grep XXX 查询进程 ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参 ...

  8. Object.wait()与Object.notify()的用法

    http://www.cnblogs.com/xwdreamer/archive/2012/05/12/2496843.html 参考文献: object.wait()和object.notify() ...

  9. opcode修改

    Smali: if-eqz opcode 38 if-nez opcode 39 SO: CMP R0,#0 00 28 CMP R0,#1 01 28

  10. Mac系统下安装Tomcat,以及终端出现No such file or directory的错误提示解决方案

    Tomcat,作为一个免费的服务器口碑实在太好,本想安装一个研究研究,无奈电脑是mac系统,在网上搜了一些安装方法总是出错,直到遇到了这篇博客,http://www.cnblogs.com/qingy ...