12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package tuirngCup2019ahstuACM;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- /*
- 把节点最多的点看作树根,那个结果就是这个树根的直接孩子数+1
- */
- public class I {
- public static void main(String[] args) {
- Scanner cin = new Scanner(System.in);
- int n = cin.nextInt();
- if (n == 1){
- System.out.println(1);
- return;
- }
- HashMap<Integer, ArrayList<Integer>>hashMap = new HashMap<>();
- for(int i = 1; i < n; i++){
- int x = cin.nextInt();
- int y = cin.nextInt();
- add(hashMap, x, y);
- add(hashMap, y, x);
- }
- int best = 0;
- for(int i = 1; i <= n; i++){
- int cnt = 1;
- ArrayList<Integer>values = hashMap.get(i);
- cnt += values.size();
- best = Math.max(best, cnt);
- }
- System.out.println(best);
- }
- private static void add(HashMap<Integer, ArrayList<Integer>> hashMap, int x, int y) {
- ArrayList<Integer>values = hashMap.get(x);
- if(values == null){
- values = new ArrayList<>();
- }
- values.add(y);
- hashMap.put(x, values);
- }
- }
|