From a4c311fdaa1a587255f01f584c42ca9bea5e1202 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Wed, 7 Jan 2026 21:13:19 +0530 Subject: [PATCH] Add DFS solution for Codeforces problem 813 Implemented a DFS-based solution to find the maximum distance Bob can delay based on optimal moves. --- .../Day-11/sol/ishanrajsingh/Solution2.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Problems/Graph/Day-11/sol/ishanrajsingh/Solution2.py diff --git a/Problems/Graph/Day-11/sol/ishanrajsingh/Solution2.py b/Problems/Graph/Day-11/sol/ishanrajsingh/Solution2.py new file mode 100644 index 00000000..64cdcf9f --- /dev/null +++ b/Problems/Graph/Day-11/sol/ishanrajsingh/Solution2.py @@ -0,0 +1,37 @@ +# Submission: https://codeforces.com/contest/813/submission/356818920 +# Approach: find the farthest node bob can delay to by dfs the distances and simulate optimal moves +# Runtime: O(n) +import sys +sys.setrecursionlimit(500000) +input=sys.stdin.readline + +n,x=map(int,input().split()) +x-=1 +g=[[] for _ in range(n)] +for _ in range(n-1): + u,v=map(int,input().split()) + u-=1; v-=1 + g[u].append(v) + g[v].append(u) + +distA=[-1]*n +distB=[-1]*n + +def dfs(start,dist): + stack=[start] + dist[start]=0 + while stack: + u=stack.pop() + for v in g[u]: + if dist[v]==-1: + dist[v]=dist[u]+1 + stack.append(v) + +dfs(0,distA) +dfs(x,distB) + +ans=0 +for i in range(n): + if distB[i]>distA[i]: + ans=max(ans,distA[i]+distB[i]) +print(ans+1)