yesugarisaicharanreddy's blog

By yesugarisaicharanreddy, history, 3 weeks ago, In English

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.

»
3 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

You are getting ILE. Fix it by cout << endl; after each queries.

  • »
    »
    3 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    i tried that too, but i am still getting ILE. the issue seems to be something else.

    • »
      »
      »
      3 weeks ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Can you send me your code, since somehow it's showing N/A for me

      • »
        »
        »
        »
        3 weeks ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        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); } } } ~~~~~~~~~~~~~~~~~