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<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 const int maxn =  1000+10;
 9 const int maxl = 101;
10
11 bool T[maxn];
12 int lky[maxn];
13 vector<int> G[maxn];
14
15 bool match(int u) {
16     for(int i=0;i<G[u].size();i++) {
17         int v=G[u][i];
18         if(!T[v]) {
19             T[v]=1;
20             if(!lky[v] || match(lky[v])) {
21                 lky[v]=u;
22                 return true;
23             }
24         }
25     }
26     return false;
27 }
28
29 int n;
30 char sex[maxn][5],mus[maxn][maxl],pe[maxn][maxl];
31 int h[maxn];
32
33 int main() {
34     int k;
35     scanf("%d",&k);
36     while(k--) {
37         scanf("%d",&n);
38         for(int i=1;i<=n;i++) G[i].clear();
39         for(int i=1;i<=n;i++) {
40             scanf("%d%s%s%s",&h[i],sex[i],mus[i],pe[i]);
41                 for(int j=1;j<i;j++)
42                     if(sex[j][0]!=sex[i][0] && abs(h[i]-h[j])<=40 && strcmp(mus[i],mus[j])==0 && strcmp(pe[i],pe[j])!=0)
43                         if(sex[i][0]==‘M‘) G[i].push_back(j); else G[j].push_back(i);
44         }
45         memset(lky,0,sizeof(lky));
46         int ans=0;
47         for(int i=1;i<=n;i++) {
48             memset(T,0,sizeof(T));
49             if(match(i)) ans++;
50         }
51         printf("%d\n",n-ans);
52     }
53     return 0;
54 }
时间: 2024-08-26 22:59:58

UVAlive3415 Guardian of Decency(最大独立集)的相关文章

UVALive3415 Guardian of Decency —— 最大独立集

题目链接:https://vjudge.net/problem/UVALive-3415 题解: 代码如下: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <se

poj 2771 Guardian of Decency 解题报告

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

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 (二分图最大点独立集)

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

POJ2771 Guardian of Decency

Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5513   Accepted: 2319 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