hdu_1083_經典二分圖

Courses

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7487    Accepted Submission(s): 3664


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
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
 

Sample Output
YES

NO

經典的二分圖 可以直接上模板,關鍵點要知道二分圖分成左右兩分,dfs每次去看有無可以增廣的路徑

#include
#include
#include
#include
using namespace std;
int st[3005],a[1005][1005],match[1000],visit[5000];
int p,n;
bool dfs(int from){//會影響到自己那個點所以許要加上課程號開始 
	for(int i=p+1;i<=n+p;i++){
		if(a[from][i]&&!visit[i]){
			visit[i]=1;
			 if(match[i]==-1||dfs(match[i])){//找到一個未匹配點或者和dfs(和他匹配的那個點) 
			 //第一次進來,假設用左邊匹配右邊, 他的右邊第一次是-1則是無匹配點,
			 //如果是匹配點了則去dfs(現在和他匹配的那個點)從他那裏開始繼續搜索有沒有無匹配點,如果他找到了那麼可以更新一條邊, 回溯上來的時候
			 //match【】匹配的內容就更新好了,一次相當於+1個邊集匹配 
					match[from]=i;
					match[i]=from;//match[i]=from 也是可以的 這樣子初始化人的時候就不用p+1了。即只改變二分圖的右邊的匹配鏈接 
					return true;
				}
		}
	}
	return false;
}
void init(){
	memset(a,0,sizeof(a));
	memset(st,0,sizeof(st));
	memset(match,-1,sizeof(match));
}
int main(){
	clock_t start_time=clock();
	int t;
	cin>>t;
	while(t--){
		scanf("%d%d",&p,&n);
		init();
		for(int i=1;i<=p;i++){
			int x ;
		scanf("%d",&x);
			for(int j=1;j<=x;j++){
				int y;
				scanf("%d",&y);//爲了防止match的時候課程數 和 人 他們的編號是一樣的所以把人員從課程總數p開始+
				//左邊是課程 右邊是人數 
				a[i][y+p]=1;//表示第i門課與第y個學生 有一條邊 
			} 
		}
		int ans=0;
		for(int i=1;i<=p;i++){
				memset(visit,0,sizeof(visit));
				if(dfs(i)){
					ans++;
				}
		}
		if(ans==p)printf("YES\n");
		else printf("NO\n"); 
	}
	clock_t end_time=clock();
cout<< "Running time is: "<(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章