程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 杭電OJ——1068 Girls and Boys(二分圖)

杭電OJ——1068 Girls and Boys(二分圖)

編輯:C++入門知識

Girls and Boys

Problem Description
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.

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 studentswww.2cto.com
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, for n subjects.
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
 

Source
Southeastern Europe 2000
 

Recommend
JGShining
 
方法:最大獨立集、最大匹配

思想:

最大獨立集指的是兩兩之間沒有邊的頂點的集合,頂點最多的獨立集成
為最大獨立集。二分圖的最大獨立集=節點數-(減號)最大匹配數。
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"。

由於本題是要找出最大的沒有關系的集合,即最大獨立集。而求最大獨立集重點在於求最大匹配數,
本題中給出的是同學之間的親密關系,並沒有指出哪些是男哪些是女,所以求出的最大匹配數
要除以2才是真正的匹配數。
 
發代碼:
[cpp] 
//關於題目意思的理解:二分圖的匹配問題,題目給出了同學之間的親密關系,沒有指出是男是女 
//匈牙利算法!又遇到過這種算法 
 
//先敲一段匈牙利算法! 
#include<iostream> 
using namespace std; 
const int Max=500; 
int num; 
 
int g[Max][Max]; 
int linker[Max]; 
bool used[Max]; 
 
bool dfs(int u) 

    int v; 
    for(v=0;v<num;v++) 
    { 
        if(g[u][v] && !used[v]) 
        { 
            used[v]=true; 
            if(linker[v]==-1 || dfs(linker[v])) 
            { 
                linker[v]=u; 
                return true; 
            } 
        } 
    } 
    return false; 

 
int hungary() 

    int res=0; 
    int u; 
    memset(linker,-1,sizeof(linker)); 
 
    for(u=0;u<num;u++) 
    { 
        memset(used,0,sizeof(used)); 
        if(dfs(u)) res++; 
    } 
    return res; 

 
 
int main() 

    int a,b,c; 
    int cases; 
    while(scanf("%d",&cases)!=EOF) 
    { 
        num=cases; 
        int m; 
        memset(g,0,sizeof(g)); 
         while(cases--)   
        {     
           scanf("%d: (%d)",&a,&b);          
           for(int i=0;i<b;i++)              
           {      
               scanf("%d",&c);   
               g[a][c]=1;   
           }   
        }   
       printf("%d\n",num-hungary()/2);   
    } 
    return 0; 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved