poj 3692 Kindergarten
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6956 | Accepted: 3436 |
Description
In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids to play a game, which need that all players know each other. You are to help to find maximum number of kids the teacher can pick.
Input
The input consists of multiple test cases. Each test case starts with a line containing three integers
G, B (1 ≤ G, B ≤ 200) and M (0 ≤ M ≤ G × B), which is the number of girls, the number of boys and
the number of pairs of girl and boy who know each other, respectively.
Each of the following M lines contains two integers X
The girls are numbered from 1 to G and the boys are numbered from 1 to B.
The last test case is followed by a line containing three zeros.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the maximum number of kids the teacher can pick.
Sample Input
2 3 3 1 1 1 2 2 3 2 3 5 1 1 1 2 2 1 2 2 2 3 0 0 0
Sample Output
Case 1: 3 Case 2: 4
翻譯:小朋友手拉手做遊戲,現在老師希望從小朋友當中挑出一個集合,集合中的小朋友全都認識,那麽這個集合最大是多少。
思路:顯然是個最大獨立集問題,只是男女小朋友關系圖的補圖的最大獨立集,其補圖中兩個人相互連線意味著相互不認識,從圖中挑出來的最大獨立集就是題設要求的。
AC代碼:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<queue> #include<set> #include<vector> #include<cstring> #include<string> using namespace std; #define INF 0x3f3f3f3f const int N_MAX = 500; int V;//點的個數 vector<int>G[N_MAX]; int match[N_MAX]; bool used[N_MAX]; void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } bool dfs(int v) { used[v] = true; for (int i = 0; i < G[v].size(); i++) { int u = G[v][i], w = match[u]; if (w < 0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; } } return false; } int bipartite_matching() { int res = 0; memset(match, -1, sizeof(match)); for (int v = 0; v < V; v++) { if (match[v] < 0) { memset(used, 0, sizeof(used)); if (dfs(v)) res++; } } return res; } bool vis[N_MAX][N_MAX]; int g, B, M; int main() { int Case = 0; while (scanf("%d%d%d",&g,&B,&M)&&g) { Case++; //0~g-1:girl //g~g+B-1:boy V = g + B; memset(vis,0,sizeof(vis)); for (int i = 0; i < M; i++) { int a, b; scanf("%d%d",&a,&b); a--, b--; vis[a][g + b]=true; } for (int i=0; i<g;i++) { for (int j = g; j < V;j++) { if (!vis[i][j]) {//建立原圖的補圖 add_edge(i,j); } } } printf("Case %d: %d\n",Case,V-bipartite_matching()); for (int i = 0; i < V;i++) { G[i].clear(); } } return 0; }
poj 3692 Kindergarten