Girls and Boys
the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)
The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.
7 0: (3) 4 5 6 1: (2) 4 6 2: (0) 3: (0) 4: (2) 0 1 5: (1) 0 6: (2) 0 1 3 0: (2) 1 2 1: (1) 0 2: (1) 0Sample Output
5 2
The general meaning of the question is: There are n students, each student is related to some people, and find the group of people who have the most relationship with each other.
Because the title only requires a set.
Max Independent Sets = Points - Max Matches. Here the maximum number of matches needs to be divided by 2 (
After seeing it, you can find that this is a very obvious problem of the largest independent set, which can be converted into a bipartite graph, or the most classic split point construction, and then according to the theorem, the largest independent set = the number of vertices - the smallest point coverage number. And for this question, we can find that the romantic relationship is mutual.
In our construction map, it is reasonable to say that one side is a male point, and the other side is a female point, but the question of gender is not mentioned in the title.
You can only split each point into two points, one as a male point and one as a female point, and then connect the sides. Since the relationship is mutual, this creates a duplication of edges. That is to say, the edge set is twice as large, which causes the maximum matching to be doubled.
Then, the maximum independent set = the number of vertices - the maximum matching / 2, so the final answer is ready to come out. ) in brackets to understand: here click here
code show as below:
/* POJ 1466 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<set>
#define manx maxn
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 500 +10;
using namespace std;
int Laxt[maxn],Next[maxn*200],To[maxn*200],vis[maxn],dis[maxn];
int cnt;
void init()
{
mem(Laxt,0);
mem(vis,0);
mem(dis,-1);
cnt=0;
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
bool dfs(int u)
{
int i;
for(i=Laxt[u]; i; i=Next[i])
{
int v=To[i];
if(!vis[v])
{
vis[v]=1;
if(dis[v]==-1||dfs(dis[v]))
{
dis[v]=u;
return true;
}
}
}
return false;
}
int main()
{
int n,m,u,v,ans;
while(~scanf("%d",&n))
{
init();
int ans=0;
for(int j=0; j<n; j++)
{
scanf("%d: (%d)",&u,&m);
while(m--)
{
scanf("%d",&v);
add(u,v);
//add(v,u);
}
}
for(int i=0; i<n; i++)
{
mem(vis,0);//这里wa了一发 注意了!!!
if(dfs(i)) ans++;
}
cout<<n-(ans/2)<<endl;
}
}