Given the number of vertices (N). Next comes the description of the N-1 edge in the graph, that is, the beginning and end of the edge. There is only one path between any two vertices. The graph itself is undirected.
Purpose: to find such a vertex in the graph, by removing which, the graph would split into at least two new smaller graphs, the number of which vertices are the same. If there is no such vertex, print -1.
I did something like this: for each vertex I looked at adjacent vertices and DFS went from them while counting how many vertices I go around. Then, if the number of vertices that DFS counted for each adjacent vertex of the original vertex is the same, then the original vertex is right for us. This solution gives the correct answer, but does not pass in time. I guess there is a more correct approach to the solution.