[Luogu 3128] USACO15DEC Max Flow
[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的更多相关文章
- [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]
		
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
 - 洛谷P3128 [USACO15DEC]最大流Max Flow
		
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
 - P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
		
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
 - luoguP3128 [USACO15DEC]最大流Max Flow  题解(树上差分)
		
链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...
 - 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]
		
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
 - USACO Max Flow
		
洛谷 P3128 [USACO15DEC]最大流Max Flow 洛谷传送门 JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow JDOJ传送门 Descrip ...
 - BZOJ 4390: [Usaco2015 dec]Max Flow
		
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
 - 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 ...
 
随机推荐
- IHttpModule理解-知识补充
			
文章:IHttpModule的那些事 可以自定义类实现IHttpModule接口,然后实现接口方法Init,Init方法可以得到HttpApplication 的实例化对象. 然后给对象的事件的注册各 ...
 - 利用CNN进行多分类的文档分类
			
# coding: utf-8 import tensorflow as tf class TCNNConfig(object): """CNN配置参数"&qu ...
 - nginx配置hls
			
备注:本来是想用浏览器播放hls,后来没有成功,最后使用flash播放rtmp的方案.所以下面的配置未使用. 修改/usr/local/nginx/conf/nginx.conf文件内容如下: wor ...
 - MySql点点滴滴(一)之可视化工具介绍
			
以下的文章主要介绍的是10个可以简化开发过程的MySQL工具,其中包括MySQL Workbench.phpMyAdmin.Aqua Data Studio,以及SQLyog与MYSQL Front等 ...
 - Enterprise Library 1.1 参考源码索引
			
http://www.projky.com/entlib/1.1/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...
 - C++获取private的变量-偷走private
			
private提供了对数据的封装,使得private成员只能被类自身的成员函数以及类的友元访问,其他的函数或者类想要访问private成员只能通过该类所提供的set和get的方法进行访问, 或者返回其 ...
 - 材料设计---Design
			
效果: main_activity.xml <?xml version="1.0" encoding="utf-8"?> <!--Coordi ...
 - 移动端-webkit-user-select:none导致input/textarea输入框无法输入
			
这个问题,也算是个大坑了. 最开始的开始,是因为我们在做大装盘活动的时候,发现在ios上面出现了这样的问题:点击“转”按钮,ios上面会有延迟并且会出现图片的阴影,这个肯定就不好看了撒,然后,找吧,改 ...
 - [转帖]DAS、SAN、NAS
			
http://blog.itpub.net/26736162/viewspace-2214368/ DAS(Direct-attached Storage) 直连存储 直连式存储与服务器主机之间的连接 ...
 - RHEL/Centos下VSFTPD服务器搭建
			
目的 Linux下安装配置vsfptd服务器,并通过客户端验证. 环境 Centos 6 局域网 内容 配置Vsftpd服务器:实现匿名用户.本地用户和虚拟用户登录的配置.匿名用户可以上载文件,上载后 ...