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.

839. Optimal Marks - SPOJ的更多相关文章

  1. 【bzoj2400】Spoj 839 Optimal Marks 按位最大流

    Spoj 839 Optimal Marks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 908  Solved: 347[Submit][Stat ...

  2. 【BZOJ2400】Spoj 839 Optimal Marks 最小割

    [BZOJ2400]Spoj 839 Optimal Marks Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. ...

  3. BZOJ2400: Spoj 839 Optimal Marks

    Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其 ...

  4. spoj 839 Optimal Marks(二进制位,最小割)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...

  5. Optimal Marks SPOJ 839

    这题远超其他题非常靠近最小割的实际意义: 割边<=>付出代价<=>决定让两个点的值不相同,边权增加 最小割<=>点的值与s一个阵营的与s相同,与t一个阵营的与t相同 ...

  6. SPOJ 839 Optimal Marks(最小割的应用)

    https://vjudge.net/problem/SPOJ-OPTM 题意: 给出一个无向图G,每个点 v 以一个有界非负整数 lv 作为标号,每条边e=(u,v)的权w定义为该边的两个端点的标号 ...

  7. 【bzoj2400】Spoj 839 Optimal Marks 网络流最小割

    题目描述 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其余的点的值由你 ...

  8. BZOJ 2400: Spoj 839 Optimal Marks (按位最小割)

    题面 一个无向图,一些点有固定权值,另外的点权值由你来定. 边的值为两点的异或值,一个无向图的值定义为所有边的值之和. 求无向图的最小值 分析 每一位都互不干扰,按位处理. 用最小割算最小值 保留原图 ...

  9. Optimal Marks SPOJ - OPTM (按位枚举-最小割)

    题意:给一张无向图,每个点有其点权,边(i,j)的cost是\(val_i\ XOR \ val_j\).现在只给出K个点的权值,求如何安排其余的点,使总花费最小. 分析:题目保证权值不超过32位整型 ...

随机推荐

  1. 【解决】该任务映像已损坏或已篡改。(异常来自HRESULT:0x80041321)

    把系统升级到Windows 10,体验了一番Windows 10.感觉不怎么好用退回到了Windows 7,发现我原来自定义的任务计划没有按时执行,于是打开任务计划,弹出了下面的对话框[该任务映像已损 ...

  2. jQuery 显示加载更多

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. Spring(3.2.3) - Beans(6): 作用域

    Spring 支持五种作用域,分别是 singleton.prototype.request.session 和 global session. 作用域 说明  singleton (默认作用域)单例 ...

  4. 【Knockout】二、监控属性Observables

    MVVM和viewModel Knockout是建立在以下三大核心功能之上的: 监控属性和依赖跟踪(Observables and dependency tracking) 声明式绑定(Declara ...

  5. 上传系列:jquery.upload.js

    最近想做一个上传的总结,把自己用过的上传插件都写一写,哪天用到的时候就不用再一次的翻阅资料,现在页面上用到的上传插件,原理上都差不多,那我也就不再废话了,下面我主要记录一下几个比较常用的,由简到繁,这 ...

  6. MTKLogger日志记录打开

    MTKLogger在 拨号界面用*#446633#(不同手机可能设置不一样) 向左滑动到Log and Debugging下面有个MTKLogger,点进去 点击右上角进入设置,打开MobileLog ...

  7. kettle

    Kettle(中文名称叫水壶)是一款ETL工具,纯java编写,可以在Window.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定.Kettle家族包括4个产品:Spoon.Pan.CHE ...

  8. Cocos2d-x优化中关于背景图片优化

    由于背景图片长时间在场景中保存,而且图片很多,我们可以对其进行一些优化.我们通过如下几个方面考虑优化:1.不要Alpha通道背景图片的特点是不需要透明的,所以纹理格式可以采用不带有Alpha通道格式, ...

  9. tip 2:找最小公倍数之Boost

    今天在codewars上面做了一题,kata5的,其中一个实现函数是几个数字的最小公倍数.自己的代码编译虽然也成功了,但是不够简介.看了别人的代码才发现可以直接调用Boost的math模块. 看eff ...

  10. rabbitmq+haproxy+keepalived实现高可用集群搭建

    项目需要搭建rabbitmq的高可用集群,最近在学习搭建过程,在这里记录下可以跟大家一起互相交流(这里只是记录了学习之后自己的搭建过程,许多原理的东西没有细说). 搭建环境 CentOS7 64位 R ...