Cod sursa(job #1429574)

Utilizator PlayLikeNeverB4George Marcus PlayLikeNeverB4 Data 6 mai 2015 18:02:20
Problema Diametrul unui arbore Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 3.37 kb
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.InputStream;
import java.util.Random;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileOutputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream;
		try {
			inputStream = new FileInputStream("darb.in");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		OutputStream outputStream;
		try {
			outputStream = new FileOutputStream("darb.out");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		darb solver = new darb();
		solver.solve(1, in, out);
		out.close();
	}
}

class darb {
    int N;
    ArrayList<Integer>[] A;
    int[] Q;
    int[] distances;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        N = in.nextInt();
        A = new ArrayList[N];
        for (int i = 0; i < N; i++) {
            A[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < N - 1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            a--; b--;
            A[a].add(b);
            A[b].add(a);
        }
        Q = new int[N + 1];
        distances = new int[N + 1];
        int best1 = bfs(-1);
        int best2 = bfs(best1);

        out.println(distances[best2] + 1);
    }

    private int bfs(int root) {
        if (root == -1) {
            root = new Random().nextInt(N);
        }
        int p = 0;
        Q[p++] = root;
        Arrays.fill(distances, -1);
        distances[root] = 0;

        int bestNode = root;
        for (int i = 0; i < p; i++) {
            int node = Q[i];
            for (int x : A[node]) {
                if (distances[x] == -1) {
                    distances[x] = distances[node] + 1;
                    Q[p++] = x;
                }
            }
            if (distances[node] > distances[bestNode]) {
                bestNode = node;
            }
        }

        return bestNode;
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));

        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

}