Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

谢谢各位帮忙,可是我的程序还是WA,那位请给看一看,不胜感激!

Posted by gongml at 2005-10-16 22:32:22 on Problem 1466
import java.io.*;
import java.util.*;

class GirlsAndBoys {
	static int num_student, num_match;
	static int[][] relation;
	static int[] gender, mate;
	static boolean[] visited;

	static boolean loadRelationship(BufferedReader input) throws IOException {
		String line = input.readLine();
		if ( line==null || line.length()==0 )
			return false;
		num_student = Integer.parseInt(line);
		relation = new int[num_student][];
		for ( int i=0; i<num_student; i++ ) { // load into adjacency list
			StringTokenizer tokens = new StringTokenizer(input.readLine(), " :()");
			int id = Integer.parseInt(tokens.nextToken());
			int num = Integer.parseInt(tokens.nextToken());
			relation[id] = new int[num];
			for ( int j=0; j<num; j++ )
				relation[id][j] = Integer.parseInt(tokens.nextToken());
		}
		return true;
	}

	static void labelGender() {
		LinkedList<Integer> queue = new LinkedList<Integer>();
		for ( int s=0; s<num_student; s++ )
			if ( gender[s] == 0 ) { // if not assigned
				gender[s] = 1;	// set to boy
				queue.add(new Integer(s));
				while ( ! queue.isEmpty() ) {
					int t = queue.remove(0).intValue();
					for ( int i=0; i<relation[t].length; i++ )
						if ( gender[relation[t][i]] == 0 ) {
							gender[relation[t][i]] = - gender[t];
							queue.add(new Integer(relation[t][i]));
						}
				}
			}
	}

	static boolean findPath(int start) {
		for ( int i=0; i<relation[start].length; i++ )
			if ( ! visited[relation[start][i]] ) {
				visited[relation[start][i]] = true;
				int next = mate[relation[start][i]];
				if ( next==-1 || findPath(next) ) {
					mate[relation[start][i]] = start;
					mate[start] = relation[start][i];
					return true;
				}
			}
		return false;
	}

	static void hungarianMatch() {
		for ( int s=0; s<num_student; s++ )
			if ( gender[s]==-1 && mate[s]==-1 ) { // only start from unmatched girl
				Arrays.fill(visited, false);
				if ( findPath(s) )
					num_match ++;
			}
	}

	public static void main (String[] args) throws IOException {
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		while ( loadRelationship(input) ) {
			num_match = 0;
			gender = new int[num_student];
			visited = new boolean[num_student];
			mate = new int[num_student];
			Arrays.fill(mate, -1);
			labelGender();
			hungarianMatch();
			System.out.println(num_student-num_match);
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator