Guardian of Decency hoj poj 二分图匹配 不错的题

136 篇文章 0 订阅
48 篇文章 0 订阅

/*这个题的原意是求不能成为夫妇的人的最大数量。
但是这个只需要满足四个条件中的一个就可以连一条边,这样匹配后产生的课匹配的边数量过多,
就超时了。
可以反过来想,求不满足题意,即能成为夫妇的点数,再用总数n-此时的最大匹配数m。
因为是拆点建图,所以m/=2.最大二分匹配就转化成了最小覆盖。
另外,对500上限的点数,用邻接矩阵是0.59s。
而用vector的临接表是0.21.时间效率很客观,还没有试过池子法的写法,应该速度会更快。*/
#include <stdio.h>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
const int maxn=501;
int n;
bool vis[maxn];
int match[maxn];
struct point
{
    int h;
    char c;
    char m[30];
    char s[30];
} p[maxn];


bool find(int x)
{
    for(int i=0; i<v[x].size(); i++)
    {
        int u=v[x][i];
        if(!vis[u])
        {
            vis[u]=true;
            if(find(match[u])||match[u]==-1)
            {
                match[u]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<maxn;i++)
        v[i].clear();
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d %c %s %s",&p[i].h,&p[i].c,p[i].m,p[i].s);
        memset(match,-1,sizeof(match));
        for(int i=0; i<n; i++)
            for(int j=i+1; j<n; j++)
            {
                if(p[i].h-p[j].h>40||p[j].h-p[i].h>40)
                    continue;
                if(p[i].c==p[j].c)
                    continue;
                if(strcmp(p[i].m,p[j].m)!=0)
                    continue;
                if(strcmp(p[i].s,p[j].s)==0)
                    continue;
                v[i].push_back(j);
                v[j].push_back(i);
            }
        int count=0;
        for(int i=0; i<n; i++)
        {
            memset(vis,false,sizeof(vis));
            if(find(i)) count++;
        }
        printf("%d\n",n-count/2);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值