Hello codeforces ,
i'm brand new to dfs and i have spend like 4 hours trying to find a solution for The Tag Game problem.
what i have tried:
bob wants to maxmizes the solution , alice wants to minimize , so bob need to reach the deepest node , when bob reaches it alice will move to catch him , but bob has the choice to stay in the node (each of them has that option but i guess only bob will use it) i can't handle this choice of staying as a move, this is my code:
const int N_=10e5+6,M=2*10e5+6;
const int NOT_VISITED = 0 , IN_PROGRESS = 1 , VISITED = 2;
int m,n,u,v;
vector<int> adj[N_];
vector<pair<int,int>> dxdy = {{-1,0},{1,0},{0,1},{0,-1}};
// vector<vector<bool>> vis;
bool vis[N_];
int c;
// bool isValid(int x , int y , vector<STR>&grid){
// if(x < 0 || x > n-1 || y <0 || y>m-1) return false;
// if(vis[x][y] || grid[x][y] == '#') return false;
// return true;
// }
int md = 0 , fn=0;
void dfs(int u,int depth){
vis[u] =true;
if(depth > md){
md = depth;
fn = u;
}
c++;
for(auto x : adj[u]){
if(!vis[x]){
dfs(x,depth+1);
}
}
}
void solve()
{
cin >> n>>m;
for(int x = 0 ;x < n-1 ; x++){
cin >>u >>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(m,0);
int ans = md;
memset(vis,false,sizeof(vis));
md=0;
c=0;
dfs(1,0);
c-=2;
cout << ans+c+md;
}
I just looked at the question. The first thing that came to my mind is to apply bfs from both 1 and X. By this you will find the distances of all nodes from alice and bob. Now you need to find the nodes where alice distance is more than bob and then the ans will be just twice of the max of all those distances of alice. I could be wrong but thats what came to my mind after looking at the question.