[Luogu 3128] USACO15DEC Max Flow

<题目链接>


最近跟 LCA 干上了…

树剖好啊,我再也不想写倍增了。

以及似乎成功转成了空格选手 qwq。

对于每两个点 S and T,求一下 LCA 顺便树上差分,最后求差分数组的前缀和并找出最大值输出就行了。

(PS:最近考前训练不开 C++11,所以如果看见我写了奇怪的 define 请自动无视QAQ!)

#include <algorithm>
#include <cstdio> #define nullptr NULL const int MAXN = 50010; int n, m; namespace HLD
{
int num, qwq[MAXN];
struct Node
{
int depth, father, son, top, size, DFN;
}s[MAXN];
class Graph
{
private:
struct Edge
{
int to;
Edge *next;
Edge(int to, Edge* next): to(to), next(next) {}
~Edge(void)
{
if(next != nullptr)
delete next;
}
}*head[MAXN];
void AddEdges(int u, int v)
{
head[u] = new Edge(v, head[u]);
head[v] = new Edge(u, head[v]);
}
void DFS1(int u, int k)
{
s[u].depth = k;
s[u].size = 1;
int v;
for(Edge *i = head[u]; i != nullptr; i = i -> next)
if(!s[v = i -> to].size)
{
DFS1(v, k + 1);
s[u].size += s[v].size;
s[v].father = u;
if(s[v].size > s[s[u].son].size)
s[u].son = v;
}
}
void DFS2(int u, int top)
{
s[u].top = top;
s[u].DFN = ++num;
if(s[u].son)
DFS2(s[u].son, top);
int v;
for(Edge *i = head[u]; i != nullptr; i = i -> next)
if(!s[v = i -> to].top)
DFS2(v, v);
}
void Modify(int x, int y)
{
++qwq[s[x].DFN];
--qwq[s[y].DFN + 1];
}
public:
Graph(int n)
{
std :: fill(head + 1, head + n + 1, (Edge*)nullptr);
for(int i = 1, x, y; i < n; ++i)
{
scanf("%d %d", &x, &y);
AddEdges(x, y);
}
DFS1(1, 1);
DFS2(1, 1);
}
~Graph(void)
{
for(int i = 1; i <= n; ++i)
delete head[i];
}
void Run(int x, int y)
{
int a, b;
while((a = s[x].top) ^ (b = s[y].top))
if(s[a].depth > s[b].depth)
{
Modify(a, x);
x = s[a].father;
}
else
{
Modify(b, y);
y = s[b].father;
}
if(s[x].depth < s[y].depth)
Modify(x, y);
else
Modify(y, x);
}
int Answer(void)
{
int ans = qwq[1];
for(int i = 2; i <= n; ++i)
ans = std :: max(ans, qwq[i] += qwq[i-1]);
return ans;
}
}*G;
} int main(void)
{
scanf("%d %d", &n, &m);
HLD :: G = new HLD :: Graph(n);
for(int i = 1, x, y; i <= m; ++i)
{
scanf("%d %d", &x, &y);
HLD :: G -> Run(x, y);
}
printf("%d\n", HLD :: G -> Answer());
return 0;
}

谢谢阅读。

[Luogu 3128] USACO15DEC Max Flow的更多相关文章

  1. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

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

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

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

  3. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  4. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

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

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

  6. USACO Max Flow

    洛谷 P3128 [USACO15DEC]最大流Max Flow 洛谷传送门 JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow JDOJ传送门 Descrip ...

  7. BZOJ 4390: [Usaco2015 dec]Max Flow

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

  8. 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 ...

  9. Max Flow

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

随机推荐

  1. JavaBean 与 EJB 的区别

    JavaBean在一般情况下指的是实体类,在大部分情况下和POJO是同义词,基本构成就是一些字段和与之对应的 setter.getter方法,如果一个JavaBean需要在不同的JVM的进程中进行传递 ...

  2. ADO.NET使用using关闭数据库连接

    using (SqlConnection conn = new SqlConnection(source)) { // open the connoction conn.Open(); // Do s ...

  3. 使用node-webkit包装浏览器

    node-webkit简称nwjs:开源地址 https://github.com/nwjs/nw.js 参考博客 https://www.cnblogs.com/soaringEveryday/p/ ...

  4. Week3结对项目-数独游戏

    题目要求 1)在文章开头给出Github项目地址.(1') 2)在开始实现程序之前,在下述PSP表格记录下你估计将在程序的各个模块的开发上耗费的时间.(0.5') 3)看教科书和其它资料中关于Info ...

  5. 2018最新Web前端经典面试试题及答案

    javascript: JavaScript中如何检测一个变量是一个String类型?请写出函数实现 typeof(obj) === "string" typeof obj === ...

  6. MicrosoftFixit50688 [Windows7事件ID10,WMI错误的解决方法

     Windows7事件记录中有如下错误提示: "Event filter with query "SELECT * FROM __InstanceModificationEve ...

  7. .net webapi创建接口

    最近使用webapi做了一个用户数据库接口,方便其它网站接入验证用户,实现中解决出现的一些问题,做了一些记录, 1.返回显示为json数据 2.允许其他网站访问,刚开始没有设,在本地机测试时可以访问, ...

  8. BZOJ3444 最后的晚餐(并查集)

    容易发现只要图中有非链部分则无解.剩下就非常简单了. #include<iostream> #include<cstdio> #include<cmath> #in ...

  9. 左连接,右连接和等值连接(left join,right join和inner join)

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  10. 【BZOJ3293】分金币(贪心)

    [BZOJ3293]分金币(贪心) 题面 BZOJ 洛谷 题解 和上一题一样啊. #include<cstdio> #include<cmath> #include<al ...