Poj_2771 Guardian of Decency -最大团

题目: 找出不会恋爱的人的最大独立集。

分析:一眼看下去以为是最大独立集,但是题目给定的边是不会恋爱的边,所以想想会发现其实求最大团。

吐槽:虽然看到500个点,但我没用邻接表,因为求补图的话边会很多,是个稠密图。

/************************************************
Author        :DarkTong
Created Time  :2016/8/1 10:14:42
File Name     :Poj_2771.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <string>
#include <iostream>
//#define abs(a) a>=0?a:-a
using namespace std;
const int maxn = 500 + 11;
struct Node
{
    int h;
    string se, mu, sp;
}peo[maxn];
//vector<int> w[maxn];
int w[maxn][maxn];
int n, m, Left[maxn];
bool used[maxn];
bool match(int i)
{
    for(int j=1;j<=m;++j) if(w[i][j]&&!used[j])
    {
    //    int v = w[i][j];
        used[j] = true;
        if(!Left[j]||match(Left[j]))
        {
            Left[j] = i;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, 0, sizeof(Left));
    for(int i=1;i<=n;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}

int main()
{
    int T, cas=1;
    scanf("%d", &T);
    while(T--)
    {
    //    for(int i=0;i<maxn;++i) w[i].clear();
        memset(w, 0x1, sizeof(w));

        scanf("%d", &n);
        m=n;

        for(int i=1;i<=n;++i) cin>>peo[i].h>>peo[i].se>>peo[i].mu>>peo[i].sp;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            {
                if(abs((peo[i].h-peo[j].h))>40||peo[i].se==peo[j].se||peo[i].mu!=peo[j].mu||peo[i].sp==peo[j].sp)
                    w[i][j]=0;
                if(i==j) w[i][j]=0;
            //        w[i].push_back(j);
            }
//        cout<<"h:"<<hungary()<<endl;
        printf("%d\n", (2*n-hungary())>>1);

    }

    return 0;
}

时间: 2024-10-15 06:50:57

Poj_2771 Guardian of Decency -最大团的相关文章

poj——2771 Guardian of Decency

poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   Accepted: 2458 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he i

poj 2771 Guardian of Decency

Guardian of Decency http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K       Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that som

UVA 12083 - Guardian of Decency(二分图最大匹配)

UVA 12083 - Guardian of Decency 题目链接 题意:给定一些男女,满足身高差不大于40,喜欢同一种音乐,不喜欢同一种体育项目,并且性别不同,就可能发生关系,现在老师要带一些男女出去玩,要求不能有一对发生关系,问最多能带多少人 思路:分男女,把会发生关系的连边,然后做最大匹配,最后n-最大匹配就是最多能带的人 代码: #include <cstdio> #include <cstring> #include <string> #include

uva 12083 Guardian of Decency (二分图匹配)

uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this poss

poj 2771 Guardian of Decency【最大点独立集】

K - Guardian of Decency Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2771 Appoint description:  System Crawler  (2014-08-17) Description Frank N. Stein is a very conservative high-school teac

poj 2771 Guardian of Decency 解题报告

题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距  > 40cm 2.相同性别 3.喜欢的音乐种类  不同 4.有共同喜爱的 运动 只要满足其中这4个条件中的一个(当然越多越好啦),就可以将他们编为一组啦(一组两个人),求能被编为一组的最多组数. 这题实质上求的是二分图的最大独立集.  最大独立集 = 顶点数 - 最大匹配数 可以这样转化:两个人至少

POJ 2771 Guardian of Decency (二分图最大点独立集)

Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 2555 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

UVAlive3415 Guardian of Decency(最大独立集)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34831 [思路] 二分图的最大独立集. 即在二分图中选取最多的点,使点与点之间不相邻. 最大独立集为最小覆盖集的补集. 男者X结点,女者Y结点,连边(Xi,Yj)当且仅当两者4个条件都不满足. [代码] 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include