Can someone help me with this problem — https://codeforces.me/problemset/problem/2258/C
i used 2 bfs traversals to find the tree's diameter, i built an adjacency list from the tree edges, used bfs from node 1 to find the farthest node, then ran bfs again from that node to find the diameter and printed the endpoints and distance.








You are getting ILE. Fix it by
cout << endl;after each queries.i tried that too, but i am still getting ILE. the issue seems to be something else.
Can you send me your code, since somehow it's showing N/A for me
idk where it is going wrong ~~~~~~~~~~~~~~~~~ import java.util.*; public class Main { static ArrayList[] adj; static int[] dist; static int bfs(int start, int n) { Arrays.fill(dist, -1); Queue q = new ArrayDeque<>(); q.add(start); dist[start] = 0; int farthest = start; while (!q.isEmpty()) { int u = q.poll(); for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.add(v); if (dist[v] > dist[farthest]) { farthest = v; } } } } return farthest; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); adj = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < n — 1; i++) { int u = sc.nextInt(); int v = sc.nextInt(); adj[u].add(v); adj[v].add(u); } dist = new int[n + 1]; int a = bfs(1, n); int b = bfs(a, n); int diameter = dist[b]; System.out.println("! " + a + " " + b + " " + diameter); } } } ~~~~~~~~~~~~~~~~~
I'm a C++ user, so this might be wrong, but I can see that in your code you didn't even ask anything. You should read the guide for interactive problems to understand more about this type of problem.