通过霍尔定理转化判定方式的一步还是很妙的

The biggest gold mine in Berland consists of n caves, connected by n - 1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions.

The mine is being developed by the InMine Inc., k miners work for it. Each day the corporation sorts miners into caves so that each cave has at most one miner working there.

For each cave we know the height of its ceiling hi in meters, and for each miner we know his height sj, also in meters. If a miner's height doesn't exceed the height of the cave ceiling where he is, then he can stand there comfortably, otherwise, he has to stoop and that makes him unhappy.

Unfortunately, miners typically go on strike in Berland, so InMine makes all the possible effort to make miners happy about their work conditions. To ensure that no miner goes on strike, you need make sure that no miner has to stoop at any moment on his way from the entrance to the mine to his cave (in particular, he must be able to stand comfortably in the cave where he works).

To reach this goal, you can choose exactly one cave and increase the height of its ceiling by several meters. However enlarging a cave is an expensive and complex procedure. That's why InMine Inc. asks you either to determine the minimum number of meters you should raise the ceiling of some cave so that it is be possible to sort the miners into the caves and keep all miners happy with their working conditions or to determine that it is impossible to achieve by raising ceiling in exactly one cave.

Input

The first line contains integer n (1 ≤ n ≤ 5·105) — the number of caves in the mine.

Then follows a line consisting of n positive integers h1, h2, ..., hn (1 ≤ hi ≤ 109), where hi is the height of the ceiling in the i-th cave.

Next n - 1 lines contain the descriptions of transitions between the caves. Each line has the form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), where aiand bi are the numbers of the caves connected by a path.

The next line contains integer k (1 ≤ k ≤ n).

The last line contains k integers s1, s2, ..., sk (1 ≤ sj ≤ 109), where sj is the j-th miner's height.

Output

In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print  - 1. If it is initially possible and there's no need to raise any ceiling, print 0.


题目大意

有一个以 1 号点为根的 N 个节点的树形洞穴结构,每个节点有高度 $H[i]$,还有 $M$ 根长棒,每根的长度为 $B[i]$。要求把这些长棒放置到树上的一些节点上,每个节点最多只能放一根棒子,并且如果要把一根棒子放到一个节点上,必须满足这个点到根路径上所有点的高度都不低于棒子的长度。 可是,这样的方案似乎太难实施了。因此,允许对树上的一个节点开凿,使那个点的高度变大,要求开凿后方案能够顺利实施。如果无解则输出$-1$,如果不需要进行开凿输出 $0$,否则输出开凿点高度增加的最小值。

题目分析

首先考虑如何判定无修改时是否合法。

先对于每一个树上节点,预处理出$mn_i$表示从根到$i$路径上最小点权;那么就可以连边向$b_j(b_j \le mn_i)$,这就是一个网络流的模型,复杂度大致是$O(n\times 网络流)$级别的,如果想要进一步优化可以采用线段树优化建边的方式。但是很明显,这样处理没有用好这个图的性质,实际的表现远远不够通过此题。

由于只有节点向木棒的连边,这张图变成了判定二分图是否具有完全匹配的问题。因此可以用霍尔定理得到方案合法的充要条件:将$b_i$从小到大排序,$\forall deg_{b_i}\ge m-i+1$.

有了这个充要条件,就可以在均摊$\log n$复杂度内判断修改一个节点是否能够使方案合法。所以总的复杂度是$O(n\log n)$的。

似乎挺多人的做法是$O(n\log^2 n)$的,即枚举每个节点时二分修改的值。实际上只需要预处理出一个最大的非法位置$lst$,修改时考虑把$lst$这个位置安排进去。因为每修改一个节点,它所影响的其他所有节点增量必定是小于等于它自身的,那么当然是只要判定是否能够加入最大非法位置即可。

 #include<bits/stdc++.h>
typedef std::set<std::pair<int, int> > spar;
const int maxn = ;
const int maxm = ;
const int INF = 2e9; struct node
{
int mn,tag;
}f[maxn<<];
int n,ans,lst;
int h[maxn],c[maxn],m,b[maxn];
int edgeTot,head[maxn],nxt[maxm],edges[maxm];
int mn[maxn],sn[maxn];
std::vector<int> vec[maxn];
spar::iterator it;
spar s; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void pushup(int rt)
{
f[rt].mn = std::min(f[rt<<].mn, f[rt<<|].mn);
}
void pushdown(int rt)
{
if (f[rt].tag){
int tag = f[rt].tag;
f[rt<<].mn += tag, f[rt<<|].mn += tag;
f[rt<<].tag += tag, f[rt<<|].tag += tag;
f[rt].tag = ;
}
}
void build(int rt, int l, int r)
{
if (l==r) f[rt].mn = -(m-l+);
else{
int mid = (l+r)>>;
build(rt<<, l, mid);
build(rt<<|, mid+, r);
pushup(rt);
}
}
void modify(int rt, int l, int r, int L, int R, int c)
{
if (L <= l&&r <= R){
f[rt].mn += c, f[rt].tag += c;
return;
}
int mid = (l+r)>>;
pushdown(rt);
if (L <= mid) modify(rt<<, l, mid, L, R, c);
if (R > mid) modify(rt<<|, mid+, r, L, R, c);
pushup(rt);
}
int query(int rt, int l, int r, int c)
{
if (l==r) return f[rt].mn;
int mid = (l+r)>>;
pushdown(rt);
if (c <= mid) return query(rt<<, l, mid, c);
else return query(rt<<|, mid+, r, c);
}
void dfs(int x, int fa)
{
s.insert(std::make_pair(h[x], x));
it = s.begin();
mn[x] = (*it).first, vec[(*it).second].push_back(x);
if ((++it)!=s.end()) sn[x] = (*it).first;
else sn[x] = INF;
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=fa) dfs(edges[i], x);
s.erase(s.find(std::make_pair(h[x], x)));
}
void End(int x){printf("%d\n",x), exit();}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++) h[i] = read();
for (int i=; i<n; i++) addedge(read(), read());
m = read();
for (int i=; i<=m; i++) b[i] = read();
std::sort(b+, b+m+);
dfs(, ), build(, , m);
for (int i=; i<=n; i++)
{
c[i] = std::upper_bound(b+, b+m+, mn[i])-b-;
if (c[i]) modify(, , m, , c[i], );
}
if (f[].mn >= ) End();
ans = INF;
for (lst=m; lst>=; lst--) //lst=n
if (query(, , m, lst) < ) break;
for (int i=, mx; i<=n; i++)
if (h[i] <= b[lst]&&sn[i] >= b[lst]&&((int)vec[i].size() >= -f[].mn)){
mx = vec[i].size();
for (int j=; j<mx; j++)
{
int val = std::min(sn[vec[i][j]], b[lst]);
int id = std::upper_bound(b+, b+m+, val)-b-;
if (c[vec[i][j]]) modify(, , m, , c[vec[i][j]], -); //c[i]
if (id) modify(, , m, , id, );
}
if (f[].mn >= ) ans = std::min(ans, b[lst]-h[i]);
for (int j=; j<mx; j++)
{
int val = std::min(sn[vec[i][j]], b[lst]);
int id = std::upper_bound(b+, b+m+, val)-b-;
if (c[vec[i][j]]) modify(, , m, , c[vec[i][j]], );
if (id) modify(, , m, , id, -);
}
}
End(ans==INF?-:ans);
return ;
}

END

【贪心 二分图 线段树】cf533A. Berland Miners的更多相关文章

  1. [BZOJ 4025]二分图(线段树分治+带边权并查集)

    [BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...

  2. BZOJ 4025: 二分图 [线段树CDQ分治 并查集]

    4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...

  3. 【Luogu1973】仓配置(贪心,线段树)

    [Luogu1973]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...

  4. bzoj4025二分图(线段树分治 并查集)

    /* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...

  5. 【Luogu1937】仓配置(贪心,线段树)

    [Luogu1937]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...

  6. CF533A Berland Miners

    线段树维护贪心 /* */ #include<cstdio> #include<algorithm> #include<cstring> #include<i ...

  7. [BZOJ4025]二分图(线段树分治,并查集)

    4025: 二分图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2191  Solved: 800[Submit][Status][Discuss] ...

  8. [bzoj1577][Usaco2009 Feb]庙会捷运Fair Shuttle_贪心_线段树

    庙会捷运 Fair Shuttle bzoj-1577 Usaco-2009 Feb 题目大意:有一辆公交车从1走到n.有m群奶牛从$S_i$到$E_i$,第i群奶牛有$W_i$只.车有一个容量c.问 ...

  9. [BJOI2019] 删数 [dp转贪心结论+线段树]

    题面 传送门 思路 dp部分 以下称合法序列为原题面中可以删空的序列 这个是我在模拟考场上的思路 一开始我是觉得,这个首先可以写成一个dp的形式:$dp[i][j]$表示用$j$个数字填满了目标序列的 ...

随机推荐

  1. 关于VS2017提示I/O文件操作函数需要加上_s的解决办法

    最近不论是在写网络编程还是在写小项目时候,都会提示让我用加个_s的函数........ 其实加上_s这个函数是为了确保函数的安全性,确保不会有内存不够或者溢出的情况.但是每次都需要重新看一下_s函数的 ...

  2. datagrid 里面的formatter方法

    A.{field:'station_staus',title:'工位状态',width:250,align:'center',formatter: function(value,row,index){ ...

  3. 基于php缓存的详解

    nginx缓存 nginx有两种缓存机制:fastcgi_cache和proxy_cache 下面我们来说说这两种缓存机制的区别吧 proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包 ...

  4. android 开发-Process and Thread

    目录 1 android中进程与线程 - Processes and Threads 1.1 进程 - Processes 1.1.1 进程的生命期 1.2 线程 - Threads 1.2.1 工作 ...

  5. JDK工具

    在之前的教程中,我曾介绍过 这些工具.现在,我向大家介绍其中最重要的5个工具. 1.javap javap是一个Java类文件反汇编程序,可以查看Java编译器生成的字节码,是分析代码的一个好工具.让 ...

  6. IIS错误HTTP 错误 500.21 - Internal Server Error

    原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:以管理员身份运行cmd->输入“%windir%\Microsoft.NET\Framewo ...

  7. 一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa  跨终端技术:HTML 5.CSS 3.JavaScri ...

  8. vue-实现一个购物车结算页面

    这是路由之间的跳转,传递值最好采用传参,而不是用$emit和$on,不起作用 如果实在一个页面中的兄弟组件,可以使用$emit和$on 中间件,eventBus.js 放在components目录下面 ...

  9. PHP函数:method_exists和function_exists

    method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...

  10. genlist -s 192.168.21.\*

    显示网段192.168.21中可用的主机.