P3128 [USACO15DEC] Max Flow P

2023-11-03 20:52
文章标签 flow max p3128 usaco15dec

本文主要是介绍P3128 [USACO15DEC] Max Flow P,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Portal.

树上差分。

这里要用的是边差分。

对于一条路径 s → t s\rightarrow t st,我们把 s s , s t s_s,s_t ss,st 加一,代表到 s , t s,t s,t 的路径上的隔间压力都加 1 1 1

注意到 LCA 被重复累加,所以要减 1 1 1。又因为 LCA 的 s lca s_{\text{lca}} slca 会对本来不该累加次数的 LCA 的父亲产生影响,所以 LCA 的父亲的 s s s 值应该减 1 1 1

注意要先 DFS 统计完信息之后再更新 for(int j=1;j<=25;j++) for(int i=1;i<=N;i++) f[i][j]=f[f[i][j-1]][j-1];

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long longconst int maxn=1e5+5;
struct edge{int to,nxt;}e[maxn*2];
int head[maxn],dep[maxn],f[maxn][30],cnt,ans,s[maxn];void add(int x,int y){e[++cnt]={y,head[x]},head[x]=cnt;}void dfs1(int x,int fa)
{dep[x]=dep[fa]+1,f[x][0]=fa;for(int i=head[x];i;i=e[i].nxt){if(e[i].to==fa) continue;dfs1(e[i].to,x);}
}int lca(int x,int y)
{if(dep[x]<dep[y]) swap(x,y);for(int i=25;i>=0;i--) if(dep[f[x][i]]>=dep[y]) x=f[x][i];if(x==y) return x;for(int i=25;i>=0;i--)if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];return f[x][0];
}void dfs2(int x,int fa)
{for(int i=head[x];i;i=e[i].nxt){if(e[i].to==fa) continue;dfs2(e[i].to,x);s[x]+=s[e[i].to];}ans=max(ans,s[x]);
}signed main()
{int N,K;cin>>N>>K;for(int i=1,x,y;i<N;i++) cin>>x>>y,add(x,y),add(y,x);dfs1(1,0);for(int j=1;j<=25;j++) for(int i=1;i<=N;i++) f[i][j]=f[f[i][j-1]][j-1];while(K--){int si,ti;cin>>si>>ti;int tmp=lca(si,ti);s[si]++,s[ti]++,s[tmp]--,s[f[tmp][0]]--;// cout<<s[si]<<endl;}dfs2(1,0);cout<<ans;return 0;
}

这篇关于P3128 [USACO15DEC] Max Flow P的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/340810

相关文章

GNSS CTS GNSS Start and Location Flow of Android15

目录 1. 本文概述2.CTS 测试3.Gnss Flow3.1 Gnss Start Flow3.2 Gnss Location Output Flow 1. 本文概述 本来是为了做Android 14 Gnss CTS 的相关环境的搭建和测试,然后在测试中遇到了一些问题,去寻找CTS源码(/cts/tests/tests/location/src/android/locat

file-max与ulimit的关系与差别

http://zhangxugg-163-com.iteye.com/blog/1108402 http://ilikedo.iteye.com/blog/1554822

Understanding the GitHub Flow

这里看下Github的入门介绍    --链接 GitHub Flow is a lightweight, branch-based workflow that supports teams and projects where deployments are made regularly. This guide explains how and why GitHub Flow works

POJ 1050 To the Max(枚举+动规)

题目: http://poj.org/problem?id=1050 题解: 此题转化成一维后就相当于求最大连续子序列了,可以枚举所有的行组合,把枚举到的起始行到终止行的值按列相加存入一个一维数组。 代码: #include<cstdio>#include<cstring>int a[101][101];int value[101];int dp[101];int max(

Versioned Staged Flow-Sensitive Pointer Analysis

VSFS 1.Introduction2.Approach2.1.相关概念2.2.VSFS 3.Evaluation参考文献 1.Introduction 上一篇blog我介绍了目前flow-sensitive pointer analysis常用的SFS算法。相比IFDS-based方法,SFS显著通过稀疏分析提升了效率,但是其内部依旧有许多冗余计算,留下了很大优化空间。 以

报错:Reached the max session limit(DM8 达梦数据库)

报错:Reached the max session limit - - DM8 达梦数据库 1 环境介绍2 数据库启动SYSTEM IS READY后面日志3 数据库刚启动日志4 达梦数据库学习使用列表 1 环境介绍 某项目无法连接数据库,报错:超过最大会话数限制 , 检查 dmdba ulimit -a openfiles 已改检查 dm.ini 其中 MAX_SESSION

【硬刚ES】ES基础(二十) 单字符串多字段查询:Dis Max Query

本文是对《【硬刚大数据之学习路线篇】从零到大数据专家的学习指南(全面升级版)》的ES部分补充。

[LeetCode] 695. Max Area of Island

题:https://leetcode.com/problems/max-area-of-island/description/ 题目 Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizont

[LeetCode] 485. Max Consecutive Ones

题: 题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consec

Salt Function Flow:深度解析复杂网关编排的优势与实践

系列文章索引: Salt Function Flow 系列文章 在业务流程编排中,处理条件逻辑、并行任务、以及复杂的流程分支是常见的挑战。对于需要高度灵活性和扩展性的项目,Salt Function Flow 提供了强大的网关编排能力,使开发者能够轻松定义和管理复杂的业务流程。本文将深入探讨Salt Function Flow中的复杂网关编排功能,展示其如何通过排他网关、并行执行等功能应对复杂的