二分圖(bfs+匈牙利演算法) POJ 1469 course

來源:互聯網
上載者:User

COURSES
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8076   Accepted: 3208

Description

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

  • every student in the committee represents a different course (a student can represent a course if he/she visits that course)
  • each course has a representative in the committee

Input

Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format:

P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
...
CountP StudentP 1 StudentP 2 ... StudentP CountP

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses �from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input data are correct.

Output

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

Sample Input

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO
/*題目大意:將vy個工作分配給vx個學生,求能不能每個同學都有分配到工作。題目解答:典型的二分圖,只要比較最大匹配數目等不等與學生數九可以了.代碼技巧:map[x][y]學生x能否勝任工作y,可以為1,不可以為0.match[y]=x,X集合中和工作y匹配的的學生編號.*/Source CodeProblem: 1469   User: wawadimu Memory: 204K   Time: 438MS Language: C++   Result: Accepted Source Code #include<iostream>#include<string>using namespace std;//記錄與mathch[y]匹配的x數值int match[305];bool vis[305];//map[i][j]==1表示元素i到元素j有一條邊bool map[105][305];//x集合和y集合的頂點數int vx,vy;//深度優先遍曆,從元素p出發尋找增廣路bool dfs(int x){    int t,y;    for(y=1;y<=vy;y++)    {//        if(vis[vx]) continue;        if(!vis[y] && map[x][y])        {            vis[y]=true;//            t=match[y];//            match[y]=x;//            if(t==0 || dfs(t)) return true;//            match[y]=t;//增廣路不成功,還原成原來匹配            if(match[y]==0 || dfs(match[y]))            {                match[y]=x;                return true;            }        }    }    return false;}int main(){    //freopen("1469.txt","r",stdin);    int i,j,k,n,Case,ans,cnt;    int m;    scanf("%d",&Case);    while(Case--)    {        scanf("%d%d",&vx,&vy);        memset(map,0,sizeof(map));        for(i=1;i<=vx;i++)        {            scanf("%d",&cnt);            for(j=1;j<=cnt;j++)            {                scanf("%d",&k);                map[i][k]=1;            }        }        ans=0;        memset(match,0,sizeof(match));        for(i=1;i<=vx;i++)        {            memset(vis,0,sizeof(vis));            //深度優先搜尋尋找增廣路,找到一條增廣路,匹配+1            if(dfs(i))ans++;//            for(int l=1;l<=vy;l++)//            printf("%d   ",match[l]);        }        if(ans==vx)        printf("YES/n");        else        printf("NO/n");    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.