OPTM - Optimal Marks

no tags 

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
 

Select Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=3e4+5;
const int M=1e6+5;
struct edge{int v,next,cap;}e[M];int tot=1,head[N];
int mark[N],ans[N],dis[N],q[N*10];bool vis[N];
int cas,n,m,k,S,T,a[N][2];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void add(int x,int y,int z1,int z2=0){
e[++tot].v=y;e[tot].cap=z1;e[tot].next=head[x];head[x]=tot;
e[++tot].v=x;e[tot].cap=z2;e[tot].next=head[y];head[y]=tot;
}
inline bool bfs(){
for(int i=S;i<=T;i++) dis[i]=-1;
int h=0,t=1;q[t]=S;dis[S]=0;
while(h!=t){
int x=q[++h];
for(int i=head[x];i;i=e[i].next){
if(e[i].cap&&dis[e[i].v]==-1){
dis[e[i].v]=dis[x]+1;
if(e[i].v==T) return 1;
q[++t]=e[i].v;
}
}
}
return 0;
}
int dfs(int x,int f){
if(x==T) return f;
int used=0,t;
for(int i=head[x];i;i=e[i].next){
if(e[i].cap&&dis[e[i].v]==dis[x]+1){
t=dfs(e[i].v,min(e[i].cap,f));
e[i].cap-=t;e[i^1].cap+=t;
used+=t;f-=t;
if(!f) return used;
}
}
if(!used) dis[x]=-1;
return used;
}
inline int dinic(){
int res=0;
while(bfs()) res+=dfs(S,2e9);
return res;
}
void init(){
n=read();m=read();S=0;T=n+1;
memset(mark,-1,n+1<<2);
for(int i=1;i<=m;i++) a[i][0]=read(),a[i][1]=read();
k=read();
for(int i=1,x,y;i<=k;i++) x=read(),y=read(),mark[x]=y;
}
void DFS(int x,int d){
vis[x]=1;
ans[x]+=d;
for(int i=head[x];i;i=e[i].next){
if(!vis[e[i].v]&&e[i].cap){
DFS(e[i].v,d);
}
}
}
void work(){
memset(ans,0,n+1<<2);
int bite=1;
for(;;){
tot=1;memset(head,0,n+2<<2);
for(int i=1;i<=m;i++) add(a[i][0],a[i][1],1,1);
bool flag=0;
for(int i=1;i<=n;i++){
if(~mark[i]){
if(mark[i]>=1) flag=1;
if(mark[i]&1){
add(S,i,2e9);
}
else{
add(i,T,2e9);
}
mark[i]>>=1;
}
}
if(!flag) break;
dinic();
memset(vis,0,sizeof vis);
DFS(S,bite);bite<<=1;
}
for(int i=1;i<=n;i++) printf("%d ",ans[i]);putchar('\n');
}
int main(){
cas=read();
while(cas--) init(),work();
return 0;
}

SPOJ OPTM - Optimal Marks的更多相关文章

  1. 图论(网络流):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 ...

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

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

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

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

  4. SPOJ 839 OPTM - Optimal Marks (最小割)(权值扩大,灵活应用除和取模)

    http://www.spoj.com/problems/OPTM/ 题意: 给出一张图,点有点权,边有边权 定义一条边的权值为其连接两点的异或和 定义一张图的权值为所有边的权值之和 已知部分点的点权 ...

  5. spoj 839 OPTM - Optimal Marks&&bzoj 2400【最小割】

    因为是异或运算,所以考虑对每一位操作.对于所有已知mark的点,mark的当前位为1则连接(s,i,inf),否则连(i,t,inf),然后其他的边按照原图连(u,v,1),(v,u,1),跑最大流求 ...

  6. BZOJ2400: Spoj 839 Optimal Marks

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

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

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

  8. SPOJ839 OPTM - Optimal Marks

    传送门 闵神讲网络流应用的例题,来水一水 要写出这道题,需要深入理解两个概念,异或和最小割. 异或具有相对独立性,所以我们把每一位拆开来看,即做大概$32$次最小割.然后累加即可. 然后是最小割把一张 ...

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

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

随机推荐

  1. spring Di依赖注入

    依赖注入有两种方式 通过 get   set 方法 Person.java package cn.itcast.spring.sh.di.set; import java.util.List; imp ...

  2. CVTE电话面试

    Cvte电话面试 1. SVM和逻辑回归的相同不同点 2. 特征值和奇异值的区别 3. 如何找到全局最优解,梯度下降和牛顿法区别 4. 防止过拟合的方法 5. 随机森林和ADBOOST方差和偏置 6. ...

  3. LaTex幻灯片制作

    头部声明是“幻灯片”: \documentclass{beamer} 其他: \documentclass{beamer}\usepackage{graphicx}\usepackage{epstop ...

  4. vue 基础-->进阶 教程(2): 指令、自定义指令、组件

    第二章 建议学习时间4小时  课程共3章 前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零 ...

  5. IE浏览器右键菜单分享扩展

    (如果本页面没有自动下载,请点这里下载) IE浏览器分享工具安装步骤:1.发起下载请求后,屏幕上会弹出文件保存对话框,将文件保存到您电脑本地的磁盘中 2.双击刚才下载的安装文件,将JiaThis_Sh ...

  6. 自己动手写CPU之第五阶段(2)——OpenMIPS对数据相关问题的解决措施

    将陆续上传本人写的新书<自己动手写CPU>(尚未出版).今天是第16篇.我尽量每周四篇 5.2 OpenMIPS对数据相关问题的解决措施 OpenMIPS处理器採用数据前推的方法来解决流水 ...

  7. 一.软件介绍(apache lighttpd nginx)

    一.软件介绍(apache  lighttpd  nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...

  8. [docker]docker压力测试

    内存测试 -m --memory-swap 内存+swap docker run -it --rm -m 200M --memory-swap=300M progrium/stress --vm 1 ...

  9. Spark Standalone与Spark on YARN的几种提交方式

    不多说,直接上干货! Spark Standalone的几种提交方式 别忘了先启动spark集群!!! spark-shell用于调试,spark-submit用于生产. 1.spark-shell ...

  10. WebService 页面重定向错误

    “/”应用程序中的服务器错误. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. xxx.xxx.xxx.xxx:xx 说明: 执行当前 Web 请求期间,出现未经处理的异常.请 ...