基础网络流,增加s和t,同时对于每个结点分裂为流入结点和流出结点。EK求最大流,判断最大流是否等于当前总人数。
/* 304E */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
int F[maxn][maxn];
int P[maxn], a[maxn];
bool visit[maxn];
int ans[maxn][maxn];
int ai[maxn], bi[maxn], ci[maxn];
int n, m, s = , t;
bool flag = true; bool bfs() {
int u, v;
queue<int> Q; memset(a, , sizeof(a));
a[s] = INT_MAX;
Q.push(s);
P[s] = s; while (!Q.empty()) {
u = Q.front();
Q.pop();
for (v=; v<=t; ++v) {
if (!a[v] && F[u][v]) {
P[v] = u;
Q.push(v);
a[v] = min(a[u], F[u][v]);
}
}
} return a[t]==;
} int Edmonds_Karp() {
int u, v;
int ans = ; while () {
if (bfs())
break;
for (u=t,v=P[u]; u!=s; u=v, v=P[u]) {
F[u][v] += a[t];
F[v][u] -= a[t];
}
ans += a[t];
}
return ans;
} int main() {
int i, j, k;
int s1, s2; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &m);
t = n+n+;
memset(F, , sizeof(F));
memset(ans, , sizeof(ans));
s1 = s2 = ; for (i=; i<=n; ++i) {
scanf("%d", &ai[i]);
s1 += ai[i];
F[][i] = ai[i];
} for (i=; i<=n; ++i) {
scanf("%d", &bi[i]);
s2 += bi[i];
F[i][i+n] = INT_MAX;
F[i+n][t] = bi[i];
} for (i=; i<m; ++i) {
scanf("%d %d", &j, &k);
F[j][k+n] = INT_MAX;
F[k][j+n] = INT_MAX;
} flag = s1==s2;
if (flag) {
flag = Edmonds_Karp() == s1;
} if (flag) {
for (i=; i<=n; ++i)
for (j=n+; j<t; ++j)
ans[i][j-n] = F[j][i];
puts("YES");
for (i=; i<=n; ++i) {
for (j=; j<=n; ++j)
printf("%d ", ans[i][j]);
putchar('\n');
}
} else {
puts("NO");
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}