題目鏈接
給出一幅二分圖,有左側N個點,右側M個點,問能否選用其中給出的K條邊使得每個點的度都爲區間[L, R]上的值,邊都是無向邊。
很明顯,限制了上下限,於是想到了往有上下界可行流上去考慮,將原圖考慮成有源匯點,讓原圖源點到左側的所有點的經過次數都要是[L, R]之間,讓右側的所有點到原圖匯點的經過次數都要是在[L, R]之間,那麼每一條邊的上下限也就確定了。
然後,依據有上下界可行流來進行處理。
假設每條邊的下限是L,上限是R,於是有對於每個點i有
其中,L是可行流的下界,g是額外流,g的範圍是[0, R - L]。
將原式化簡
可以得到
意思是:下界入度 - 下界出度 = 額外流出度 - 額外流入度。
於是,我們假設
則有
x > 0時候,,意思是出度=入度+x
x < 0時候,,意思是入度=出度+(-x)
於是問題就解決了,當x大於0的時候,我們用超級源點和它鏈接流量爲x的邊,當x小於0的時候,我們用超級匯點和它鏈接流量爲-x的邊。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 5e3 + 7, maxM = 4e4 + 7;
int N, M, E, head[maxN], cnt, L, R, ss, tt, In_det_Out[maxN];
struct Eddge
{
int nex, to; ll flow;
Eddge(int a=-1, int b=0, ll c=0):nex(a), to(b), flow(c) {}
}edge[maxM];
inline void addEddge(int u, int v, ll w)
{
edge[cnt] = Eddge(head[u], v, w);
head[u] = cnt++;
}
inline void _add(int u, int v, ll w) { addEddge(u, v, w); addEddge(v, u, 0); }
struct Max_Flow
{
int gap[maxN], d[maxN], que[maxN], ql, qr, cur[maxN], S, T, node;
inline void init()
{
for(int i=0; i<=node + 1; i++)
{
gap[i] = d[i] = 0;
cur[i] = head[i];
}
++gap[d[T] = 1];
que[ql = qr = 1] = T;
while(ql <= qr)
{
int x = que[ql ++];
for(int i=head[x], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(!d[v]) { ++gap[d[v] = d[x] + 1]; que[++qr] = v; }
}
}
}
inline ll aug(int x, ll FLOW)
{
if(x == T) return FLOW;
int flow = 0;
for(int &i=cur[x], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(d[x] == d[v] + 1)
{
ll tmp = aug(v, min(FLOW, edge[i].flow));
flow += tmp; FLOW -= tmp; edge[i].flow -= tmp; edge[i ^ 1].flow += tmp;
if(!FLOW) return flow;
}
}
if(!(--gap[d[x]])) d[S] = node + 1;
++gap[++d[x]]; cur[x] = head[x];
return flow;
}
inline ll max_flow()
{
init();
ll ret = aug(S, INF);
while(d[S] <= node) ret += aug(S, INF);
return ret;
}
} mf;
ll sum;
inline void init()
{
cnt = 0; ss = 0; tt = N + M + 1; mf.S = tt + 1; mf.T = mf.S + 1; mf.node = mf.T + 1; sum = 0;
for(int i=0; i<=mf.node + 1; i++)
{
head[i] = -1;
In_det_Out[i] = 0;
}
for(int i=1; i<=N; i++)
{
In_det_Out[i] += L; In_det_Out[ss] -= L;
_add(ss, i, R - L);
}
for(int i=1; i<=M; i++)
{
In_det_Out[tt] += L; In_det_Out[N + i] -= L;
_add(N + i, tt, R - L);
}
for(int i=ss; i<=tt; i++)
{
if(In_det_Out[i] < 0)
{
sum -= In_det_Out[i];
_add(i, mf.T, -In_det_Out[i]);
}
else if(In_det_Out[i] > 0)
{
sum += In_det_Out[i];
_add(mf.S, i, In_det_Out[i]);
}
}
_add(tt, ss, INF);
}
int main()
{
int Cas = 0;
while(scanf("%d%d%d", &N, &M, &E) != EOF)
{
scanf("%d%d", &L, &R);
init();
for(int i=1, u, v; i<=E; i++)
{
scanf("%d%d", &u, &v);
_add(u, N + v, 1);
}
printf("Case %d: ", ++Cas);
ll ans = mf.max_flow();
if(ans * 2LL == sum) printf("Yes\n");
else printf("No\n");
}
return 0;
}