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 题目大意:
每一条边的权值定义为x xor y,(x,y是两端点),有一些点的权值已知,求剩下的点怎么弄,是总边权最小。
题解:
首先要知道xor操作时,二进制的每一位都是独立的,不相互影响,所以可以分开处理。
对于每一位,我们把未知的点初始为0,然后对已知的进行操作:当前位为1的S到i有一条边,容量为INF,为0则到T有一条为INF边。
然后对于每一条边:(i,j)拆成(i,j,1)(j,i,1)
然后跑最小割,可以发现对于每一个子图,最小割不是割在T就是割在S,割在T表示前面一堆点都设为1(因为已知的1比0多),隔在S表示后面一堆点都设为0(因为已知的0比1多)
知道了这个,于是在跑完最小割之后就从S开始把能到的点都标为1。
至于反向弧为什么为1,一是原图无向,二就是为了这个时候能全都遍历到。
贴代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,M=,INF=;
int gi(){
int str=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>=''&&ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int n,m,mark[N];
struct Edge{
int x,y;
}e[M];
bool d[N];bool vis[N];
int num=,head[N],S=,T;
struct Lin{
int next,to,dis;
}a[M*];
void init(int x,int y,int z){
a[++num].next=head[x];
a[num].to=y;
a[num].dis=z;
head[x]=num;
a[++num].next=head[y];
a[num].to=x;
a[num].dis=(z==INF?INF:);
head[y]=num;
}
int q[N],dep[N];
bool bfs()
{
memset(dep,,sizeof(dep));
dep[S]=;q[]=S;int u,x,sum=,t=;
while(t!=sum)
{
x=q[++t];
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u]||a[i].dis<=)continue;
dep[u]=dep[x]+;q[++sum]=u;
}
}
return dep[T];
}
int dfs(int x,int flow)
{
if(x==T || !flow)return flow;
int tmp,sum=,u;
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u]!=dep[x]+ || a[i].dis<=)continue;
tmp=dfs(u,min(flow,a[i].dis));
sum+=tmp;flow-=tmp;
a[i].dis-=tmp;a[i^].dis+=tmp;
if(!flow)break;
}
return sum;
}
void maxflow(){
int tmp;
while(bfs()){
tmp=dfs(S,INF);
while(tmp)tmp=dfs(S,INF);
}
}
void Reset(){
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
num=;
}
void remark(int x,int pa){
vis[x]=true;
if(!d[x])mark[x]+=pa;
for(int i=head[x];i;i=a[i].next){
if(a[i].dis> && !vis[a[i].to])remark(a[i].to,pa);
}
}
void check(int fx)
{
Reset();
int pa=(<<fx);
for(int i=;i<=n;i++){
if(!d[i])continue;
if(mark[i]&pa)init(S,i,INF);
else init(i,T,INF);
}
for(int i=;i<=m;i++)init(e[i].x,e[i].y,);
maxflow();
remark(S,pa);
}
void work()
{
int pp,x;
n=gi();m=gi();
T=n+;
for(int i=;i<=m;i++)e[i].x=gi(),e[i].y=gi();
pp=gi();
for(int i=;i<=pp;i++)x=gi(),mark[x]=gi(),d[x]=true;
for(int i=;i<=;i++)check(i);
for(int i=;i<=n;i++)printf("%d\n",mark[i]);
}
void Clear(){
memset(mark,,sizeof(mark));
memset(d,,sizeof(d));
}
int main()
{
//freopen("pp.in","r",stdin);
int TT=gi();
while(TT--){
work();
Clear();
}
return ;
}