给一个图,要求删一些边,使每个点的度数为给定的 d i ( 1 ≤ d i ≤ 2 ) d_i(1\leq d_i\leq2) di(1≤di≤2)。
每个点拆成 i i i和 i ′ i' i′,源点 S S S和 i i i连一条权值为 d i d_i di的边, i ′ i' i′和汇点 T T T连一条权值为 d i d_i di的边; m m m条边, u u u和 v ′ v' v′、 u ‘ u‘ u‘和 v v v建边。
然后只要判断最大流与 ∑ d i \sum d_i ∑di是否相等即可。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
#define inf 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef double db;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<double, double> pdd;
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 8e5 + 10;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
struct mxfl {
int n = 0;
int m = 0;
int s, t;
struct Edge {
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[N];
void init(int _n, int _s, int _t) {
n = _n;
s = _s;
t = _t;
edges.clear();
for (int i = 1; i <= n; i++)G[i].clear();
}
void add_edge(int from, int to, int cap) {
edges.push_back(Edge{from, to, cap, 0});
edges.push_back(Edge{to, from, 0, 0});
m = (int) edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool vis[N];
int d[N], cur[N];
bool bfs() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < (int) G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == 0)return a;
int flow = 0, f;
for (int &i = cur[x]; i < (int) G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)break;
}
}
return flow;
}
int maxflow() {
int flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, inf);
}
return flow;
}
} MF;
int n, m;
int d[N];
int main() {
while (scanf("%d%d", &n, &m) == 2) {
int S = 2 * n + 1, T = 2 * n + 2;
MF.init(2 * n + 2, S, T);
int sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &d[i]);
MF.add_edge(S, i, d[i]);
MF.add_edge(n + i, T, d[i]);
sum += d[i];
}
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
MF.add_edge(u, v + n, 1);
MF.add_edge(v, u + n, 1);
}
if (MF.maxflow() != sum)puts("No");
else puts("Yes");
}
return 0;
}