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. sharepoint 中用自带的download.aspx实现文件的下载,中文文件名编码的问题

    ]中的路径绑定的是下载路径,用到了sharepoint中自带的download.aspx下载页面,只要将文件的URL赋值给sourceurl即可,但是我前台用的是<a>标签的href来导向 ...

  2. 每天一道LeetCode--169.Majority Elemen

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. C# 日期减法

    public class DateExample { public static void Main() { DateTime dt1 = new DateTime(2012, 7, 16); Dat ...

  4. Entity Framework 6.1 学习系列1--概况、安装

    原文:Entity Framework 6.1 学习系列1--概况.安装 Entity Framework:实体框架,看名字就知道是针对模型数据的.这是MS推出的一款ORM工具. 与NHibernat ...

  5. MacBook Pro 的照相机在哪?

    用于拍照 用于录制视频

  6. CentOS 7 下Docker的安装

    笔者是刚入行1年的新鸟,近期公司开始推Docker技术.这个系列的文章都是基于CentOS7系统下进行讲解的. Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及 ...

  7. 对JavaScript莫名的愤怒

    很多时候,我都察觉JavaScript有一中描述性语言的特性,为了实现他的功能,在浏览器中完美的发挥作用,他就像是一个巨型的工厂工程模式,外挂了很多API和功能集,他们试图用完美的方案去解释所有必须的 ...

  8. (POJ 1797) Heavy Transportation 最大生成树

    题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...

  9. undrop for innodb c_parser 不完美之处

    今天发现c_parser导出数据是会丢掉某些行,给过调试发现是他处理utf8编码时计算有误,目前还没有发现自动解决总是的方法,只会手动改代码来解决. 下一步计划把c_parser移植到windows下 ...

  10. strlen() 和 strcpy()函数

    strlen() 和 strcpy()函数的区别,这两个一个是返回一个C风格字符串的长度,一个是对一个C风格字符串的拷贝,两个本来功能上是不同的,此外,他们还有一些细小的区别:strlen(" ...