using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace S3 { class Program { class Friendship { public int friender; public int friendee; public Friendship(int friender, int friendee) { this.friender = friender; this.friendee = friendee; } } static void Main(string[] args) { List friends = new List(); int students = int.Parse(Console.ReadLine()); for (int i = 0; i < students; i++) { string[] assignments = Console.ReadLine().Split(' '); friends.Add(new Friendship(int.Parse(assignments[0]), int.Parse(assignments[1]))); } // Generate circle list List> circles = new List>(); foreach (Friendship friend in friends) { // Stop repeated instances of the same circles from popping up if (circles.SingleOrDefault(c => c.Contains(friend.friender)) != null) continue; //nvm, that screws things up //nvm*2, that takes for-fucking-ever List circle = new List(); circle.Add(friend.friender); circle.Add(friend.friendee); ContinueCircle(friends, circle); circles.Add(circle); } do { string[] checksS = Console.ReadLine().Split(' '); int[] checks = new int[2]; checks[0] = int.Parse(checksS[0]); checks[1] = int.Parse(checksS[1]); if (checks[0] == 0 && checks[1] == 0) break; //List circle = circles.Single(c => c[0] == checks[0]); List circle = circles.Single(c => c.Contains(checks[0])); RearrangeCircle(circle, checks[0], out circle); if (!circle.Contains(checks[1])) Console.WriteLine("No"); else { Console.Write("Yes "); bool working = true; Console.WriteLine(circle.Count(i => { if (working && i == checks[1]) { working = false; } return working; }) - 1); } } while (true); } static void RearrangeCircle(List circle, int num, out List ret) { int index = circle.IndexOf(num); ret = new List(); for (int i = 0; i < circle.Count; i++) { ret.Add(circle[(i + index) % circle.Count]); } } static void ContinueCircle(List friends, List circle) { foreach (Friendship friend in friends) { if (circle[circle.Count - 1] == friend.friender) { if (circle[0] == friend.friendee) return; circle.Add(friend.friendee); ContinueCircle(friends, circle); } } } } }