How do I optimize my code?

I am trying to solve this problem : WORDS1 Problem - CodeChef

I am using BFS to see if the graph is connected and then finding Eulerian path.

My code : CodeChef: Practical coding for everyone (adjacency list)

CodeChef: Practical coding for everyone (adjacency matrix)