spoj 839 OPTM - Optimal Marks (最小割)

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 

题解:最小割

按照bzoj 2400的方式见图。

因为我们需要知道哪些点在当前位的取值是0,我们从S开始按照remain不等于0的边进行dfs,所能遍历到的点就是在当前位取值为0的点。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define N 20003
#define inf 1000000000
#define LL long long
using namespace std;
int n,m,val[N],a[503][32],mx,mi[N],tot;
int point[N],v[N],remain[N],next[N];
int last[N],deep[N],cur[N],num[N],x[N],y[N],id[N],mark[N];
void add(int x,int y,int z)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; 
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; 
}
int addflow(int s,int t)
{
	int now=t; int ans=inf;
	while (now!=s)  {
		ans=min(ans,remain[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s) {
		remain[last[now]]-=ans;
		remain[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
void bfs(int s,int t)
{
	queue<int> p;
	for (int i=1;i<=t;i++) deep[i]=t;
	deep[t]=0; p.push(t);
	while (!p.empty()) {
		int now=p.front(); p.pop();
		for (int i=point[now];i!=-1;i=next[i])
		 if (deep[v[i]]==t&&remain[i^1])
		  deep[v[i]]=deep[now]+1,p.push(v[i]);
	}
}
int isap(int s,int t)
{
	int now=s; int ans=0; 
	bfs(s,t);
	for (int i=1;i<=t;i++) num[deep[i]]++;
	for (int i=1;i<=t;i++) cur[i]=point[i];
	while (deep[s]<t) {
		if (now==t) {
			ans+=addflow(s,t);
			now=s;
		}
		bool pd=false;
		for (int i=cur[now];i!=-1;i=next[i]) 
		 if (deep[now]==deep[v[i]]+1&&remain[i]) {
		 	last[v[i]]=i;
		 	cur[now]=i;
		 	now=v[i];
		 	pd=true;
		 	break;
		 }
		if (!pd) {
			int minn=t;
			for (int i=point[now];i!=-1;i=next[i])
			 if (remain[i]) minn=min(minn,deep[v[i]]);
			if (!--num[deep[now]]) break;
			num[deep[now]=minn+1]++;
			cur[now]=point[now];
			if (now!=s) now=v[last[now]^1];
		}
	} 
	return ans;
}
void dfs(int x)
{
	mark[x]=true;
	for (int i=point[x];i!=-1;i=next[i])
	 if (!mark[v[i]]&&remain[i]) dfs(v[i]);
}
int main()
{
	freopen("a.in","r",stdin);
	//freopen("my.out","w",stdout);
    int T; scanf("%d",&T);
    for (int l=1;l<=T;l++) {
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]);
		mx=0;
		memset(a,-1,sizeof(a));
		memset(val,-1,sizeof(val));
		int q; scanf("%d",&q);
		for (int j=1;j<=q;j++) {
	        int i; scanf("%d",&i); scanf("%d",&val[i]);
			int x=val[i];  int cnt=0;
			while (x) {
				cnt++;
				a[i][cnt]=x&1;
				x>>=1;
			}
			mx=max(mx,cnt);
		}
		mi[0]=1;
		int s=1; int t=n+2;
		for (int i=1;i<=31;i++) mi[i]=mi[i-1]*2;
		for (int i=1;i<=mx;i++) {
			tot=-1;
			memset(point,-1,sizeof(point));
			memset(num,0,sizeof(num));
			memset(id,0,sizeof(id));
			for (int j=1;j<=n;j++) {
				if (val[j]!=-1) {
			     if (a[j][i]==0||a[j][i]==-1) add(s,j+1,inf);
			     else add(j+1,t,inf);
		        }
		        else add(s,j+1,1),id[j]=tot;
			}
			for (int j=1;j<=m;j++) {
				add(x[j]+1,y[j]+1,100000); 
				add(y[j]+1,x[j]+1,100000); 
			}
			int flow=isap(s,t);
			memset(mark,0,sizeof(mark));
			dfs(s);
			for (int j=1;j<=n;j++)
			 if (!mark[j+1]) a[j][i]=1;
		}
		for (int i=1;i<=n;i++) {
			int ans=0;
			if (val[i]!=-1) {
			   printf("%d\n",val[i]);
			   continue;
		    }
			for (int j=1;j<=mx;j++) 
			 if (a[i][j]!=-1) ans+=mi[j-1]*a[i][j];
			printf("%d\n",ans);
		}
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值