閱讀681 返回首頁    go 技術社區[雲棲]


【二分匹配】HDU1068-Girls and Boys

說說什麼是最大獨立集


在二分圖中,
獨立集指的是兩兩之間沒有邊的頂點的集合,
頂點最多的獨立集成為最大獨立集。
二分圖的最大獨立集=節點數-最大匹配數


例子:
1
2 - 1'
3 - 2'
4 - 3'   
    4'
最大獨立集=8-3=5.
最大獨立集是1,2,3,4,4'

 

例題:
Girls and Boys
Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7486    Accepted Submission(s): 3419


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 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, 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
 

 

 

 

依舊是二分匹配,用匈牙利算法。。。。
等等,好像沒有所謂的集合A和集合B了,好像所有人都在相互喜歡= =
就是說0 喜歡 4 5 6,4 5 6也喜歡 0。。。。也就是說這裏的數字是
不分男女的。。。僅僅是數字喜歡數字(暫且這麼看吧)
這就有影響了,假設0選擇了4做匹配,那麼4隻能選0做匹配了。。。。
這就把原來的二分圖匹配變成了“一分圖”匹配。。。(自己起得名= =)
這樣,按原來匈牙利的做法,求出來是男數字與女數字之間的匹配,
所以隻要將匹配的結果除以2,就是所謂的“一分圖”匹配數量,
就可以用剛剛的公式:二分圖的最大獨立集=節點數-最大匹配數


AC代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int line[1100][1100],used[1100],boy[1100];
int n;//全局變量和局部變量不要使用相同名稱的變量,否則導致結果出錯
int Find(int x)
{
    int i;
    for(i=0;i<n;i++)
    {
       if(line[x][i]==1&&used[i]==0)
       { 
           used[i]=1;
           if(boy[i]==0||Find(boy[i]))
           {
              boy[i]=x;
              return 1;
           }
       }                
    }
    return 0;
}
int main()
{
    int i,j,x,y,z,sum;
    while(scanf("%d",&n)!=EOF)
    {
        memset(line,0,sizeof(line));
        memset(boy,0,sizeof(boy));
        for(i=0;i<n;i++)
        {
           scanf("%d: (%d)",&x,&y);
           while(y--)
           {
              scanf("%d",&z); 
              line[x][z]=1;//x跟z曖昧 
              line[z][x]=1;
           }
        }
        /*for(i=0;i<n;i++)//檢測一下誰和誰曖昧..... 
        {
           for(j=0;j<n;j++)
           if(line[i][j]==1)
           printf("%d LOVE %d\n",i,j);
        }*/
        sum=0;
        for(i=0;i<n;i++)
        {
           memset(used,0,sizeof(used));
           if(Find(i)) sum++;//找到可以結合的就記錄 
        }
        //因為此題是單集合的,所以不能夠按照平常的求最大獨立集
        //的方法,要除以2(因為實際上是一分圖而不是二分圖....) 
        printf("%d\n",n-sum/2);
    }
    return 0;
} 

最後更新:2017-04-03 05:39:54

  上一篇:go poj 2190 ISBN
  下一篇:go 【哈夫曼編碼】HDU1053-Entropy