- 题目链接:http://poj.org/problem?id=2987
- 题意:一个公司要裁员,裁员可以给公司省下其工资及奖金。开除一个员工时,要把他的下属也开除。一个员工可以属于多个部门,在一个部门中,一个员工的下属可能在另一个部门是他的上司。
- 补充知识点
- 闭合图:对于一个有向图G,存在点集合V,任取点u属于V,u的出边的另一个点也属于V,则为闭合图。
- 理解:任取一起点,终点必定是无出度的点。
- 最大权闭合子图:当每个点有一个权值w(有正有负),点权和最大的闭合图为最大权闭合子图。
- 如下图,最大权闭合子图为点集{3,4,5},最大权为7+0-3=4。
- 思路:做最大权闭合图的套路:把权值为正的点与超级源点S相连,容量为该权值,把权值为负的点与超级汇点T相连,容量为该权值的绝对值,然后点与点之间的连边是,如果a被删除时b也要被删除,那么a连一条边指向b,容量为INF。如果要求删除的点数的话,从S开始DFS,残余网络中的点即删除的点。因为删除的点的集合是从S出发可以达到的并且不能达到T的点集(即割分成的两个集(S集和T集)的S集)。
复制代码 - 注意:设一个员工a有一个下属b,则建图时为a指向b,这样才能建出符合题意的闭合图。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 50000 + 10;
const int maxv = 5000 + 10;
const int maxe = 60010 * 2;
const int mod = 1e9 + 7;
int b[maxv];
int s, t;
int head[maxe], cnt=0, nowedge[maxe];
int dis[maxv];
int vis[maxv];
int n, f, d;
struct Edge
{
int to, next, cap;
}es[maxe];
void init()
{
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
cnt=0;
}
void add(int u, int v, int vol)
{
es[cnt].to = v;
es[cnt].cap = vol;
es[cnt].next = head[u];
head[u] = cnt++;
es[cnt].to = u;
es[cnt].cap = 0;
es[cnt].next = head[v];
head[v] = cnt++;
}
bool BFS()
{
queue<int> que;
memset(dis, -1, sizeof(dis));
dis[s] = 0;
que.push(s);
int cur, v;
while(!que.empty())
{
cur = que.front(); que.pop();
for(int i=head[cur]; i!=-1; i=es[i].next){
v = es[i].to;
if(dis[v]==-1 && es[i].cap>0){
dis[v] = dis[cur]+1;
que.push(v);
}
}
}
if(dis[t] == -1) return false;
else return true;
}
LL DFS(int cur, LL low)
{
LL res=0, add=0;
if(cur == t) return low;
for(int i=nowedge[cur]; i!=-1; i=es[i].next){
nowedge[cur] = i;
if(dis[es[i].to]==dis[cur]+1
&& es[i].cap>0
&& (add = DFS(es[i].to, min(low, (LL)es[i].cap))) ){
es[i].cap -= add;
es[i^1].cap += add;
low -= add;
res += add;
if(low == 0) break;
}
}
if(res == 0) dis[cur] = 0;
return res;
}
LL MAXFLOW()
{
LL ans=0 , tmp=0;
while(BFS()){
for(int i=s; i<=t; i++){
nowedge[i] = head[i];
}
if(tmp = DFS(s, INF))
ans += tmp;
}
return ans;
}
int left_net(int cur)
{
vis[cur] = 1;
int ans = 1;
for(int i=head[cur]; i!=-1; i = es[i].next){
if(es[i].cap > 0 && !vis[es[i].to]) ans += left_net(es[i].to);
}
return ans;
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)){
init();
s = 0;
t = n+1;
LL sum = 0;
for(int i=1; i<=n; i++){
scanf("%d", &b[i]);
if(b[i] > 0) add(s, i, b[i]), sum+=b[i];
else add(i, t, -b[i]);
}
for(int i=1; i<=m; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v, INF);
}
LL ans = sum - MAXFLOW();
printf("%d %lld\n", left_net(s)-1, ans);
}
}