time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.

Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.

Input

First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200).

Next line contains n integers a1, a2, …, an (0 ≤ ai ≤ 100).

Next line contains n integers b1, b2, …, bn (0 ≤ bi ≤ 100).

Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q.

It is guaranteed that there is at most one road between each pair of cities.

Output

If the conditions can not be met output single word “NO”.

Otherwise output word “YES” and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).

If there are several possible answers you may output any of them.

Examples

input

4 4

1 2 6 3

3 5 3 1

1 2

2 3

3 4

4 2

output

YES

1 0 0 0

2 0 0 0

0 5 1 0

0 0 2 1

input

2 0

1 2

2 1

output

NO

【题目链接】:http://codeforces.com/problemset/problem/546/E

【题解】



题意:

每个点上的士兵只能走到相邻的点(只能走一次);

然后问你目标状态能不能达到;

做法:

按照下面这张图的规则建边;

把n个点每个点分成入点和出点两个点;

虚拟一个起点和终点(作为源点和汇点);

入点都和起点连在一起;边权为ai;

出点都和终点连在一起;边权为bi;

然后入点和出点之间接一条边;边权为INF;

如果x和y之间有一条边;

则在x的入点和y的出点之间,以及y的入点以及x的出点之间建INF的边;

然后从起点开始跑最大流;



这样造成的效果是,每个点的士兵都只会到和其距离为1的点(然后进入汇点)

显然∑ai==∑bi要满足

这样f[起点][1..i]和f[i+n][汇点]必然都是满流的;

以这个作为判断是否有解的依据;

然后输出每个点到其他点的流量就好;



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 100+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n,m,s,t;
int g[MAXN*2][MAXN*2],f[MAXN*2][MAXN*2];
int flag[MAXN*2]; int dfs(int x,int m)
{
if (x==t || !m) return m;
if (flag[x]++) return 0;
rep1(y,1,2*n+2)
{
int judge = dfs(y,min(m,g[x][y]-f[x][y]));
if (judge)
{
f[x][y]+=judge;
f[y][x]-=judge;
return judge;
}
}
return 0;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
s = 2*n+1,t = 2*n+2;
rep1(i,1,n)
{
int x;
rei(x);
g[s][i] = x;
}
rep1(i,1,n)
{
int x;
rei(x);
g[i+n][t] = x;
}
rep1(i,1,n)
g[i][i+n] = 100000;
rep1(i,1,m)
{
int x,y;
rei(x);rei(y);
g[x][y+n] = 100000;
g[y][x+n] = 100000;
}
while (dfs(s,100000))
memset(flag,0,sizeof flag);
rep1(i,1,n)
if (f[s][i]!=g[s][i] || f[i+n][t]!=g[i+n][t])
{
puts("NO");
return 0;
}
puts("YES");
rep1(i,1,n)
rep1(j,1,n)
{
printf("%d",f[i][j+n]);
if (j==n)
puts("");
else
putchar(' ');
}
return 0;
}

最新文章

  1. Android Studio插件之快速findViewById(butterknife和Android CodeGenerator的使用)
  2. Python基础篇【第7篇】: 面向对象(1)
  3. Web系统大规模并发——电商秒杀与抢购 【转】
  4. ASP.Net MVC的ViewBag一个坑,不要跳进去
  5. [游戏模版21] Win32 物理引擎 能量守恒
  6. Windows Server 安装 BitLocker
  7. 二十八、带给我们一种新的编码思路——EFW框架CS系统开发中的MVC模式探讨
  8. html部分---a标签的用法、放置图片与表格;
  9. 不容错过的七个jQuery图片滑块插件
  10. WPF处理Windows消息
  11. 转自 z55250825 的几篇关于FFT的博文(一)
  12. css字体转换程序(Node.js)
  13. iOS程序员的自我修养之道
  14. Java枚举7常见种用法
  15. MySQL简单查询
  16. Oracle 调用存储过程执行CRUD的小DEMO
  17. emguCv3.x 实现字符分割,轮廓检测
  18. [ExtJS5学习笔记]第三十三节 sencha extjs 5 grid表格导出excel
  19. JavaScript进阶(四)js字符串转换成数字的三种方法
  20. SHELL输出带颜色字体

热门文章

  1. [Python] Different ways to test multiple flags at once in Python
  2. Dell shareplex 与HVR数据复制软件
  3. 怎样让索引仅仅能被一个SQL使用
  4. Netty推荐addListener回调异步执行
  5. 利用ServiceWorker实现页面的快速加载和离线访问
  6. Flask项目之手机端租房网站的实战开发(十二)
  7. 洛谷 P1170 兔八哥与猎人
  8. Android 快速下载 Android framework 源码
  9. JS实现动画方向切换效果(包括:撞墙反弹,暂停继续左右运动等)
  10. 具体解释ebs接口之客户配置文件导入(二)