Any better alternative to do CHEFTRAV

I did this question using link list.But it showed me TLE for both the test cases.

link to ans:https://www.codechef.com/viewsolution/15586147

Any other better approach?

Hello,

This Problem can be reduced to a basic graph problem where we are given source and destination and we have to go from source to destination.

finding source: the source[i] which is occuring only once

finding destination: the dest[i] which is occuring only once

void dfs(string source,string destination)
{
	if(source!=destination){
		cout<<source<<"-"<<graph[start]<<" ";
		dfs(graph[source],destination);
	}
}

here graph stores destination of the particular source.

hope that helps… complexity is O(n)

1 Like