[USACO2015DEC]Max Flow
题目大意:
给你一棵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的更多相关文章
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 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 ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
随机推荐
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- angular的一些问题
引入第三方类库 1.安装依赖 npm install jquey --save 2.引入项目 在angular-cli.json "scripts": [ "../nod ...
- codeforces 1065D
题目链接:https://codeforces.com/problemset/problem/1065/D 题意:给你一个又1~n^2组成的n行n列的矩阵,你可以走日字型,直线,斜线,现在要求你从1走 ...
- rest与restful
知乎上面摘抄的,感觉不错,分享下: https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...
- ireport写sql语句的按钮在哪
- C#与数据库的连接的三种方式
学习了.net的知识从C#一直到MVC,我一直觉得基础很重要,最近有复习一下数据库连接的三种方式 1 返回结果集的一张表 public static DataTable ExecuteDataTabl ...
- hihoCoder 1527 快速乘法
#include<bits/stdc++.h> using namespace std; ; char a[N]; int main() { scanf(); ); ,r = n; ') ...
- Dokuwiki 二次开发记录
Dokuwiki 二次开发记录 [转]http://www.syyong.com/other/Dokuwiki-Secondary-Development-Record.html DokuWiki 是 ...
- 网络流专题练习Day2
04/17 目前做了:题 由于目前六道都是1A感觉非常爽... BZOJ1412: [ZJOI2009]狼和羊的故事 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向 ...
- [POJ1423]Stirling公式的应用
Stirling公式: n!约等于sqrt(2*pi*n)*(n/e)^n 另外,e约等于2.71828182845409523... 试了一下发现math库里面并不能像pi一样直接调e但是发现挺好记 ...