la 3415, 12083 - Guardian of Decency 最大匹配,独立集,覆盖集
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 possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:
- Their height differs by more than 40 cm.
- They are of the same sex.
- Their preferred music style is different.
- Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).
So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information.
Input
The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items:
- an integer h giving the height in cm;
- a character 'F' for female or 'M' for male;
- a string describing the preferred music style;
- a string with the name of the favourite sport.
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
Output
For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.
Sample Input
2 4 35 M classicism programming 0 M baroque skiing 43 M baroque chess 30 F baroque soccer 8 27 M romance programming 194 F baroque programming 67 M baroque ping-pong 51 M classicism programming 80 M classicism Paintball 35 M baroque ping-pong 39 F romance ping-pong 110 M romance Paintball
Sample Output
3 7
根据性别建立二分图,然后用等式:
最大独立集基数 = 点数 - 最小覆盖点数
抄下《训练指南》P356的简单证明:
覆盖集:对于每条边,至少有一个点要被选中
独立集:对于每条边,至少有一个点不被选中
所以给定其一,二者一一对应,从而。。。
然后,最小覆盖点数 = 最大匹配数,该式
证明参考网上(貌似来自LRJ) :http://blog.csdn.net/l04205613/article/details/6260863
1.
①M个是足够的,只需要让他们覆盖最大的匹配的M条边,则其他边一定被覆盖
(如果有边e不被覆盖,把e加入后得到一个更大的匹配)
②M个是必需的,仅考虑形成最大匹配的M条边,由于两两无公共点,因此至少
需要M个点才能把它们覆盖;
照敲LRJ的。。。AC代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<cstdlib> #include<algorithm> using namespace std; #define LL long long #define ULL unsigned long long #define UINT unsigned int #define MAX_INT 0x7fffffff #define MAX_LL 0x7fffffffffffffff #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) #define MAXN 555 int w[MAXN][MAXN]; int g[MAXN][MAXN]; int n; int x, y; struct pep{ int h; string msc, spt; pep(int h0, string s1, string s2): h(h0), msc(s1), spt(s2) {} }; inline bool ok(pep u, pep v){ return (abs(u.h-v.h)<=40 && u.msc==v.msc && u.spt!=v.spt); } int lft[MAXN]; bool t[MAXN]; bool match(int u){ for(int v=0; v<y; v++) if(g[u][v] && !t[v]){ t[v]=true; if(lft[v]==-1 || match(lft[v])){ lft[v]=u; return true; } } return false; } int solve(){ int i,j, ans=0; memset(lft, -1, sizeof(lft)); for(i=0; i<x; i++){ memset(t, 0, sizeof(t)); if(match(i)) ans++; } return x+y-ans; } int main(){ //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); int T; cin>>T; while(T--){ cin>>n; int i,j; int h; string sex,msc,spt; vector<pep> boy, girl; for(i=0; i<n; i++){ cin>>h>>sex>>msc>>spt; if(sex[0]=='M') boy.push_back(pep(h, msc, spt)); else girl.push_back(pep(h, msc, spt)); } x=boy.size(), y=girl.size(); memset(g, 0, sizeof(g)); for(i=0; i<x; i++) for(j=0; j<y; j++) if(ok(boy[i], girl[j])) g[i][j]=1; cout<<solve()<<endl; } return 0; }