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

神啊!!!!谁能告诉我为什么会Runtime Error

Posted by blackchaoxu at 2007-01-30 12:40:44 on Problem 3107
import java.util.*;

public class Main {
	static int n;
	static ArrayList<Integer>[] edge;
	
	static int[] maxSon,sonNum;
	
	static void readIn() {
		Scanner cin = new Scanner(System.in);
		n = cin.nextInt();
		edge = new ArrayList[n+1];
		for (int i = 1; i <= n; i++)
			edge[i] = new ArrayList<Integer>();
		for (int i = 0; i < n-1; i++) {
			int u,v;
			u = cin.nextInt();
			v = cin.nextInt();
			edge[u].add(v);
			edge[v].add(u);
		}
	}
	
	static void makeTree(int p, int f) {
		for (int i = 0; i < edge[p].size(); i++) {
			int v = edge[p].get(i);
			if (v != f) {
				makeTree(v, p);
				sonNum[p] += sonNum[v];
				if (maxSon[p] < sonNum[v])
					maxSon[p] = sonNum[v];
			}
			sonNum[p]++;
		}
	}
	
	public static void main(String[] args) {
		readIn();
		
		maxSon = new int[n+1];
		sonNum = new int[n+1];
		makeTree(1, 0);
		
		ArrayList<Integer> ans = new ArrayList<Integer>();
		int maxComp = n;
		for (int i = 1; i <= n; i++) {
			if (maxComp > Math.max(sonNum[1]-sonNum[i], maxSon[i])) {
				maxComp = Math.max(sonNum[1]-sonNum[i], maxSon[i]);
				ans.clear();
				ans.add(i);
			}else if (maxComp == Math.max(sonNum[1]-sonNum[i], maxSon[i]))
				ans.add(i);
		}
		for (int i = 0; i < ans.size(); i++)
			System.out.print(ans.get(i)+" ");
	}
	
}

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