839. Optimal MarksProblem 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.
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.
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.
Input: 1 3 2 1 2 2 3 2 1 5 3 100 Output: 5 4 100
《最小割模型在信息学竞赛中的应用》作者胡伯涛
里面的题目,上面直接给建图方法求最小割,原因没有确切的指出,可以试着直观理解,实际上把对于任意割C(S,T),S中的点节点可以全部赋成相同的值,这样对于S内部的边来说异或全部为0,T中的点同理, 这样只有e∈C(S,T)异或值为1,又因为每条边的容量为1,所以只要求最小的C(S,T)即可,即最小割,证明方法还没有想到o(╯□╰)o
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> #include <vector> #include <cstring> #include <stack> #include <cctype> #include <utility> #include <map> #include <string> #include <climits> #include <set> using std::priority_queue; using std::vector; using std::swap; using std::stack; using std::sort; using std::max; using std::min; using std::pair; using std::map; using std::string; using std::cin; using std::cout; using std::set; using std::queue; const int MAXN(510); const int MAXE(3010); const int INFI((INT_MAX-1) >> 1); int first[MAXN]; int u[(MAXE << 2)+(MAXN << 1)], v[(MAXE << 2)+(MAXN << 1)]; int lf[(MAXE << 2)+(MAXN << 1)], next[(MAXE << 2)+(MAXN << 1)]; int rear; void init() { memset(first, -1, sizeof(first)); rear = 0; } void insert(int tu, int tv, int tc) { u[rear] = tu; v[rear] = tv; lf[rear] = tc; next[rear] = first[tu]; first[tu] = rear++; u[rear] = tv; v[rear] = tu; lf[rear] = 0; next[rear] = first[tv]; first[tv] = rear++; } int ans[MAXN]; int level[MAXN]; int s, t; bool bfs() { memset(level, -1, sizeof(level)); level[s] = 0; queue<int> que; que.push(s); while(!que.empty()) { int cur = que.front(); que.pop(); for(int i = first[cur]; i != -1; i = next[i]) if(lf[i]) { int tv = v[i]; if(level[tv] == -1) { level[tv] = level[cur]+1; que.push(tv); } } } return level[t] != -1; } int dfs(int cur, int limit) { if(cur == t) return limit; int tf = 0; for(int i = first[cur]; i != -1; i = next[i]) if(lf[i]) { int tv = v[i]; if(level[tv] == level[cur]+1) { int temp = dfs(tv, min(limit-tf, lf[i])); tf += temp; lf[i] -= temp; lf[i^1] += temp; } } if(tf == 0) level[cur] = -1; return tf; } int dinic() { int ret = 0; while(bfs()) { ret += dfs(s, INFI); } return ret; } int cbit(int sour) { int l = 0, r = 31; while(l < r) { int m = (l+r) >> 1; if(sour >> m) { l = m+1; } else { r = m; } } return l; } /*bool vis[MAXN]; void mark(int cur, int tb) { vis[cur] = true; ans[cur] |= 1 << tb; for(int i = first[cur]; i != -1; i = next[i]) if(lf[i] && !vis[v[i]]) { mark(v[i], tb); } } */ bool certain[MAXN]; int edge[MAXE][2]; int main() { int n_case; scanf("%d", &n_case); while(n_case--) { int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i) scanf("%d%d", edge[i], edge[i]+1); int k; scanf("%d", &k); int mx = 0; memset(ans, 0, sizeof(ans)); memset(certain, 0, sizeof(certain)); for(int i = 0; i < k; ++i) { int temp; scanf("%d", &temp); certain[temp] = true; scanf("%d", ans+temp); mx = max(mx, cbit(ans[temp])); } s = n+1; t = n+2; for(int i = 0; i < mx; ++i) { init(); for(int j = 1; j <= n; ++j) if(certain[j]) { if(ans[j]&(1 << i)) { insert(s, j, INFI); } else { insert(j, t, INFI); } } for(int j = 0; j < m; ++j) { insert(edge[j][0], edge[j][1], 1); insert(edge[j][1], edge[j][0], 1); } dinic(); for(int j = 1; j <= n; ++j) if(level[j] != -1) ans[j] |= 1 << i; // mark(s, i); } for(int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } } return 0; }