首页 > 专题系列 > Java解POJ > POJ 3692 Kindergarten [解题报告] Java
2013
11-13

POJ 3692 Kindergarten [解题报告] Java

Kindergarten

问题描述 :

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.

输入:

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 ≤ MG × 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 and Y (1 ≤ X≤ G,1 ≤ Y ≤ B), which indicates that girl X and boy Y know each other.
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.

输出:

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.

样例输入:

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

样例输出:

Case 1: 3
Case 2: 4

解题代码:

//* @author: Yeming Hu"cslittleye@gmail.com"
import java.util.*;

public class Main
{
    public static int b;
    public static int g;
    public static int m;
    public static boolean[][] graph;
    public static boolean[] checked;
    public static int[] link;

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
	graph = new boolean[200][200];
	checked = new boolean[200];
	link = new int[200];
	int t = 0;

	while(true)
	{
	    b = sc.nextInt();
	    g = sc.nextInt();
	    m = sc.nextInt();

	    if(b == 0 && g == 0 && m == 0)
	    {
	        break;
	    }

	    Arrays.fill(checked,false);
	    Arrays.fill(link,-1);

	    for(int i = 0; i < b; i++)
	    {
	        Arrays.fill(graph[i],true);
	    }
	    for(int i = 0; i < m; i++)
	    {
	        int a = sc.nextInt();
		int b = sc.nextInt();
		graph[a-1][b-1] = false;
	    }
	    int ans = 0;
	    for(int i = 0; i < b; i++)
	    {
	        Arrays.fill(checked,false);
		if(find(i))
		{
		    ans++;
		}
	    }
	    System.out.println("Case " + (++t) + ": " + (g+b-ans));
	}
    }

    public static boolean find(int a)
    {
        for(int i = 0; i < g; i++)
	{
	    if(graph[a][i] && !checked[i])
	    {
		checked[i] = true;
		if(link[i] == -1 || find(link[i]))
		{
		    link[i] = a;
		    return true;
		}
	    }
	}
	return false;
    }
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  3. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?