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

我这java代码怎么会是wrong answer 啊?哪位大神帮看下啊。。。

Posted by snowing0427 at 2011-12-19 23:48:03 on Problem 1988
import java.util.Scanner;

public class Main {

	public static int[] parent =new int[30005];
	public static int[] deep=new int[30005];//the length to father
	public static int[] amount=new int[30005];
	
	public static void make_set(){
		for(int i=0;i<amount.length;i++){
			parent[i]=i;
			deep[i]=0;
			amount[i]=1;
		}
	}
	
	public static int findhead(int x){
		if(x!=parent[x]){
			int temp=parent[x];
			parent[x]=findhead(parent[x]);
			deep[x]+=deep[temp];
		}
		return parent[x];
	}
	
	public static void move_set(int x,int y){
		int px=findhead(x);
		int py=findhead(y);
		
		parent[py]=px;
		deep[py]=amount[x];
		amount[px]+=amount[py];
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin=new Scanner(System.in);	
		int line=Integer.parseInt(cin.nextLine());
		//long startTime=System.nanoTime();
		make_set();
		while(line--!=0){
			String str=cin.nextLine();
			String[] array=str.split("\\s+");
			if(array[0].equals("M")){				
					move_set(Integer.parseInt(array[1]), Integer.parseInt(array[2]));
			}
			else {
				int x=Integer.parseInt(array[1]);
				int px=findhead(x);
				System.out.println(amount[px]-deep[x]-1);
			}
		}
	}
}

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