通过霍尔定理转化判定方式的一步还是很妙的
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 imes 网络流)$级别的,如果想要进一步优化可以采用线段树优化建边的方式。但是很明显,这样处理没有用好这个图的性质,实际的表现远远不够通过此题。
由于只有节点向木棒的连边,这张图变成了判定二分图是否具有完全匹配的问题。因此可以用霍尔定理得到方案合法的充要条件:将$b_i$从小到大排序,$forall deg_{b_i}ge m-i+1$.
有了这个充要条件,就可以在均摊$log n$复杂度内判断修改一个节点是否能够使方案合法。所以总的复杂度是$O(nlog n)$的。
似乎挺多人的做法是$O(nlog^2 n)$的,即枚举每个节点时二分修改的值。实际上只需要预处理出一个最大的非法位置$lst$,修改时考虑把$lst$这个位置安排进去。因为每修改一个节点,它所影响的其他所有节点增量必定是小于等于它自身的,那么当然是只要判定是否能够加入最大非法位置即可。
1 #include<bits/stdc++.h> 2 typedef std::set<std::pair<int, int> > spar; 3 const int maxn = 500035; 4 const int maxm = 1000035; 5 const int INF = 2e9; 6 7 struct node 8 { 9 int mn,tag; 10 }f[maxn<<2]; 11 int n,ans,lst; 12 int h[maxn],c[maxn],m,b[maxn]; 13 int edgeTot,head[maxn],nxt[maxm],edges[maxm]; 14 int mn[maxn],sn[maxn]; 15 std::vector<int> vec[maxn]; 16 spar::iterator it; 17 spar s; 18 19 int read() 20 { 21 char ch = getchar(); 22 int num = 0, fl = 1; 23 for (; !isdigit(ch); ch=getchar()) 24 if (ch=='-') fl = -1; 25 for (; isdigit(ch); ch=getchar()) 26 num = (num<<1)+(num<<3)+ch-48; 27 return num*fl; 28 } 29 void addedge(int u, int v) 30 { 31 edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot; 32 edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot; 33 } 34 void pushup(int rt) 35 { 36 f[rt].mn = std::min(f[rt<<1].mn, f[rt<<1|1].mn); 37 } 38 void pushdown(int rt) 39 { 40 if (f[rt].tag){ 41 int tag = f[rt].tag; 42 f[rt<<1].mn += tag, f[rt<<1|1].mn += tag; 43 f[rt<<1].tag += tag, f[rt<<1|1].tag += tag; 44 f[rt].tag = 0; 45 } 46 } 47 void build(int rt, int l, int r) 48 { 49 if (l==r) f[rt].mn = -(m-l+1); 50 else{ 51 int mid = (l+r)>>1; 52 build(rt<<1, l, mid); 53 build(rt<<1|1, mid+1, r); 54 pushup(rt); 55 } 56 } 57 void modify(int rt, int l, int r, int L, int R, int c) 58 { 59 if (L <= l&&r <= R){ 60 f[rt].mn += c, f[rt].tag += c; 61 return; 62 } 63 int mid = (l+r)>>1; 64 pushdown(rt); 65 if (L <= mid) modify(rt<<1, l, mid, L, R, c); 66 if (R > mid) modify(rt<<1|1, mid+1, r, L, R, c); 67 pushup(rt); 68 } 69 int query(int rt, int l, int r, int c) 70 { 71 if (l==r) return f[rt].mn; 72 int mid = (l+r)>>1; 73 pushdown(rt); 74 if (c <= mid) return query(rt<<1, l, mid, c); 75 else return query(rt<<1|1, mid+1, r, c); 76 } 77 void dfs(int x, int fa) 78 { 79 s.insert(std::make_pair(h[x], x)); 80 it = s.begin(); 81 mn[x] = (*it).first, vec[(*it).second].push_back(x); 82 if ((++it)!=s.end()) sn[x] = (*it).first; 83 else sn[x] = INF; 84 for (int i=head[x]; i!=-1; i=nxt[i]) 85 if (edges[i]!=fa) dfs(edges[i], x); 86 s.erase(s.find(std::make_pair(h[x], x))); 87 } 88 void End(int x){printf("%d ",x), exit(0);} 89 int main() 90 { 91 memset(head, -1, sizeof head); 92 n = read(); 93 for (int i=1; i<=n; i++) h[i] = read(); 94 for (int i=1; i<n; i++) addedge(read(), read()); 95 m = read(); 96 for (int i=1; i<=m; i++) b[i] = read(); 97 std::sort(b+1, b+m+1); 98 dfs(1, 0), build(1, 1, m); 99 for (int i=1; i<=n; i++) 100 { 101 c[i] = std::upper_bound(b+1, b+m+1, mn[i])-b-1; 102 if (c[i]) modify(1, 1, m, 1, c[i], 1); 103 } 104 if (f[1].mn >= 0) End(0); 105 ans = INF; 106 for (lst=m; lst>=1; lst--) //lst=n 107 if (query(1, 1, m, lst) < 0) break; 108 for (int i=1, mx; i<=n; i++) 109 if (h[i] <= b[lst]&&sn[i] >= b[lst]&&((int)vec[i].size() >= -f[1].mn)){ 110 mx = vec[i].size(); 111 for (int j=0; j<mx; j++) 112 { 113 int val = std::min(sn[vec[i][j]], b[lst]); 114 int id = std::upper_bound(b+1, b+m+1, val)-b-1; 115 if (c[vec[i][j]]) modify(1, 1, m, 1, c[vec[i][j]], -1); //c[i] 116 if (id) modify(1, 1, m, 1, id, 1); 117 } 118 if (f[1].mn >= 0) ans = std::min(ans, b[lst]-h[i]); 119 for (int j=0; j<mx; j++) 120 { 121 int val = std::min(sn[vec[i][j]], b[lst]); 122 int id = std::upper_bound(b+1, b+m+1, val)-b-1; 123 if (c[vec[i][j]]) modify(1, 1, m, 1, c[vec[i][j]], 1); 124 if (id) modify(1, 1, m, 1, id, -1); 125 } 126 } 127 End(ans==INF?-1:ans); 128 return 0; 129 }
END