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 ;
}

 

【SPOJ839】Optimal Marks 网络流的更多相关文章

  1. [SPOJ839]Optimal Marks

    [SPOJ839]Optimal Marks 试题描述 You are given an undirected graph \(G(V, E)\). Each vertex has a mark wh ...

  2. SPOJ839 Optimal Marks(最小割)

    题目大概说给一张图,每个点都有权,边的权等于其两端点权的异或和,现已知几个点的权,为了使所有边的边权和最小,其他点的权值该是多少. 很有意思的一道题,完全看不出和网络流有什么关系. 考虑每个未知的点$ ...

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

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

  4. spoj839 Optimal Marks(最小割,dinic)

    题目大意: 给你一个无向图\(G(V,E)\). 每个顶点都有一个int范围内的整数的标记. 不同的顶点可能有相同的标记. 对于边\((u,v)\),我们定义\(Cost(u,v)=mark [u]\ ...

  5. 图论(网络流):SPOJ OPTM - Optimal Marks

    OPTM - Optimal Marks You are given an undirected graph G(V, E). Each vertex has a mark which is an i ...

  6. SPOJ OPTM - Optimal Marks

    OPTM - Optimal Marks no tags  You are given an undirected graph G(V, E). Each vertex has a mark whic ...

  7. SP839 Optimal marks(最小割)

    SP839 Optimal marks(最小割) 给你一个无向图G(V,E). 每个顶点都有一个int范围内的整数的标记. 不同的顶点可能有相同的标记.对于边(u,v),我们定义Cost(u,v)= ...

  8. Optimal Marks(optimal)

    Optimal Marks(optimal) 题目描述 定义无向图边的值为这条边连接的两个点的点权异或值. 定义无向图的值为无向图中所有边的值的和. 给定nn个点mm条边构成的图.其中有些点的权值是给 ...

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

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

随机推荐

  1. 学号:201621123032 《Java程序设计》第10周学习总结

    1:本周学习总结 1.1.:以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2:书面作业 2.1.:常用异常--结合题集题目7-1回答 2.1.1:自己以前编写的代码中经常出现什么异常.需要捕 ...

  2. 视频聊天 Demo

    ESFramework Demo -- 入门Demo,简单的即时通讯系统(附源码) 是基于ESFramework实现的一个简单的文字聊天demo,现在,我们将在这个demo的基础上,使用OMCS为其增 ...

  3. HTTP协议中PUT和POST使用区别

          有的观点认为,应该用POST来创建一个资源,用PUT来更新一个资源:有的观点认为,应该用PUT来创建一个资源,用POST来更新一个资源:还有的观点认为可以用PUT和POST中任何一个来做创 ...

  4. Codeforces 837E. Vasya's Function

    http://codeforces.com/problemset/problem/837/E   题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...

  5. js判断flash文件是否加载完毕

    轮询判断加载进度 img的加载完成有onload方法,一直不知道该怎么判断swf文件是否加载完毕了? 在应用中使用了轮询判断加载进度值PercentLoaded是否达到100,经测试,可以达到效果. ...

  6. 11-TypeScript中的名称空间

    在后端开发语言中,比如C#中,可以将不同源代码文件中的代码通过名称空间组合到一起.一般一个类定义在一个源代码文件中,在功能上属于一个上下文的源代码文件通过名称空间进行组织. 在TypeScript中, ...

  7. hexo博客图片问题

    hexo博客图片问题 第一步 首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folde ...

  8. LeetCode & Q1-Two Sum-Easy

    Array Hash Table Question Given an array of integers, return indices of the two numbers such that th ...

  9. [UWP]针对UWP程序多语言支持的总结,含RTL

    UWP 对 Globalization and localization 的支持非常好,可以非常容易地实现应用程序本地化. 所谓本地化,表现最为直观的就是UI上文字和布局方式了,针对文字,提供不同的语 ...

  10. Mysql启动时提示:Another MySQL daemon already running with the same unix socket.

    场景:vmvare虚拟机.centos7.mysql5.7 解决: mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak 参考: htt ...