#K72667. Cycle Through a Specific Vertex

    ID: 33805 Type: Default 1000ms 256MiB

Cycle Through a Specific Vertex

Cycle Through a Specific Vertex

You are given an undirected graph with n vertices and m edges. Additionally, a special vertex x is provided. Your task is to determine whether there exists a cycle in the graph which passes through vertex x exactly once.

A cycle is defined as a path (with at least 3 vertices) that starts and ends at the same vertex and doesn’t repeat any other vertices. The cycle found must include vertex x precisely one time, meaning that vertex x should appear in the cycle and should not be repeated apart from its role as an entry/exit point.

Note: The graph is undirected and may contain isolated vertices or disconnected components.

Input Format: The first line contains three space-separated integers n, m and x. The next m lines each contain two space-separated integers u and v indicating an undirected edge between vertices u and v.

Output: Print YES if there exists such a cycle and NO otherwise.

inputFormat

The first line contains three integers: n (the number of vertices), m (the number of edges) and x (the vertex that must appear in the cycle exactly once).

The following m lines each contain two integers u and v representing an undirected edge between vertices u and v.

outputFormat

Output a single line: YES if there exists a cycle that passes through vertex x exactly once; otherwise, output NO.

## sample
5 6 3
1 2
2 3
3 4
4 5
5 3
1 3
YES