POJ-1466 Girls and Boys (Bipartite largest independent set)

                                      Girls and Boys

In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.
Input
The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description: 

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.
Output
For each given data set, the program should write to standard output a line containing the result.
Sample Input
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) 0
Sample 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;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282010&siteId=291194637