Optimal markstime limit:6000msmemory limit:262144kbthis problem would be judged onSpoj. Original Id:optm
64-bit integer IO format: %lld Java class name: Main
You is given an undirected graph G (V, E). Each vertex have a mark which is an integer from the range [0..231–1]. Different vertexes may have the same mark.
For a edge (U, v), we define cost (U, v) = Mark[u] xor mark[v].
Now we know the marks of some certain nodes. You has to determine the marks of other nodes so, 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 <=, 0 <= M <= 3000). N is the number of vertexes and M are the number of edges. Then M lines describing edges follow, each of the them contains the integers u, v representing an edge connecting u a nd v.
Then a integer K, representing the number of nodes whose mark is known. The next K lines contain 2 integers u and p each, meaning this 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 k-th line contains an integer number representing the mark of node K. If There is several solutions, you has to output the one which minimize the sum of marks. If There is several solutions, just output any of them.
Example
Input:321 +Output:
SourceGuo Huayang Problem Solving: Amber Classmate's paper above some classic minimum cut topic
1#include <bits/stdc++.h>2 using namespacestd;3 Const intINF = ~0U>>2;4 Const intMAXN =510;5 structarc{6 intTo,flow,next;7Arcintx =0,inty =0,intz =-1){8to =x;9Flow =y;TenNext =Z; One } A}e[maxn*MAXN]; - intHead[maxn],gap[maxn],d[maxn],s,t,tot; - voidAddintUintVintflow) { theE[tot] =arc (V,flow,head[u]); -Head[u] = tot++; -E[tot] = arc (U,0, Head[v]); -HEAD[V] = tot++; + } - voidBFs () { +queue<int>Q; AMemset (Gap,0,sizeofgap); atmemset (d,-1,sizeofd); - Q.push (T); -D[T] =0; - while(!Q.empty ()) { - intU =Q.front (); - Q.pop (); in++Gap[d[u]]; - for(inti = Head[u]; ~i; i =E[i].next) { to if(e[i^1].flow && d[e[i].to] = =-1){ +D[e[i].to] = D[u] +1; - Q.push (e[i].to); the } * } $ }Panax Notoginseng } - intSapintUintLow ) { the if(U = = T)returnLow ; + intTMP =0, A,minh = T-1; A for(inti = Head[u]; ~i; i =E[i].next) { the if(e[i].flow) { + if(D[u] = = D[e[i].to] +1){ -A =SAP (E[i].to,min (Low,e[i].flow)); $ if(!a)Continue; $E[i].flow-=A; -e[i^1].flow + =A; -Low-=A; theTMP + =A; - if(!low) Break;Wuyi } theMinH =min (minh,d[e[i].to]); - if(D[s] >= T)returntmp; Wu } - } About if(!tmp) { $ if(--gap[d[u]] = =0) D[s] =T; -++gap[d[u] = MinH +1]; - } - returntmp; A } + intMaxflow (intRET =0){ the BFS (); - while(D[s] < T) ret + =SAP (S,inf); $ returnret; the } the intN,M,K,MARK[MAXN],CON[MAXN]; the BOOLMP[MAXN][MAXN],VIS[MAXN]; the voidBuildintx) { -S = n +1; inT = S +1; thememset (head,-1,sizeofhead); thememset (Vis,false,sizeofvis); Abouttot =0; the for(inti =0; I < K; ++i) { the if((mark[con[i]]>>x) &1) Add (s,con[i],inf); the ElseAdd (con[i],t,inf); + } - for(inti =1; I <= N; ++i) the for(intj =1; J <= N; ++j)Bayi if(Mp[i][j]) Add (i,j,1); the } the voidDfsintUintx) { -Vis[u] =true; -Mark[u] |= (1<<x); the for(inti = Head[u]; ~i; i =e[i].next) the if(!vis[e[i].to] &&e[i].flow) DFS (e[i].to,x); the } the intMain () { - intkase,u,v; thescanf"%d",&Kase); the while(kase--){ thescanf"%d%d",&n,&m);94memset (Mark,0,sizeofmark); theMemset (MP,false,sizeofMP); the for(inti =0; I < m; ++i) { thescanf"%d%d",&u,&v);98MP[U][V] = Mp[v][u] =true; About } -scanf"%d",&k);101 for(inti =0; I < K; ++i) {102scanf"%d%d",&u,&v);103Mark[u] =v;104Con[i] =u; the }106 for(inti =0; I < +; ++i) {107 build (i);108 Maxflow ();109 DFS (s,i); the }111 for(inti =1; I <= N; ++i) theprintf"%d\n", Mark[i]);113 } the return 0; the}
View Code
Spoj Optm Optimal Marks