题意
思路
首先分析一下边权是如何计算的,边权等于两点的异或值,由于异或是按位计算,不同位之间是独立,因此我们可以单独看一位,只要每一位的答案最小,然后将这些位的答案拼起来,就是最终的答案。
每一位的数值非\(0\)即\(1\),因此可以划分成两个集合。对于已经知道点权的点,如果是\(0\),就与源点\(S\)相连,容量是\(\infty\);如果是\(1\),就与汇点\(T\)相连,容量是\(\infty\)
其他的点有可能属于\(0\)集合,也可能属于\(1\)集合,因此最小割即为答案。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 510, M = 8010, inf = 1e8;
int n, m, S, T, K;
int h[N], e[M], ne[M], f[M], idx;
int cur[N], d[N], p[N];
pii edge[M];
void add(int a, int b, int c1, int c2)
{
e[idx] = b, f[idx] = c1, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, f[idx] = c2, ne[idx] = h[b], h[b] = idx ++;
}
void build(int k)
{
memset(h, -1, sizeof(h));
idx = 0;
for(int i = 1; i <= n; i ++) {
if(p[i] != -1) {
int t = p[i] >> k & 1;
if(t) add(i, T, inf, 0);
else add(S, i, inf, 0);
}
}
for(int i = 0; i < m; i ++) {
int a = edge[i].x, b = edge[i].y;
add(a, b, 1, 1);
}
}
bool bfs()
{
memset(d, -1, sizeof(d));
queue<int> que;
que.push(S);
d[S] = 0, cur[S] = h[S];
while(que.size()) {
int t = que.front();
que.pop();
for(int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if(d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if(ver == T) return true;
que.push(ver);
}
}
}
return false;
}
int find(int u, int limit)
{
if(u == T) return limit;
int flow = 0;
for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if(d[ver] == d[u] + 1 && f[i]) {
int t = find(ver, min(f[i], limit - flow));
if(!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
ll dinic(int k)
{
build(k);
int res = 0, flow;
while(bfs()) {
while(flow = find(S, inf)) {
res += flow;
}
}
return res;
}
int main()
{
scanf("%d%d", &n, &m);
memset(p, -1, sizeof(p));
S = 0, T = n + 1;
for(int i = 0; i < m; i ++) {
int a, b;
scanf("%d%d", &a, &b);
edge[i] = {a, b};
}
scanf("%d", &K);
for(int i = 0; i < K; i ++) {
int u, x;
scanf("%d%d", &u, &x);
p[u] = x;
}
ll res = 0;
for(int i = 0; i < 31; i ++) res += dinic(i) << i;
printf("%lld\n", res);
return 0;
}