NC24019. [USACO 2015 Dec P]Max Flow
描述
FJ is pumping milk between KK pairs of stalls (1≤K≤100,0001≤K≤100,000). For the iith such pair, you are told two stalls sisi and titi, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the KK paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from sisi to titi, then it counts as being pumped through the endpoint stalls sisi and titi, as well as through every stall along the path between them.
输入描述
The first line of the input contains NN and KK.
The next N−1N−1 lines each contain two integers xx and yy (x≠yx≠y) describing a pipe between stalls xx and yy.
The next KK lines each contain two integers ss and tt describing the endpoint stalls of a path through which milk is being pumped.
输出描述
An integer specifying the maximum amount of milk pumped through any stall in the barn.
示例1
输入:
5 10 3 4 1 5 4 2 5 4 5 4 5 4 3 5 4 3 4 3 1 3 3 5 5 4 1 5 3 4
输出:
9
C++(g++ 7.5.0) 解法, 执行用时: 153ms, 内存消耗: 10772K, 提交时间: 2023-05-18 01:44:53
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 50007; vector<int> g[N]; int delta[N]; int f[27][N], dep[N]; void dfs(int u, int fa) { f[0][u] = fa; dep[u] = dep[fa] + 1; for (int i = 1;i <= 20;i++) f[i][u] = f[i - 1][f[i - 1][u]]; for (auto v : g[u]) { if (v == fa) continue; dfs(v, u); } } int LCA(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20;i >= 0;i--) { if (dep[f[i][u]] >= dep[v]) u = f[i][u]; if (u == v) return u; } for (int i = 20;i >= 0;i--) { if (f[i][u] != f[i][v]) { u = f[i][u]; v = f[i][v]; } } return f[0][u]; } void calc(int u, int fa) { for (auto v : g[u]) { if (v == fa) continue; calc(v, u); delta[u] += delta[v]; } } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, k; cin >> n >> k; for (int i = 1;i <= n - 1;i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); for (int i = 1;i <= k;i++) { int u, v; cin >> u >> v; int lca = LCA(u, v); delta[u]++; delta[v]++; delta[lca]--; delta[f[0][lca]]--; } calc(1, 0); cout << *max_element(delta + 1, delta + n + 1) << '\n'; return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 82ms, 内存消耗: 7600K, 提交时间: 2020-09-09 20:14:59
#include<bits/stdc++.h> #define LL long long using namespace std; const int N=5e4+5; struct Edge{int to,nxt;}e[N<<1]; int n,q,fst[N],tote,fa[N][21],dep[N],s[N],ans; void adde(int u,int v){e[++tote]=(Edge){v,fst[u]};fst[u]=tote;} int lca(int u,int v){ if(dep[u]<dep[v])swap(u,v); for(int i=20;~i;i--)if(dep[fa[u][i]]>=dep[v])u=fa[u][i]; if(u==v)return u; for(int i=20;~i;i--)if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i]; return fa[u][0]; } void dfs(int u,int f){ fa[u][0]=f;dep[u]=dep[f]+1; for(int i=1;i<=20;i++)fa[u][i]=fa[fa[u][i-1]][i-1]; for(int i=fst[u],v;i;i=e[i].nxt)if((v=e[i].to)!=f)dfs(v,u); } void getans(int u){ for(int i=fst[u],v;i;i=e[i].nxt)if((v=e[i].to)!=fa[u][0]) getans(v),s[u]+=s[v]; ans=max(ans,s[u]); } int main(){ scanf("%d%d",&n,&q); for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),adde(u,v),adde(v,u); dfs(1,0); for(int i=1,u,v,f;i<=q;i++){ scanf("%d%d",&u,&v);f=lca(u,v); s[u]++;s[v]++;s[f]--;s[fa[f][0]]--; } getans(1); printf("%d\n",ans); return 0; }