muffinman1015

Jan2019SilverI

Jan 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class planting {
  5.  
  6. public static void main(String[] args) throws java.io.IOException {
  7. BufferedReader in = new BufferedReader(new FileReader("planting.in"));
  8. PrintWriter out = new PrintWriter(new FileWriter("planting.out"));
  9. StringTokenizer tok = new StringTokenizer(in.readLine());
  10.  
  11. int n = Integer.parseInt(tok.nextToken());
  12. int numFields = n;
  13.  
  14. int[] fields = new int[2*(n-1)];
  15.  
  16. for (int i = 0; i < n-1; i++) {
  17. tok = new StringTokenizer(in.readLine());
  18. fields[2*i] = Integer.parseInt(tok.nextToken());
  19. fields[2*i+1] = Integer.parseInt(tok.nextToken());
  20. }
  21. Arrays.sort(fields);
  22.  
  23. int max_count = 1;
  24. int res = fields[0];
  25. int curr_count = 1;
  26.  
  27. for (int i = 1; i < fields.length; i++)
  28. {
  29. if (fields[i] == fields[i - 1])
  30. curr_count++;
  31. else
  32. {
  33. if (curr_count > max_count)
  34. {
  35. max_count = curr_count;
  36. res = fields[i - 1];
  37. }
  38. curr_count = 1;
  39. }
  40. }
  41.  
  42. // If last element is most frequent
  43. if (curr_count > max_count)
  44. {
  45. max_count = curr_count;
  46. res = fields[fields.length - 1];
  47. }
  48.  
  49. int count = 0;
  50. for (int j = 0; j < fields.length; j++) {
  51. if (fields[j] == res) {
  52. count++;
  53. }
  54. }
  55.  
  56. out.println(count + 1);
  57. out.close();
  58. in.close();
  59. }
  60.  
  61.  
  62. }
Add Comment
Please, Sign In to add comment