214 lines
4.1 KiB
C++
214 lines
4.1 KiB
C++
/* Problem URL: https://codeforces.com/problemset/problem/546/E */
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
int tmp, m;
|
|
cin >> tmp >> m;
|
|
|
|
int n = tmp * 2 + 2;
|
|
|
|
vvi graph(n);
|
|
V<tuple<int, int, int>> edges;
|
|
|
|
auto makeedge = [&](int a, int b, int c){
|
|
graph[a].push_back(edges.size());
|
|
edges.emplace_back(b, c, 0);
|
|
graph[b].push_back(edges.size());
|
|
edges.emplace_back(a, 0, 0);
|
|
};
|
|
|
|
int inf = INT32_MAX >> 1;
|
|
|
|
vi a(tmp);
|
|
vi b(tmp);
|
|
cin >> a >> b;
|
|
|
|
int total = accumulate(all(b), 0);
|
|
if (total != accumulate(all(a), 0)) {
|
|
cout << "NO\n";
|
|
return 0;
|
|
}
|
|
|
|
int s = 0;
|
|
int t = n - 1;
|
|
|
|
rep(i, tmp) {
|
|
makeedge(s, i + 1, a[i]);
|
|
makeedge(i + 1, i + tmp + 1, inf);
|
|
makeedge(i + tmp + 1, t, b[i]);
|
|
}
|
|
|
|
while (m--) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
|
|
makeedge(a, b + tmp, inf);
|
|
makeedge(b, a + tmp, inf);
|
|
}
|
|
|
|
vi level(n);
|
|
|
|
auto bfs = [&](){
|
|
queue<int> q;
|
|
q.push(s);
|
|
level[s] = 0;
|
|
|
|
while (!q.empty()) {
|
|
int u = q.front();
|
|
q.pop();
|
|
|
|
for (auto id : graph[u]) {
|
|
auto [v, c, f] = edges[id];
|
|
if (level[v] > level[u] + 1 && c != f) {
|
|
level[v] = level[u] + 1;
|
|
q.push(v);
|
|
}
|
|
}
|
|
}
|
|
|
|
return level[t] != inf;
|
|
};
|
|
|
|
V<bool> vis(n);
|
|
|
|
function<int(int, int)> dfs = [&](int i, int f){
|
|
if (f == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (i == t) {
|
|
return f;
|
|
}
|
|
|
|
for (auto id : graph[i]) {
|
|
auto &[j, c, fj] = edges[id];
|
|
if (vis[j] || level[j] <= level[i]) {
|
|
continue;
|
|
}
|
|
|
|
int ans = dfs(j, min(f, c - fj));
|
|
if (ans == 0) {
|
|
continue;
|
|
}
|
|
|
|
auto &[_, __, fc] = edges[id ^ 1];
|
|
|
|
fj += ans;
|
|
fc -= ans;
|
|
|
|
return ans;
|
|
}
|
|
|
|
vis[i] = true;
|
|
|
|
return 0;
|
|
};
|
|
|
|
int ans = 0;
|
|
while (true) {
|
|
fill(all(level), inf);
|
|
if (!bfs()) {
|
|
break;
|
|
}
|
|
|
|
fill(all(vis), false);
|
|
|
|
int count = 0;
|
|
while ((count = dfs(s, inf))) {
|
|
ans += count;
|
|
}
|
|
}
|
|
|
|
if (ans != total) {
|
|
cout << "NO\n";
|
|
return 0;
|
|
}
|
|
|
|
vvi act(tmp, vi(tmp, 0));
|
|
|
|
rep(i, tmp) {
|
|
for (auto _ : graph[i + 1]) {
|
|
if ((_ & 1) == 1) {
|
|
continue;
|
|
}
|
|
|
|
auto [j, __, f] = edges[_];
|
|
act[i][j - tmp - 1] = f;
|
|
}
|
|
}
|
|
|
|
cout << "YES\n" << act;
|
|
}
|