You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range [0..231 – 1]. Different vertexes may have the same mark.

For an edge (u, v), we define Cost(u, v) = mark[u] xor mark[v].

Now we know the marks of some certain nodes. You have to determine the marks of other nodes so that the total cost of edges is as small as possible.

Input

The first line of the input data contains integer T (1 ≤ T ≤ 10) - the number of testcases. Then the descriptions of T testcases follow.

First line of each testcase contains 2 integers N and M (0 < N <= 500, 0 <= M <= 3000). N is the number of vertexes and M is the number of edges. Then M lines describing edges follow, each of them contains two integers u, v representing an edge connecting u and v.

Then an integer K, representing the number of nodes whose mark is known. The next K lines contain 2 integers u and p each, meaning that node u has a mark p. It’s guaranteed that nodes won’t duplicate in this part.

Output

For each testcase you should print N lines integer the output. The Kth line contains an integer number representing the mark of node K. If there are several solutions, you have to output the one which minimize the sum of marks. If there are several solutions, just output any of them.

Example

Input:
1
3 2
1 2
2 3
2
1 5
3 100 Output:
5
4
100

详情请见胡伯涛的论文

经典最小割模型

首先我们分位处理,因为每一位都是独立的,互不影响,然后每个点就变成0或1

再想一下割集的定义,割集把一个图分成了两块,而割集就是这两块之间的连边(好吧,这是我乱编的.....)

xor正好是值不同才会贡献答案,于是建立最小割模型

设一个源s,向确定是1的点连容量为inf的边(因为他不能贡献答案,不能让他成为最小割)

设一个汇t,从确定是0的点向汇连容量为inf的边,理由同上

其他不确定的点就和所有的点(除了s和t)连容量为1的边(双向都要连)

然后跑最大流,也就是最小割

然后我们看这个割集,他把图分成了两部分,最后与s在一起的就是最后为1的(即在残留网络上可以从s走到他),不与s在一起的就是最后为0的,因为是最小割,所以是代价最小的

然后我们要让总和最小,其实我们已经做到了

与s在一起的必须是1,可以画图看一看,不与s在一起的不一定选1,为了和最小,就选0

每一位做一次,最后输出答案就行了

 const
maxn=;
inf=;
var
t,si,ti,n,m,step,time:longint;
flag:array[..maxn]of boolean;
tu:array[..maxn,..maxn]of boolean;
map:array[..maxn,..maxn]of longint;
a,dis,vh,his,pre,vis:array[..maxn]of longint; procedure sap;
var
i,j,min,aug:longint;
flag:boolean;
begin
fillchar(dis,sizeof(dis),);
fillchar(vh,sizeof(vh),);
i:=si;
vh[]:=n+;
aug:=inf;
while dis[i]<n+ do
begin
his[i]:=aug;
flag:=false;
for j:= to ti do
if tu[i,j] then
if (map[i,j]>) and (dis[i]=dis[j]+) then
begin
if aug>map[i,j] then aug:=map[i,j];
flag:=true;
pre[j]:=i;
i:=j;
if i=ti then
begin
while i<>si do
begin
inc(map[i,pre[i]]);
dec(map[pre[i],i]);
i:=pre[i];
end;
aug:=inf;
end;
break;
end;
if flag then continue;
min:=n+;
for j:= to ti do
if tu[i,j] then
if (map[i,j]>) and (dis[j]<min) then min:=dis[j];
dec(vh[dis[i]]);
if vh[dis[i]]= then break;
dis[i]:=min+;
inc(vh[dis[i]]);
if i<>si then
begin
i:=pre[i];
aug:=his[i];
end;
end;
end; procedure dfs(x:longint);
var
i:longint;
begin
vis[x]:=time;
if flag[x]=false then inc(a[x],<<step);
for i:= to ti do
if vis[i]<>time then
if tu[x,i] then
if map[x,i]> then dfs(i);
end; procedure main;
var
j,k,m,x,y:longint;
begin
fillchar(flag,sizeof(flag),false);
fillchar(tu,sizeof(tu),false);
fillchar(a,sizeof(a),);
read(n,m);
si:=;
ti:=n+;
for j:= to m do
begin
read(x,y);
tu[y,x]:=true;
tu[x,y]:=true;
end;
for j:= to n do
begin
tu[si,j]:=true;
tu[j,ti]:=true;
end;
read(m);
for j:= to m do
begin
read(x,y);
flag[x]:=true;
a[x]:=y;
end;
for step:= to do
begin
fillchar(map,sizeof(map),);
for j:= to n do
if flag[j] then
if a[j] and (<<step)= then
begin
map[j,ti]:=inf;
for k:= to n do
if flag[k]=false then map[k,j]:=;
end
else
begin
map[si,j]:=inf;
for k:= to n do
if flag[k]=false then map[j,k]:=;
end
else
for k:=j+ to n do
if flag[k]=false then
begin
map[j,k]:=;
map[k,j]:=;
end;
sap;
inc(time);
dfs();
end;
for j:= to n do
writeln(a[j]);
end; begin
read(t);
while t> do
begin
main;
dec(t);
end;
end.

最新文章

  1. 在Pycharm中使用GitHub
  2. Android 四大组件之再论BroadCast
  3. 《你必须知道的.NET》读书笔记:方法表初窥
  4. Hyper-V 共享式网络链接 端口映射
  5. asp.net中TreeView的大数据加载速度优化
  6. [stl] SGI STL的空间配置器
  7. CSS布局属性
  8. DHCP 工作原理
  9. C51关键字
  10. android layoutparams应用指南(转)
  11. ubuntu apt-get常用命令的使用
  12. SilkTest Q&amp;A 12
  13. java 测试IP
  14. 构建微服务-使用OAuth 2.0保护API接口
  15. Java课程设计博客(个人)
  16. Tomcat手动部署Web项目详细步骤
  17. Eclipse编程中免除alt+斜杠,设置自动提示
  18. CSS选择器 + Xpath + 正则表达式整理(有空再整理)
  19. crossdomain.xml配置不当的利用和解决办法
  20. 异常解决 Unable to write generated Java files for schemas: null

热门文章

  1. MyBatis(3.2.3) - Mapped statements: The INSERT statement, Autogenerated keys
  2. MyEclipse中配置SWT/JFace/SWT-Designer 艰辛路程
  3. Access和Sql区别
  4. android自学笔记(1):android简介
  5. 第六十四篇、OC_计步器
  6. 5中方式实现String反转
  7. 【wenqi】重置Centos 7 Root密码
  8. [UNIX环境高级编程](第三版)中apue.h的问题
  9. JAVA_SE复习(OOP2)
  10. Mysql配置文件my.cnf解析