HDU-1507-Uncle Tom's Inherited Land*

链接:https://vjudge.net/problem/HDU-1507

题意:

Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.) 

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle's property. Furthermore, ponds are not salable property. 

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks). 

思路:

根据矩阵x,y的和建立二分图,同时不处理池塘。

输出路径根路据Link数组,找到每个边对应左端点的编号,同时结构体排序,使编号小的在前,再遍历数组查找即可。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 1e4+10;
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

struct Node
{
    int x, y;
    int pos;
    Node(int x, int y, int pos):x(x), y(y), pos(pos){}
    bool operator < (const Node & that)const
    {
        return this->pos < that.pos;
    }
};

vector<int> G[MAXN];
int Dis[200][200];
int Link[MAXN], Vis[MAXN];
int n, m, k;
int mid;
int cnt1, cnt2;

void Init()
{
    for (int i = 1;i <= n;i++)
        G[i].clear();
}

bool Dfs(int x)
{
    for (int i = 0;i < G[x].size();i++)
    {
        int node = G[x][i];
        if (Vis[node] == 0)
        {
            Vis[node] = 1;
            if (Link[node] == -1 || Dfs(Link[node]))
            {
                Link[node] = x;
                return true;
            }
        }
    }
    return false;
}

int Solve()
{
    memset(Link, -1, sizeof(Link));
    int cnt = 0;
    for (int i = 1;i <= cnt1;i++)
    {
        memset(Vis, 0, sizeof(Vis));
        if (Dfs(i))
            cnt++;
    }
    return cnt;
}

int main()
{
    while (~scanf("%d%d", &n, &m) && n)
    {
        cnt1 = cnt2 = 0;
        memset(Dis, 0, sizeof(Dis));
        cin >> k;
        int x, y;
        for (int i = 1;i <= k;i++)
        {
            cin >> x >> y;
            Dis[x][y] = -1;
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if (Dis[i][j] != -1)
                {
                    if ((i+j)%2 == 1)
                        Dis[i][j] = ++cnt1;
                    else
                        Dis[i][j] = ++cnt2;
                }
            }
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 1 && Dis[i][j] != -1)
                {
                    for (int k = 0;k < 4;k++)
                    {
                        int tx = i+Next[k][0];
                        int ty = j+Next[k][1];
                        if (tx < 1 || tx > n || ty < 1 || ty > m)
                            continue;
                        if (Dis[tx][ty] == -1)
                            continue;
                        G[Dis[i][j]].push_back(Dis[tx][ty]);
                    }
                }
            }
        }
        int res = Solve();
        set<Node> kv;
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 0 && Dis[i][j] != -1 && Link[Dis[i][j]] != -1)
                {
                    kv.emplace(i, j, Link[Dis[i][j]]);
                }
            }
        }
        cout << res << endl;
        auto it = kv.begin();
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 1 && Dis[i][j] == it->pos)
                {
                    printf("(%d,%d)--(%d,%d)\n", i, j, it->x, it->y);
                    ++it;
                }
            }
        }
        for (int i = 1;i <= cnt1;i++)
            G[i].clear();
    }

    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/10871591.html