题目大意:
  给你一棵n个点的树,有m次操作,每次将给定的路径上所有点的点权+1。
  问最后最大的点权是多少。

思路:
  

 #include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
class SegmentTree {
#define _left <<1
#define _right <<1|1
private:
int max[N<<],tag[N<<];
void push_down(const int &p) {
max[p _left]+=tag[p];
max[p _right]+=tag[p];
tag[p _left]+=tag[p];
tag[p _right]+=tag[p];
tag[p]=;
}
void push_up(const int &p) {
max[p]=std::max(max[p _left],max[p _right]);
}
public:
void modify(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(b==l&&e==r) {
max[p]++;
tag[p]++;
return;
}
push_down(p);
const int mid=(b+e)>>;
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r));
if(r>mid) modify(p _right,mid+,e,std::max(mid+,l),r);
push_up(p);
}
int query() const {
return max[];
}
#undef _left
#undef _right
};
SegmentTree t;
int n,par[N],dep[N],top[N],low[N],size[N],son[N],id[N];
void dfs1(const int &x,const int &par) {
::par[x]=par;
dep[x]=dep[par]+;
size[x]=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs1(y,x);
size[x]+=size[y];
if(size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(const int &x) {
id[x]=++id[];
if(x==son[par[x]]) {
top[x]=top[par[x]];
} else {
top[x]=x;
}
if(son[x]) dfs2(son[x]);
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par[x]||y==son[x]) continue;
dfs2(y);
}
}
inline void modify(int u,int v) {
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) std::swap(u,v);
t.modify(,,n,id[top[u]],id[u]);
u=par[top[u]];
}
if(dep[u]<dep[v]) std::swap(u,v);
t.modify(,,n,id[v],id[u]);
}
int main() {
n=getint();
const int m=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
dfs1(,);
dfs2();
for(register int i=;i<m;i++) {
const int s=getint(),t=getint();
modify(s,t);
}
printf("%d\n",t.query());
return ;
}

树链剖分模板题。

[USACO2015DEC]Max Flow的更多相关文章

  1. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

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

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

  3. HackerRank "Training the army" - Max Flow

    First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...

  4. Max Flow

    Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...

  5. min cost max flow算法示例

    问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...

  6. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  7. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  8. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  9. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

随机推荐

  1. com.mongodb.MongoException$CursorNotFound: cursor not found on server异常处理

    java链接MongoDB处理大量数据时经常碰到cursor not found 的异常,其实是超时所致 Exception in thread "main" com.mongod ...

  2. LOJ 6057 - [HNOI2016]序列 加强版再加强版

    Description 给定一个长度为 \(n\le 3*10^6\) 的序列 \(q\le 10^7\) 次询问每次求区间 \([l,r]\) 的所有子区间的最小值的和 询问随机 Solution ...

  3. Spring学习之路——单例模式和多例模式

    在Spring中,bean可以被定义为两种模式:prototype(多例)和singleton(单例) singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实 ...

  4. [转]使用 LDAP 组或角色限制访问,包含部分单点登录SSO说明

    参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEP7J_10.2.2/com.ibm.swg.ba.cognos.crn ...

  5. Vue 还是 React 还是 Angular ?

    有空的时候还是把3个都熟悉一下.除了Angular学习起来笔记花时间外.React跟Vue没啥难度,学习用的时间不多. 如果你在Google工作:Angular如果你喜欢TypeScript:Angu ...

  6. 手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客

    http://www.360doc.com/content/15/0118/17/1073512_441822850.shtml# 钿畑的文章索引 1. 什么是推送通知 2. 什么是APNS? 3. ...

  7. POJ3466(01背包变形)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  8. malloc和new的区别 end

    3. c++中new的几种用法 c++中,new的用法很灵活,这里进行了简单的总结: 1. new() 分配这种类型的一个大小的内存空间,并以括号中的值来初始化这个变量; 2. new[] 分配这种类 ...

  9. AOP相关

    静态代理.动态代理与AOP: 简单易懂:http://blog.csdn.net/hejingyuan6/article/details/36203505 补充:http://layznet.itey ...

  10. 移动开发之css3实现背景渐变效果

    前段时间由于手机项目需要实现一个背景渐变功能,  开始是想切个小背景图平铺下,  后来想到css3可以实现,如是用下面的代码就实现了. .sec_case_list li  span{ backgro ...