SPOJ 839 Optimal Marks

839. Optimal Marks

Problem code: OPTM

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 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>

#define PB push_back
#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 510
#define E 500000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;

struct Edge
{
    int en,cap,flow,next;
} edge[E];
int head[N] , tot , now[N];
int source,sink,tot_num;
int pre[N] , dis[N] , gap[N];

int bit[N][40];
vector<int> adj[N];
bool fg[N];
bool vs[N];

void add_edge(int st,int en,int cap)
{
    edge[tot].en=en;
    edge[tot].cap=cap;
    edge[tot].flow=0;
    edge[tot].next=head[st];
    head[st]=tot++;

    edge[tot].en=st;
    edge[tot].cap=0;
    edge[tot].flow=0;
    edge[tot].next=head[en];
    head[en]=tot++;
}

void augment(int flow)
{
    for(int i=source;i!=sink;i=edge[now[i]].en)
    {
        edge[now[i]].flow+=flow;
        edge[now[i]^1].flow-=flow;
    }
}

int sap()
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(int i=0;i<tot_num;i++)
        now[i]=head[i];
    gap[0]=tot_num;
    int point=source,flow=0,min_flow=INT_INF;
    while(dis[source]<tot_num)
    {
        bool fg=false;
        for(int i=now[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && dis[point]==dis[edge[i].en]+1)
            {
                min_flow=min(min_flow,edge[i].cap-edge[i].flow);
                now[point]=i;
                pre[edge[i].en]=point;
                point=edge[i].en;
                if(point==sink)
                {
                    flow+=min_flow;
                    augment(min_flow);
                    point=source;
                    min_flow=INT_INF;
                }
                fg=true;
                break;
            }
        if(fg) continue;
        if(--gap[dis[point]]==0) break;
        int Min=tot_num;
        for(int i=head[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
            {
                Min=dis[edge[i].en];
                now[point]=i;
            }
        gap[dis[point]=Min+1]++;
        if(point!=source) point=pre[point];
    }
    return flow;
}

void build(int n,int Bit)
{
    memset(head,-1,sizeof(head));
    tot=0;
    source=0; sink=n+1; tot_num=n+2;
    for(int i=1; i<=n; i++)
        for(int j=0; j<(int)adj[i].size(); j++)
            add_edge(i,adj[i][j],1);
    for(int i=1; i<=n; i++)
        if(fg[i])
        {
            if(bit[i][Bit]==1) add_edge(source,i,INT_INF);
            if(bit[i][Bit]==0) add_edge(i,sink,INT_INF);
        }
}

void dfs(int u)
{
    vs[u]=true;
    for(int i=head[u],v; i!=-1; i=edge[i].next)
    {
    	v=edge[i].en;
    	if(!vs[v] && edge[i].cap-edge[i].flow>0)
    	    dfs(v);
    }
}

int main()
{
    int t; scanf("%d",&t);
    for(int ca=1; ca<=t; ca++)
    {
    	int n,m;
    	scanf("%d%d",&n,&m);
    	for(int i=1; i<=n; i++)
    		adj[i].clear();
        for(int i=0,a,b; i<m; i++)
        {
        	scanf("%d%d",&a,&b);
        	adj[a].PB(b);
        	adj[b].PB(a);
        }
        memset(bit,0,sizeof(bit));
        memset(fg,0,sizeof(fg));
        int k,max_bit=0;
        scanf("%d",&k);
        for(int i=0,a,b; i<k; i++)
        {
        	scanf("%d%d",&a,&b);
        	fg[a]=true;
        	int pos=0;
        	for(; b>0; pos++,b/=2)
                bit[a][pos]=b%2;
            bit[a][pos]=b;
            if(pos>max_bit) max_bit=pos;
        }
        for(int Bit=0; Bit<max_bit; Bit++)
        {
            build(n,Bit);
            int ans=sap();
            memset(vs,0,sizeof(vs));
            dfs(source);
            for(int i=1; i<=n; i++)
                if(!fg[i] && vs[i])
                    bit[i][Bit]=1;
        }
        for(int i=1; i<=n; i++)
        {
            int cnt=0;
            for(int j=0; j<=max_bit; j++)
                cnt+=bit[i][j]*(1<<j);
            printf("%d\n",cnt);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章