Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class planting {
- public static void main(String[] args) throws java.io.IOException {
- BufferedReader in = new BufferedReader(new FileReader("planting.in"));
- PrintWriter out = new PrintWriter(new FileWriter("planting.out"));
- StringTokenizer tok = new StringTokenizer(in.readLine());
- int n = Integer.parseInt(tok.nextToken());
- int numFields = n;
- int[] fields = new int[2*(n-1)];
- for (int i = 0; i < n-1; i++) {
- tok = new StringTokenizer(in.readLine());
- fields[2*i] = Integer.parseInt(tok.nextToken());
- fields[2*i+1] = Integer.parseInt(tok.nextToken());
- }
- Arrays.sort(fields);
- int max_count = 1;
- int res = fields[0];
- int curr_count = 1;
- for (int i = 1; i < fields.length; i++)
- {
- if (fields[i] == fields[i - 1])
- curr_count++;
- else
- {
- if (curr_count > max_count)
- {
- max_count = curr_count;
- res = fields[i - 1];
- }
- curr_count = 1;
- }
- }
- // If last element is most frequent
- if (curr_count > max_count)
- {
- max_count = curr_count;
- res = fields[fields.length - 1];
- }
- int count = 0;
- for (int j = 0; j < fields.length; j++) {
- if (fields[j] == res) {
- count++;
- }
- }
- out.println(count + 1);
- out.close();
- in.close();
- }
- }
Add Comment
Please, Sign In to add comment