839. Optimal Marks - SPOJ

时间:2021-12-17 19:36:15

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.