二分图(最大匹配)

来源:互联网 发布:ubuntu新立得安装命令 编辑:程序博客网 时间:2024/04/18 12:50

HDU 1083:

Problem 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

Your program should read sets of data from a text file. The first line of the input file 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'll 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.

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.

An example of program input and output:
 

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

Sample Output
YESNO
 

Source
Southeastern Europe 2000
 

求最大匹配的裸题。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;int mp[330][330];int used[330],link[330];int t,p,n;bool dfs(int x){    REPF(i,1,n)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}int main(){    int x,y;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&p,&n);        CLEAR(mp,0);        REPF(i,1,p)        {            scanf("%d",&x);            while(x--)            {                scanf("%d",&y);                mp[i][y]=1;            }        }        int ans=0;        CLEAR(link,-1);        REPF(i,1,p)        {            CLEAR(used,0);            if(dfs(i))  ans++;        }        puts(ans==p?"YES":"NO");    }    return 0;}
HDU:2819

Problem Description
Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1?
 

Input
There are several test cases in the input. The first line of each test case is an integer N (1 <= N <= 100). Then N lines follow, each contains N numbers (0 or 1), separating by space, indicating the N*N matrix.
 

Output
For each test case, the first line contain the number of swaps M. Then M lines follow, whose format is “R a b” or “C a b”, indicating swapping the row a and row b, or swapping the column a and column b. (1 <= a, b <= N). Any correct answer will be accepted, but M should be more than 1000.

If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”. 
 

Sample Input
20 11 021 01 0
 

Sample Output
1R 1 2-1
 

Source
2009 Multi-University Training Contest 1 - Host by TJU
 
只要保证最大匹配为n即每行都有不同的列和其匹配就可以转化为对角阵。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=110;int mp[maxn][maxn];int used[maxn],link[maxn];pil ans[maxn];int n;bool dfs(int x){    REPF(i,1,n)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}int main(){    int x;    while(~scanf("%d",&n))    {        CLEAR(mp,0);        REPF(i,1,n)          REPF(j,1,n)          {              scanf("%d",&x);              if(x) mp[i][j]=1;          }        int res=0;        CLEAR(link,-1);        REPF(i,1,n)        {            CLEAR(used,0);            if(dfs(i))  res++;        }        if(res!=n)        {            puts("-1");            continue;        }        int j,cnt=0;        REPF(i,1,n)        {            for(j=i;j<=n;j++)                if(link[j]==i)  break;            if(j!=i)            {                ans[cnt].first=i;                ans[cnt++].second=j;                swap(link[i],link[j]);            }        }        printf("%d\n",cnt);        for(int i=0;i<cnt;i++)            printf("C %d %d\n",ans[i].first,ans[i].second);    }    return 0;}



0 0
原创粉丝点击