• ZOJ1516 Uncle Tom's Inherited Land 最大匹配


    题意:有一块土地,某些地方被挖去了,现在允许以每相邻的两块进行出售,问最多能出售多少块出去。

    解法:其实相邻即曼哈顿距离为1,那么最经典的方式当然是黑白染色棋盘来进行构图了,但是这题数据量太大了,虽然知道会MLE或者TLE,不过还是写了一遍。正确的解法当然是把所有的合法的点抠出来,题目已经告诉我们这样的点不会超过50个,所以不用当心任何问题,然后就是枚举两两组合是否相邻进行构图,然后求出最大匹配出售。这个枚举过程其实也是将图进行了拆点处理,曼哈顿距离为1的组合方式确保了从一点连出去的边的另一端点内部曼哈顿距离一定不为1,所以拆点后仍可以将坐标和为奇数的点看做X部,坐标和为偶数的点看做Y部,所不同的是拆点后图中包含了两套这样互不干预的二分图,最后对匹配数除2即可。因此干脆将抽离出来的点只在坐标和为奇数与坐标和为偶数之间的点连边。这样最后的结果不用除2了。

    代码如下:

    奇偶构图
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int MaxN = 105;
    int N, M, K, LIM;
    char mp[MaxN][MaxN], G[55][55];
    int match[55];
    char vis[55];
    
    /*
    经典的黑白染色构图
    一维化坐标为奇数的点作为X部 
    一维化坐标为偶数的点作为Y部
    将上下左右相邻的点连一条边,边的含义为一个合法的出售
    这样也能够保证X部和Y部内不会出现边相连的情况
    以上做法超内存,适应于行列较小的情况 
    */
    
    struct Node {
        int x, y, odd;    
    }nd[55];
    int idx;
    
    void check(int a, int b) {
        if (abs(nd[a].x-nd[b].x)+abs(nd[a].y-nd[b].y) == 1) {
            G[a][b] = 1;
        }
    }
    
    void build() {
        idx = 1;
        memset(G, 0, sizeof (G));
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                if (!mp[i][j]) {
                    nd[idx].x = i;
                    nd[idx].y = j;
                    nd[idx].odd = (i+j)%2;
                    ++idx;
                }
            }
        }
        for (int i = 1; i < idx; ++i) {
            for (int j = 1; j < idx; ++j) {
                if (nd[i].odd && !nd[j].odd)
                    check(i, j);
            }
        }
    }
    
    bool path(int u) {
        for (int i = 1; i < idx; ++i) {
            if (!G[u][i] || vis[i]) continue;
            vis[i] = 1;
            if (!match[i] || path(match[i])) {
                match[i] = u;
                return true;
            }
        }
        return false;
    }
    
    int query() {
        int ret = 0;
        memset(match, 0, sizeof (match));
        for (int i = 1; i < idx; ++i) {
            memset(vis, 0, sizeof (vis));
            ret += path(i);
        }
        return ret;
    }
    
    int main() {
        int x, y;
        while (scanf("%d %d", &N, &M), N|M) {
            scanf("%d", &K);
            memset(mp, 0, sizeof (mp));
            for (int i = 0; i < K; ++i) {
                scanf("%d %d", &x, &y);
                mp[x][y] = 1; // 1表示该点不能够被出售
            }
            build();
            printf("%d\n", query());
        }
        return 0;    
    }
    拆点构图
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int MaxN = 105;
    int N, M, K, LIM;
    char mp[MaxN][MaxN], G[55][55];
    int match[55];
    char vis[55];
    
    /*
    经典的黑白染色构图
    一维化坐标为奇数的点作为X部 
    一维化坐标为偶数的点作为Y部
    将上下左右相邻的点连一条边,边的含义为一个合法的出售
    这样也能够保证X部和Y部内不会出现边相连的情况
    以上做法超内存,适应于行列较小的情况 
    */
    
    struct Node {
        int x, y;    
    }nd[55];
    int idx;
    
    void check(int a, int b) {
        if (abs(nd[a].x-nd[b].x)+abs(nd[a].y-nd[b].y) == 1) {
            G[a][b] = G[b][a] = 1;
        }
    }
    
    void build() {
        idx = 1;
        memset(G, 0, sizeof (G));
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                if (!mp[i][j]) {
                    nd[idx].x = i, nd[idx].y = j;
                    ++idx;
                }
            }
        }
        for (int i = 1; i < idx; ++i) {
            for (int j = i+1; j < idx; ++j) {
                check(i, j);
            }
        }
    }
    
    bool path(int u) {
        for (int i = 1; i < idx; ++i) {
            if (!G[u][i] || vis[i]) continue;
            vis[i] = 1;
            if (!match[i] || path(match[i])) {
                match[i] = u;
                return true;
            }
        }
        return false;
    }
    
    int query() {
        int ret = 0;
        memset(match, 0, sizeof (match));
        for (int i = 1; i < idx; ++i) {
            memset(vis, 0, sizeof (vis));
            ret += path(i);
        }
        return ret >> 1;
    }
    
    int main() {
        int x, y;
        while (scanf("%d %d", &N, &M), N|M) {
            scanf("%d", &K);
            memset(mp, 0, sizeof (mp));
            for (int i = 0; i < K; ++i) {
                scanf("%d %d", &x, &y);
                mp[x][y] = 1; // 1表示该点不能够被出售
            }
            build();
            printf("%d\n", query());
        }
        return 0;    
    }
    黑白染色
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int MaxN = 105;
    int N, M, K, LIM, dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
    char mp[MaxN][MaxN], G[MaxN*MaxN][MaxN*MaxN];
    int match[MaxN*MaxN];
    char vis[MaxN*MaxN];
    
    /*
    经典的黑白染色构图
    一维化坐标为奇数的点作为X部 
    一维化坐标为偶数的点作为Y部
    将上下左右相邻的点连一条边,边的含义为一个合法的出售
    这样也能够保证X部和Y部内不会出现边相连的情况 
    */
    
    bool legal(int x, int y) {
        if (x < 1 || x > N || y < 1 || y > M) return false;
        return true;    
    }
    
    void check(int a) {
        int x1 = (a-1)/M+1, y1 = (a-1)%M+1;
        if (mp[x1][y1]) return;// 如果该点不能被出售 
        int x2, y2;
        for (int i = 0; i < 4; ++i) {
            x2 = x1 + dir[i][0], y2 = y1 + dir[i][1];
            if (!mp[x2][y2]) {
                G[a][x2*M+y2] = 1;
            }
        }
    }
    
    void build() {
        LIM = N * M;
        for (int i = 1; i <= LIM; i+=2) {
            check(i);
        }
    }
    
    bool path(int u) {
        for (int i = 2; i <= LIM; i += 2) {
            if (!G[u][i] || vis[i]) continue;
            vis[i] = 1;
            if (!match[i] || path(match[i])) {
                match[i] = u;
                return true;
            }
        }
        return false;
    }
    
    int query() {
        int ret = 0;
        memset(match, 0, sizeof (match));
        for (int i = 1; i <= LIM; i+=2) {
            memset(vis, 0, sizeof (vis));
            ret += path(i);    
        }
        return ret;
    }
    
    int main() {
        int x, y;
        while (scanf("%d %d", &N, &M), N|M) {
            scanf("%d", &K);
            memset(mp, 0, sizeof (mp));
            for (int i = 0; i < K; ++i) {
                scanf("%d %d", &x, &y);
                mp[x][y] = 1; // 1表示该点不能够被出售
            }
            build();
            printf("%d\n", query());
        }
        return 0;    
    }
  • 相关阅读:
    典型案例道出“服务台”的价值
    银监会拟允许银行理财产品直接投资
    解读中国版存款保险制度:差别费率+强监管色彩
    央行牵头互联网金融“顶层设计”引业内关注
    央行降息 是农村互联网金融的救命稻草?
    历史上最伟大的 12 位程序员
    年关将至业内警示P2P跑路风险
    央行启动我国征信自律组织研究课题
    windows下开启redis拓展
    php使用curl新增微信临时素材(上传图片)
  • 原文地址:https://www.cnblogs.com/Lyush/p/3003495.html
Copyright © 2020-2023  润新知