首页 > 题解 > bzoj2400 Spoj 839 Optimal Marks

bzoj2400 Spoj 839 Optimal Marks

Description

定义无向图中的一条边的值为:这条边连接的两个点的值的异或值。
定义一个无向图的值为:这个无向图所有边的值的和。
给你一个有n个结点m条边的无向图。其中的一些点的值是给定的,而其余的点的值由你决定(但要求均为非负数),使得这个无向图的值最小。在无向图的值最小的前提下,使得无向图中所有点的值的和最小。

Input

第一行,两个数n,m,表示图的点数和边数。
接下来n行,每行一个数,按编号给出每个点的值(若为负数则表示这个点的值由你决定,值的绝对值大小不超过10^9)。
接下来m行,每行二个数a,b,表示编号为a与b的两点间连一条边。(保证无重边与自环。)

Output

第一行,一个数,表示无向图的值。
第二行,一个数,表示无向图中所有点的值的和。

Sample Input

3 2

2

-1

0

1 2

2 3

Sample Output

2

2

HINT

数据约定

n<=500,m<=2000

样例解释

2结点的值定为0即可。

题解

考虑异或的性质,可以分位考虑。这样这个图就变成了01的权值。

于是按标号为1或者为0就可以把顶点划分成两个集合,而连通这两个集合的边的数量就是我们需要累加的边权,为了让数量最少,于是就变成了集合划分模型。

这样我们将S和所有权值为1的点连起来,容量为INF,将所有mark为0的点和T连起来,容量为INF,还没标号的点就不和源点汇点连了,他们的标号取决于做完最小割之后属于哪个集合。

接着将其他边建好,容量为1,之后就可以做最大流了,做完最大流后从S出发沿容量不为0的边走所能到达的点都是属于mark为1的这个集合的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 60010
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
    struct node {
        int to, next, flow;
    } e[N];
    int tot, st[N], dis[N], cur[N];
    void init() {
        tot = -1, memset(e, -1, sizeof e),
        memset(st, -1, sizeof st);
    }
    void add(int x, int y, int z) {
        e[++tot].to = y;
        e[tot].flow = z;
        e[tot].next = st[x];
        st[x] = tot;
//      printf("add:%d %d %d\n", x, y, z);
    }
    void add_edge(int x, int y, int z) {
        add(x, y, z), add(y, x, 0);
    }
    queue <int> que;
    int bfs(int S, int T) {
        memcpy(cur, st, sizeof st);
        memset(dis, 0, sizeof dis);
        while (!que.empty()) que.pop();
        que.push(S);
        dis[S] = 1;
        while(!que.empty()) {
            int now = que.front();
            que.pop();
            for (int i = st[now]; ~i; i = e[i].next)
                if (e[i].flow && !dis[e[i].to]) {
                    dis[e[i].to] = dis[now] + 1;
                    if (e[i].to == T) return 1;
                    que.push(e[i].to);
                }
        }
        return 0;
    }
    int finds(int now, int T, int flow) {
        if (now == T)
            return flow;
        int f;
        for (int i = cur[now]; ~i; i = e[i].next) {
            cur[now] = i;
            if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
                (f = finds(e[i].to, T, min(flow, e[i].flow)))) {
                e[i].flow -= f;
                e[i^1].flow += f;
                return f;
            }
        }
        return 0;
    }
    int dinic(int S, int T) {
        int flow = 0, x;
        while(bfs(S, T)) {
            while(x = finds(S, T, inf)) {
                flow += x;
            }
        }
        return flow;
    }
}flow;
int n, m, S, T, a[N], vis[N], val[N];
long long ans1, ans2;
struct edge {
    int x, y;
}e[N];
void work(int now) {
    flow.init();
    S = 0, T = n + 1;
    for (int i = 1; i <= m; ++i)
        flow.add(e[i].x, e[i].y, 1),
        flow.add(e[i].y, e[i].x, 1);
    for (int i = 1; i <= n; ++i)
        if (a[i] >= 0)
            if ((a[i] & (1 << now)))
                flow.add_edge(S, i, inf);
            else
                flow.add_edge(i, T, inf);
}
void dfs(int now, int k) {
    vis[now] = 1;
    val[now] |= 1 << k;
    for (int i = flow.st[now]; ~i; i = flow.e[i].next)
        if (!vis[flow.e[i].to] && flow.e[i].flow)
            dfs(flow.e[i].to, k);
}
main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 1; i <= m; ++i)
        scanf("%d%d", &e[i].x, &e[i].y);
    long long ans = 0;
    for (int i = 0; i < 31; ++i) {
        work(i);
        ans += flow.dinic(S, T) * (1ll << i);
        memset(vis, 0, sizeof vis);
        dfs(S, i);
    }
    for (int i = 1; i <= n; ++i)
        if (a[i] >= 0)
            val[i] = a[i];
    for (int i = 1; i <= m; ++i)
        ans1 += val[e[i].x] ^ val[e[i].y],
        ans2 += val[i];
    printf("%lld\n%lld\n", ans1, ans2);
}