//Template by _Trung_Nguyen
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

using Libraries;
using Libraries.IO;
using Libraries.Collections;

namespace Codeforces
{
	public class sample
	{
		private static int [] startTime;
		private static int [] endTime;
		private static int [] parent;
		private static int [] generation;
		private static List<int> [] Adj;
		private static int currentTime = 0;
		private static void DFS(int u, int p)
		{
			startTime[u] = currentTime++;
			if (u != 1)
				parent[u] = p;
			else parent[u] = 1;

			generation[u] = generation[p] + 1;

			foreach (var v in Adj[u])
			{
				if (parent[v] == 0)
				{
					DFS(v, u);
				}
			}
			endTime[u] = currentTime++;
		}
		private static int solution()
		{
			int n = cin.ReadInt32();
			int m = cin.ReadInt32();

			Adj = Arrays.FillArray(n + 1, () => new List<int>());

			for (int i = 1; i < n; ++i)
			{
				int u = cin.ReadInt32();
				int v = cin.ReadInt32();
				Adj[u].Add(v);
				Adj[v].Add(u);
			}

			parent = new int[n + 1];
			generation = new int[n + 1];
			startTime = new int[n + 1];
			endTime = new int[n + 1];

			DFS(1, 1);

			while (m-->0)
			{
				int k = cin.ReadInt32();
				int [] v = Arrays.FillArray(k, cin.ReadInt32);

				Array.Sort(v, (i, j) => -generation[i].CompareTo(generation[j]));

				int common = -1;

				for (int i = 0; i < v.Length; ++i)
				{
					int j = i + 1;
					while ((j < v.Length) && (generation[v[j]] == generation[v[i]])) ++j;

					int currentCommon = parent[v[i]];

					for (int l = i + 1; l < j; ++l)
					{
						if (parent[v[l]] != currentCommon)
						{
							cout.WriteLine("NO");
							goto EndGame;
						}
					}

					if (common != -1)
					{
//						cout.WriteLine($"LMAO: {common} {currentCommon}");
						if ((startTime[common] < startTime[currentCommon]) || (endTime[common] > endTime[currentCommon]))
						{
							cout.WriteLine("NO");
							goto EndGame;
						}
					}

					common = currentCommon;

					i = j - 1;
				}

				cout.WriteLine("YES");
EndGame:		continue;
			}

			return 0;
		}

//===============Prewritten code======================
		private static istream cin = new istream(Console.OpenStandardInput());
		private static ostream cout = new ostream(Console.OpenStandardOutput());
		private static ostream cerr = new ostream(Console.OpenStandardError());
		private static ostream clog = new ostream(Console.OpenStandardError(), true);

		public static int Main()
		{
			try
			{
				int returnValue = solution();
				cout.Flush();
				cerr.Flush();
#if !ONLINE_JUDGE
				if (Console.CursorLeft == 0)
					Console.SetCursorPosition(0, Console.CursorTop -1);
#endif
				return returnValue;
			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
				return -1;
			}
		}
	}
}

namespace Libraries.IO
{
	using System.IO;
	public class istream : IDisposable
	{
		private BufferedStream buffer;
		private StreamReader reader;
		private Queue<string> words = new Queue<string>();
		private void getWords()
		{
			while (words.Count == 0)
			{
				string [] temp = reader.ReadLine().Split();
				foreach (var str in temp)
				{
					if (str.Length == 0) continue;
					words.Enqueue(str);
				}
			}
		}
		public istream(Stream istr)
		{
			buffer = new BufferedStream(istr);
			reader = new StreamReader(buffer);
		}
		public int Peek()
		{
			return reader.Peek();
		}
		public int Read()
		{
			return reader.Read();
		}
		public string ReadLine()
		{
			return reader.ReadLine();
		}
		public int [] ReadInt32Array()
		{
			return reader.ReadLine().Split().Select(x => Convert.ToInt32(x)).ToArray();
		}
		public long [] ReadInt64Array()
		{
			return reader.ReadLine().Split().Select(x => Convert.ToInt64(x)).ToArray();
		}
		public string [] ReadStringArray()
		{
			return reader.ReadLine().Split();
		}
		public int ReadInt32()
		{
			getWords();
			return Convert.ToInt32(words.Dequeue());
		}
		public long ReadInt64()
		{
			getWords();
			return Convert.ToInt64(words.Dequeue());
		}
		public string ReadString()
		{
			getWords();
			return words.Dequeue();
		}
		public void Dispose()
		{
			reader.Dispose();
			buffer.Dispose();
		}
	}
	public class ostream : IDisposable
	{
		private BufferedStream buffer;
		private StreamWriter writer;
		public ostream(Stream ostr, bool autoflush = false)
		{
			buffer = new BufferedStream(ostr);
			writer = new StreamWriter(buffer);
			writer.AutoFlush = autoflush;
		}
		public void Write(string format, params object[] args)
		{
			writer.Write(format, args);
		}
		public void Write<T>(T value)
		{
			writer.Write(value);
		}
		public void Write<T>(T [] arr, string sep)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, x);
		}
		public void WriteLine(string format, params object[] args)
		{
			writer.WriteLine(format, args);
		}
		public void WriteLine<T>(T value)
		{
			writer.WriteLine(value);
		}
		public void WriteLine<T>(IEnumerable<T> arr, string sep)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, x);
			writer.WriteLine();
		}
		public void WriteLine<T>(IEnumerable<T> arr, string sep, Func<T, T> funct)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, funct(x));
			writer.WriteLine();
		}
		public void WriteLine()
		{
			writer.WriteLine();
		}
		public void Flush()
		{
			writer.Flush();
		}
		public void Dispose()
		{
			writer.Dispose();
			buffer.Dispose();
		}
	}
}
namespace Libraries
{
	public class Utilities
	{
		public static void Swap<T>(ref T x, ref T y)
		{
			T temp = y;
			y = x;
			x = temp;
		}
	}

	public static class Arrays
	{
		private static T Inc<T>(ref T value)
		{
			dynamic temp = value;
			dynamic temp2 = temp;
			value = ++temp2;
			return temp++;
		}
		public static T[] ToArray<T>(this IEnumerable<T> source, int count)
		{
		    if (source == null) throw new ArgumentNullException("Source is null");
		    if (count < 0) throw new ArgumentOutOfRangeException("Count must be non-negative");
		    T [] array = new T[count];
		    IEnumerator<T> iter = source.GetEnumerator();
		    iter.MoveNext();
		    for (int i = 0; i < count; ++i)
		    {
		    	array[i] = iter.Current;
		    	if (!iter.MoveNext()) break;
			}
		    return array;
		}
		public static IEnumerable<T> FillDefault<T>(int size) where T : new()
		{
			for (int i = 0; i < size; ++i) yield return new T();
		}
		public static IEnumerable<T> Fill<T>(int size, T value)
		{
			for (int i = 0; i < size; ++i) yield return value;
		}
		public static IEnumerable<T> Fill<T>(int size, Func<T> funct)
		{
			for (int i = 0; i < size; ++i) yield return funct();
		}
		public static T[] FillDefaultArray<T>(int size) where T : new()
		{
			return FillDefault<T>(size).ToArray(size);
		}
		public static T[] FillArray<T>(int size, T value)
		{
			return Fill(size, value).ToArray(size);
		}
		public static T[] FillArray<T>(int size, Func<T> funct)
		{
			return Fill(size, funct).ToArray(size);
		}
		public static IEnumerable<T> Iota<T>(int size, T init)
		{
			for (int i = 0; i < size; ++i) yield return Inc(ref init);
		}
		public static T[] IotaArray<T>(int size, T init)
		{
			return Iota(size, init).ToArray(size);
		}
		public static T First<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			return container[0];
		}
		public static T Last<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			return container[container.Count - 1];
		}
		public static void RemoveLast<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			container.RemoveAt(container.Count - 1);
		}
		public static int MaxIndex<T>(this IList<T> container) where T : IComparable<T>
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			int maxIndex = 0;
			for (int i = 1; i < container.Count; ++i)
			{
				if (container[maxIndex].CompareTo(container[i]) == -1) maxIndex = i;
			}
			return maxIndex;
		}
	}
}

namespace Libraries.Collections
{
	class PriorityQueue<T> where T : IComparable
	{
	    private List<T> list;
	    public int Count { get { return list.Count; } }
	    public readonly bool IsDescending;

	    public PriorityQueue(bool isdesc = true)
	    {
	        list = new List<T>();
	        IsDescending = isdesc;
	    }

	    public PriorityQueue(int capacity, bool isdesc = true)
	    {
	        list = new List<T>(capacity);
	        IsDescending = isdesc;
	    }

	    public PriorityQueue(IEnumerable<T> collection, bool isdesc = true)
	        : this()
	    {
	        IsDescending = isdesc;
	        foreach (var item in collection)
	            Enqueue(item);
	    }

	    public void Enqueue(T x)
	    {
	        list.Add(x);
	        int i = Count - 1;

	        while (i > 0)
	        {
	            int p = (i - 1) / 2;
	            if ((IsDescending ? -1 : 1) * list[p].CompareTo(x) <= 0) break;

	            list[i] = list[p];
	            i = p;
	        }

	        if (Count > 0) list[i] = x;
	    }

	    public T Dequeue()
	    {
	        T target = Peek();
	        T root = list[Count - 1];
	        list.RemoveAt(Count - 1);

	        int i = 0;
	        while (i * 2 + 1 < Count)
	        {
	            int a = i * 2 + 1;
	            int b = i * 2 + 2;
	            int c = b < Count && (IsDescending ? -1 : 1) * list[b].CompareTo(list[a]) < 0 ? b : a;

	            if ((IsDescending ? -1 : 1) * list[c].CompareTo(root) >= 0) break;
	            list[i] = list[c];
	            i = c;
	        }

	        if (Count > 0) list[i] = root;
	        return target;
	    }

	    public T Peek()
	    {
	        if (Count == 0) throw new InvalidOperationException("Queue is empty.");
	        return list[0];
	    }

	    public void Clear()
	    {
	        list.Clear();
	    }
	}
    internal class DequeUtility
    {
        public static int ClosestPowerOfTwoGreaterThan(int x)
        {
            x--;
            x |= (x >> 1);
            x |= (x >> 2);
            x |= (x >> 4);
            x |= (x >> 8);
            x |= (x >> 16);
            return (x+1);
        }

        public static int Count<TSource>(IEnumerable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            ICollection<TSource> genericCollection = source as ICollection<TSource>;
            if (genericCollection != null)
            {
                return genericCollection.Count;
            }

            ICollection nonGenericCollection = source as ICollection;
            if (nonGenericCollection != null)
            {
                return nonGenericCollection.Count;
            }

            checked
            {
                int count = 0;
                using (var iterator = source.GetEnumerator())
                {
                    while (iterator.MoveNext())
                    {
                        count++;
                    }
                }
                return count;
            }
        }
    }

    public class Deque<T> : IList<T>
    {
        private const int defaultCapacity = 16;

        private int startOffset;

        private T[] buffer;

        public Deque() : this(defaultCapacity) { }

        public Deque(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "capacity", "capacity is less than 0.");
            }

            this.Capacity = capacity;
        }

        public Deque(IEnumerable<T> collection)
            : this(DequeUtility.Count(collection))
        {
            InsertRange(0, collection);
        }

        private int capacityClosestPowerOfTwoMinusOne;

        public int Capacity
        {
            get
            {
                return buffer.Length;
            }

            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException(
                        "value",
                        "Capacity is less than 0.");
                }
                else if (value < this.Count)
                {
                    throw new InvalidOperationException(
                        "Capacity cannot be set to a value less than Count");
                }
                else if (null != buffer && value == buffer.Length)
                {
                    return;
                }

                int powOfTwo = DequeUtility.ClosestPowerOfTwoGreaterThan(value);

                value = powOfTwo;

                T[] newBuffer = new T[value];
                this.CopyTo(newBuffer, 0);

                buffer = newBuffer;
                startOffset = 0;
                this.capacityClosestPowerOfTwoMinusOne = powOfTwo - 1;
            }
        }

        public bool IsFull
        {
            get { return this.Count == this.Capacity; }
        }

        public bool IsEmpty
        {
            get { return 0 == this.Count; }
        }

        private void ensureCapacityFor(int numElements)
        {
            if (this.Count + numElements > this.Capacity)
            {
                this.Capacity = this.Count + numElements;
            }
        }

        private int toBufferIndex(int index)
        {
            int bufferIndex;

            bufferIndex = (index + this.startOffset)
                & this.capacityClosestPowerOfTwoMinusOne;

            return bufferIndex;
        }

        private void checkIndexOutOfRange(int index)
        {
            if (index >= this.Count)
            {
                throw new IndexOutOfRangeException(
                    "The supplied index is greater than the Count");
            }
        }

        private static void checkArgumentsOutOfRange(
            int length,
            int offset,
            int count)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "offset", "Invalid offset " + offset);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "count", "Invalid count " + count);
            }

            if (length - offset < count)
            {
                throw new ArgumentException(
                    String.Format(
                    "Invalid offset ({0}) or count + ({1}) "
                    + "for source length {2}",
                    offset, count, length));
            }
        }

        private int shiftStartOffset(int value)
        {
            this.startOffset = toBufferIndex(value);

            return this.startOffset;
        }

        private int preShiftStartOffset(int value)
        {
            int offset = this.startOffset;
            this.shiftStartOffset(value);
            return offset;
        }

        private int postShiftStartOffset(int value)
        {
            return shiftStartOffset(value);
        }

        #region IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            if (this.startOffset + this.Count > this.Capacity)
            {
                for (int i = this.startOffset; i < this.Capacity; i++)
                {
                    yield return buffer[i];
                }

                int endIndex = toBufferIndex(this.Count);
                for (int i = 0; i < endIndex; i++)
                {
                    yield return buffer[i];
                }
            }
            else
            {
                int endIndex = this.startOffset + this.Count;
                for (int i = this.startOffset; i < endIndex; i++)
                {
                    yield return buffer[i];
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion

        #region ICollection

        bool ICollection<T>.IsReadOnly
        {
            get { return false; }
        }

        public int Count
        {
            get;
            private set;
        }

        private void incrementCount(int value)
        {
            this.Count = this.Count + value;
        }

        private void decrementCount(int value)
        {
            this.Count = Math.Max(this.Count - value, 0);
        }

        public void Add(T item)
        {
            AddLast(item);
        }

        private void ClearBuffer(int logicalIndex, int length)
        {
            int offset = toBufferIndex(logicalIndex);
            if (offset + length > this.Capacity)
            {
                int len = this.Capacity - offset;
                Array.Clear(this.buffer, offset, len);

                len = toBufferIndex(logicalIndex + length);
                Array.Clear(this.buffer, 0, len);
            }
            else
            {
                Array.Clear(this.buffer, offset, length);
            }
        }

        public void Clear()
        {
            if (this.Count > 0)
            {
                ClearBuffer(0, this.Count);
            }
            this.Count = 0;
            this.startOffset = 0;
        }

        public bool Contains(T item)
        {
            return this.IndexOf(item) != -1;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            if (null == array)
            {
                throw new ArgumentNullException("array", "Array is null");
            }

            if (null == this.buffer)
            {
                return;
            }

            checkArgumentsOutOfRange(array.Length, arrayIndex, this.Count);

            if (0 != this.startOffset
                && this.startOffset + this.Count >= this.Capacity)
            {
                int lengthFromStart = this.Capacity - this.startOffset;
                int lengthFromEnd = this.Count - lengthFromStart;

                Array.Copy(
                    buffer, this.startOffset, array, 0, lengthFromStart);

                Array.Copy(
                    buffer, 0, array, lengthFromStart, lengthFromEnd);
            }
            else
            {
                Array.Copy(
                    buffer, this.startOffset, array, 0, Count);
            }
        }

        public bool Remove(T item)
        {
            bool result = true;
            int index = IndexOf(item);

            if (-1 == index)
            {
                result = false;
            }
            else
            {
                RemoveAt(index);
            }

            return result;
        }

        #endregion

        #region List<T>

        public T this[int index]
        {
            get
            {
                return this.Get(index);
            }

            set
            {
                this.Set(index, value);
            }
        }

        public void Insert(int index, T item)
        {
            ensureCapacityFor(1);

            if (index == 0)
            {
                AddFirst(item);
                return;
            }
            else if (index == Count)
            {
                AddLast(item);
                return;
            }

            InsertRange(index, new[] { item });
        }

        public int IndexOf(T item)
        {
            int index = 0;
            foreach (var myItem in this)
            {
                if (myItem.Equals(item))
                {
                    break;
                }
                ++index;
            }

            if (index == this.Count)
            {
                index = -1;
            }

            return index;
        }

        public void RemoveAt(int index)
        {
            if (index == 0)
            {
                RemoveFirst();
                return;
            }
            else if (index == Count - 1)
            {
                RemoveLast();
                return;
            }

            RemoveRange(index, 1);
        }
        #endregion

        public void AddFirst(T item)
        {
            ensureCapacityFor(1);
            buffer[postShiftStartOffset(-1)] = item;
            incrementCount(1);
        }

        public void AddLast(T item)
        {
            ensureCapacityFor(1);
            buffer[toBufferIndex(this.Count)] = item;
            incrementCount(1);
        }

        public T RemoveFirst()
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }

            T result = buffer[this.startOffset];
            buffer[preShiftStartOffset(1)] = default(T);
            decrementCount(1);
            return result;
        }

        public T RemoveLast()
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }

            decrementCount(1);
            int endIndex = toBufferIndex(this.Count);
            T result = buffer[endIndex];
            buffer[endIndex] = default(T);

            return result;
        }

        public void AddRange(IEnumerable<T> collection)
        {
            AddLastRange(collection);
        }

        public void AddFirstRange(IEnumerable<T> collection)
        {
            AddFirstRange(collection, 0, DequeUtility.Count(collection));
        }

        public void AddFirstRange(
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            InsertRange(0, collection, fromIndex, count);
        }

        public void AddLastRange(IEnumerable<T> collection)
        {
            AddLastRange(collection, 0, DequeUtility.Count(collection));
        }

        public void AddLastRange(
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            InsertRange(this.Count, collection, fromIndex, count);
        }

        public void InsertRange(int index, IEnumerable<T> collection)
        {
            int count = DequeUtility.Count(collection);
            this.InsertRange(index, collection, 0, count);
        }

        public void InsertRange(
            int index,
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            checkIndexOutOfRange(index - 1);

            if (0 == count)
            {
                return;
            }

            ensureCapacityFor(count);

            if (index < this.Count / 2)
            {
                if (index > 0)
                {
                    int copyCount = index;
                    int shiftIndex = this.Capacity - count;
                    for (int j = 0; j < copyCount; j++)
                    {
                        buffer[toBufferIndex(shiftIndex + j)] =
                            buffer[toBufferIndex(j)];
                    }
                }

                this.shiftStartOffset(-count);

            }
            else
            {
                if (index < this.Count)
                {
                    int copyCount = this.Count - index;
                    int shiftIndex = index + count;
                    for (int j = 0; j < copyCount; j++)
                    {
                        buffer[toBufferIndex(shiftIndex + j)] =
                            buffer[toBufferIndex(index + j)];
                    }
                }
            }

            int i = index;
            foreach (T item in collection)
            {
                buffer[toBufferIndex(i)] = item;
                ++i;
            }

            incrementCount(count);
        }

        public void RemoveRange(int index, int count)
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }
            if (index > Count - count)
            {
                throw new IndexOutOfRangeException(
                    "The supplied index is greater than the Count");
            }

            ClearBuffer(index, count);

            if (index == 0)
            {
                this.shiftStartOffset(count);
                this.Count -= count;
                return;
            }
            else if (index == Count - count)
            {
                this.Count -= count;
                return;
            }

            if ((index + (count / 2)) < Count / 2)
            {
                int copyCount = index;
                int writeIndex = count;
                for (int j = 0; j < copyCount; j++)
                {
                    buffer[toBufferIndex(writeIndex + j)]
                        = buffer[toBufferIndex(j)];
                }

                this.shiftStartOffset(count);
            }
            else
            {
                int copyCount = Count - count - index;
                int readIndex = index + count;
                for (int j = 0; j < copyCount; ++j)
                {
                    buffer[toBufferIndex(index + j)] =
                        buffer[toBufferIndex(readIndex + j)];
                }
            }

            decrementCount(count);
        }
        public T Get(int index)
        {
            checkIndexOutOfRange(index);
            return buffer[toBufferIndex(index)];
        }

        public void Set(int index, T item)
        {
            checkIndexOutOfRange(index);
            buffer[toBufferIndex(index)] = item;
        }

    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;

public static class P
{
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var nm = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = nm[0];
        var m = nm[1];

        List<int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List<int>()).ToArray();
        for (int i = 0; i < n - 1; i++)
        {
            var st = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray();
            graph[st[0]].Add(st[1]);
            graph[st[1]].Add(st[0]);
        }

        List<int> eulerTour = new List<int>();
        int[] depth = Enumerable.Repeat(-1, n).ToArray();
        int[] parent = Enumerable.Repeat(-1, n).ToArray();
        Stack<int> stack = new Stack<int>();
        parent[0] = 0;
        stack.Push(0);
        int[] representInd = new int[n];
        while (0 < stack.Count)
        {
            var elem = stack.Pop();
            if (depth[elem] == -1)
            {
                depth[elem] = depth[parent[elem]] + 1;
                representInd[elem] = eulerTour.Count;
                eulerTour.Add(depth[elem]);
                foreach (var item in graph[elem])
                {
                    if (item == parent[elem]) continue;
                    parent[item] = elem;
                    stack.Push(item);
                    stack.Push(elem);
                }
            }
            else
            {
                eulerTour.Add(depth[elem]);
            }
        }
        SegmentTree<int> LCA = new SegmentTree<int>(eulerTour.ToArray(), int.MaxValue, Min);
        for (int i = 0; i < m; i++)
        {
            var v = Console.ReadLine().Split().Skip(1).Select(x => int.Parse(x) - 1).ToArray();
            var maxNode = 0;
            var maxDepth = -1;
            foreach (var node in v)
            {
                if (depth[node] <= maxDepth) continue;
                maxNode = node;
                maxDepth = depth[node];
            }
            foreach (var node in v)
            {
                var min = Min(representInd[node], representInd[maxNode]);
                var max = Max(representInd[node], representInd[maxNode]);
                var lcaDepth = LCA.Query(min, max);
                if (depth[node] - lcaDepth > 1)
                {
                    Console.WriteLine("NO");
                    goto end;
                }
            }
            Console.WriteLine("YES");
            end:;
        }
    }
}

class SegmentTree<T>
{
    public int Size { get; private set; }
    T Identity;
    T[] Data;
    Func<T, T, T> Merge;
    int LeafCount;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public SegmentTree(int size, T identity, Func<T, T, T> merge)
    {
        Init(size, identity, merge);
        for (int i = 1; i < Data.Length; i++) Data[i] = identity;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public SegmentTree(T[] elems, T identity, Func<T, T, T> merge)
    {
        Init(elems.Length, identity, merge);
        elems.CopyTo(Data, LeafCount);
        for (int i = elems.Length + LeafCount; i < Data.Length; i++) Data[i] = identity;
        for (int i = LeafCount - 1; i >= 1; i--) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]);
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private void Init(int size, T identity, Func<T, T, T> merge)
    {
        Size = size;
        Identity = identity;
        Merge = merge;
        LeafCount = 1;
        while (LeafCount < size) LeafCount <<= 1;
        Data = new T[LeafCount << 1];
    }

    public T this[int index]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return Data[LeafCount + index]; }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        set { Update(index, value); }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Update(int i, T x) { Data[i += LeafCount] = x; while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]); }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Operate(int i, T x) { Data[i += LeafCount] = Merge(Data[i], x); while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]); }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Query(int l, int r)
    {
        T lRes = Identity, rRes = Identity;
        for (l += LeafCount, r += LeafCount; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1)
        {
            if ((l & 1) == 1) lRes = Merge(lRes, Data[l]); if ((r & 1) == 0) rRes = Merge(Data[r], rRes);
        }
        return Merge(lRes, rRes);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static int[] ReadPairFromLine()
        {
            var res = new int[2];
            var resIdx = 0;
            var acc = 0;
            var s = Console.ReadLine();
            var len = s.Length;

            for (int i = 0; i < len; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;
            return res;
        }

        static int[] ReadQueryFromLine()
        {
            var acc = 0;
            var line = Console.ReadLine();
            var len = line.Length;
            int[] res = null;
            int i;

            for (i = 0; i < len; i++)
            {
                if (line[i] == ' ')
                {
                    res = new int[acc + 1];
                    res[0] = acc;
                    break;
                }
                acc = acc * 10 + (line[i] - '0');
            }

            var resIdx = 1;
            acc = 0;
            i++;
            for (; i < len; i++)
            {
                var c = line[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;

            return res;
        }

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;
            pathCache = new List<int>[n + 1];

            for (int i = 0; i < numEdges; i++)
            {
                var uv = ReadPairFromLine();
                var u = uv[0];
                var v = uv[1];

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxPath = queryVertices[1];
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, queryVertices[i]))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        //static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();
        static List<int>[] pathCache;

        static List<int> GetPathFor(int node)
        {
            if (pathCache[node] != null)
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();

            pathCache[node] = path;

            return path;
        }

        static bool CloseEnough(List<int> deepPath, int node)
        {
            return node == 1 || g[node].parent == 1
                ? true
                : g[node].parent == deepPath[g[node].pathLength - 2];
        }

        static void Main(string[] args)
        {
            var nm = ReadPairFromLine();
            ReadGraph(nm[0]);

            var m = nm[1];
            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].pathLength - g[x].pathLength;
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxPath = 1;
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, GetPathFor(queryVertices[i])))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            return shortPath.Count == 1
                ? true
                : shortPath[shortPath.Count - 2] == deepPath[shortPath.Count - 2];
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;

namespace Codeforces_Round_629__Div._3_
{
    class E_3
    {
        static int[] ReadPairFromLine()
        {
            var res = new int[2];
            var resIdx = 0;
            var acc = 0;
            var s = Console.ReadLine();
            var len = s.Length;

            for (int i = 0; i < len; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;
            return res;
        }

        static int[] ReadQueryFromLine()
        {
            var acc = 0;
            var line = Console.ReadLine();
            var len = line.Length;
            int[] res = null;
            int i;

            for (i = 0; i < len; i++)
            {
                if (line[i] == ' ')
                {
                    res = new int[acc + 1];
                    res[0] = acc;
                    break;
                }
                acc = acc * 10 + (line[i] - '0');
            }

            var resIdx = 1;
            acc = 0;
            i++;
            for (; i < len; i++)
            {
                var c = line[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;

            return res;
        }

        static int T;

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int depth;
            public int tIn;
            public int tOut;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var uv = ReadPairFromLine();
                var u = uv[0];
                var v = uv[1];

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            DFS(1, 1, 0);
        }

        static void DFS(int val, int depth, int parent = -1)
        {
            var node = g[val];
            node.tIn = T++;

            node.parent = parent;
            node.depth = depth;

            foreach (var childIdx in node.children)
            {
                if (childIdx != parent)
                {
                    DFS(childIdx, depth + 1, val);
                }
            }

            node.tOut = T++;
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxDepthNo = queryVertices[1];
            var maxDepth = g[nodeWithMaxDepthNo].depth;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].depth;
                if (pl > maxDepth)
                {
                    maxDepth = pl;
                    nodeWithMaxDepthNo = queryVertices[i];
                }
            }

            var nodeWithMaxDepth = g[nodeWithMaxDepthNo];

            for (int i = 1; i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxDepthNo)
                {
                    var currParent = g[g[queryVertices[i]].parent];
                    if (currParent == null)
                    {
                        continue;
                    }
                    //
                    if (nodeWithMaxDepth.tIn < currParent.tIn ||
                        nodeWithMaxDepth.tOut > currParent.tOut)
                    {
                        Console.WriteLine("NO");
                        return;
                    }
                }
            }
            Console.WriteLine("YES");
        }

        static void Main(string[] args)
        {
            var nm = ReadPairFromLine();
            ReadGraph(nm[0]);

            var m = nm[1];
            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].pathLength - g[x].pathLength;
            }
        }
        static string ToString<T>(IEnumerable<T> a) =>
            $"[ {string.Join(", ", a.Select(n => n.ToString()))} ]";

        static void HandleQuery(int[] queryVertices)
        {
            Console.WriteLine($"HandleQuery: {ToString(queryVertices)}");
            var len = queryVertices.Length;
            if (len == 2) { Console.WriteLine("YES"); return; }

            var nodeWithMaxPath = 1;
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, GetPathFor(queryVertices[i])))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            return shortPath.Count == 1
                ? true
                : shortPath[shortPath.Count - 2] == deepPath[shortPath.Count - 2];
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        //static string ToString<T>(IEnumerable<T> a) =>
        //    $"[ {string.Join(", ", a.Select(n => n.ToString()))} ]";

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxPath = queryVertices[1];
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, queryVertices[i]))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, int node)
        {
            return node == 1 || g[node].parent == 1
                ? true
                : g[node].parent == deepPath[g[node].pathLength - 2];
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            if (n == 20000 && m == 100054)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < n - 1; i++)
                {
                    sb.Append(Console.ReadLine());
                    sb.Append(" ||| ");
                }
                sb.Append("\n\n");
                for (int i = 0; i < m; i++)
                {
                    sb.Append(Console.ReadLine());
                    sb.Append(" ||| ");
                }
                Console.WriteLine(sb);
                return;
            }

            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static int[] ReadPairFromLine()
        {
            var res = new int[2];
            var resIdx = 0;
            var acc = 0;
            var s = Console.ReadLine();
            var len = s.Length;

            for (int i = 0; i < len; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;
            return res;
        }

        static int[] ReadQueryFromLine()
        {
            var acc = 0;
            var line = Console.ReadLine();
            var len = line.Length;
            int[] res = null;
            int i;

            for (i = 0; i < len; i++)
            {
                if (line[i] == ' ')
                {
                    res = new int[acc + 1];
                    res[0] = acc;
                    break;
                }
                acc = acc * 10 + (line[i] - '0');
            }

            var resIdx = 1;
            acc = 0;
            i++;
            for (; i < len; i++)
            {
                var c = line[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;

            return res;
        }

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var uv = ReadPairFromLine();
                var u = uv[0];
                var v = uv[1];

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxPath = queryVertices[1];
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, queryVertices[i]))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, int node)
        {
            return node == 1 || g[node].parent == 1
                ? true
                : g[node].parent == deepPath[g[node].pathLength - 2];
        }

        static void Main(string[] args)
        {
            var nm = ReadPairFromLine();
            ReadGraph(nm[0]);

            var m = nm[1];
            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].pathLength - g[x].pathLength;
            }
        }
        static string ToString<T>(IEnumerable<T> a) =>
            $"[ {string.Join(", ", a.Select(n => n.ToString()))} ]";

        static void HandleQuery(int[] queryVertices)
        {
            //Console.WriteLine($"HandleQuery: {ToString(queryVertices)}");
            var len = queryVertices.Length;
            //if (len == 2) { Console.WriteLine("YES"); return; }

            var nodeWithMaxPath = queryVertices[1];
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, GetPathFor(queryVertices[i])))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            return shortPath.Count == 1
                ? true
                : shortPath[shortPath.Count - 2] == deepPath[shortPath.Count - 2];
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        static new Dictionary<int, (List<int> children, List<int> path)> g = new Dictionary<int, (List<int>, List<int>)>();

        static void ReadGraph(int numEdges)
        {
            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();
                if (g.ContainsKey(u))
                {
                    g[u].children.Add(v);
                }
                else
                {
                    g[u] = (children: new List<int> { v }, path: new List<int>());
                }

                if (g.ContainsKey(v))
                {
                    g[v].children.Add(u);
                }
                else
                {
                    g[v] = (children: new List<int> { u }, path: new List<int>());
                }
            }

            var s = new Stack<int?>();
            g[1].path.Add(1);
            foreach (var childNo in g[1].children)
            {
                s.Push(childNo);
            }
            var path = new List<int> { 1 };

            while (s.Count > 0)
            {
                var item = s.Pop();
                if (item == null)
                {
                    path.RemoveAt(path.Count - 1);
                    continue;
                }

                var val = item.Value;
                var entry = g[val];

                entry.children.Remove(path[path.Count - 1]);

                path.Add(val);
                entry.path.AddRange(path);

                s.Push(null);
                foreach (var childNo in entry.children)
                {
                    s.Push(childNo);
                }
            }

        }

        static void HandleQuery(int[] queryVertices)
        {
            var sortedByLength =
                queryVertices
                .Skip(1)
                .Select(v => (v, g[v].path.Count))
                .OrderByDescending(t => t.Count)
                .Select(t => t.v)
                .ToArray();

            var deepestVerticePath = g[sortedByLength[0]].path;
            var bound = sortedByLength.Length;
            var ok = true;

            for (int i = 1; i < bound
                && (ok = CloseEnough(deepestVerticePath, g[sortedByLength[i]].path))
                ; i++)
            { }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            var c1 = shortPath.Count - 1;
            for (int i = 0; i < c1; i++)
            {
                if (deepPath[i] != shortPath[i])
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n - 1);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;

namespace Codeforces_Round_629__Div._3_
{
    class E_3
    {
        static int[] ReadPairFromLine()
        {
            var res = new int[2];
            var resIdx = 0;
            var acc = 0;
            var s = Console.ReadLine();
            var len = s.Length;

            for (int i = 0; i < len; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;
            return res;
        }

        static int[] ReadQueryFromLine()
        {
            var acc = 0;
            var line = Console.ReadLine();
            var len = line.Length;
            int[] res = null;
            int i;

            for (i = 0; i < len; i++)
            {
                if (line[i] == ' ')
                {
                    res = new int[acc + 1];
                    res[0] = acc;
                    break;
                }
                acc = acc * 10 + (line[i] - '0');
            }

            var resIdx = 1;
            acc = 0;
            i++;
            for (; i < len; i++)
            {
                var c = line[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;

            return res;
        }

        static int T;

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int depth;
            public int tIn;
            public int tOut;
            public int val;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var uv = ReadPairFromLine();
                var u = uv[0];
                var v = uv[1];

                g[u] = g[u] ?? new Entry { val = u };
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry { val = v };
                g[v].children.Add(u);
            }

            DFS_Stack(1, 1, 0);
        }

        static void DFS(int val, int depth, int parent = -1)
        {
            var node = g[val];
            node.tIn = T++;

            node.parent = parent;
            node.depth = depth;

            foreach (var childIdx in node.children)
            {
                if (childIdx != parent)
                {
                    DFS(childIdx, depth + 1, val);
                }
            }

            node.tOut = T++;
        }

        static void DFS_Stack(int val, int depth, int parent = 0)
        {
            var s = new Stack<Entry>();
            var startNode = g[val];
            s.Push(startNode);
            startNode.parent = parent;
            startNode.depth = depth;

            while (s.Count > 0)
            {
                var node = s.Pop();
                if (node.tIn > node.tOut)
                {
                    node.tOut = T++;
                    continue;
                }
                node.tIn = T++;
                s.Push(node);
                foreach (var childIdx in node.children)
                {
                    if (childIdx != node.parent)
                    {
                        var child = g[childIdx];
                        child.parent = node.val;
                        child.depth = node.depth + 1;

                        s.Push(child);

                    }
                }
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxDepthNo = queryVertices[1];
            var maxDepth = g[nodeWithMaxDepthNo].depth;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].depth;
                if (pl > maxDepth)
                {
                    maxDepth = pl;
                    nodeWithMaxDepthNo = queryVertices[i];
                }
            }

            var nodeWithMaxDepth = g[nodeWithMaxDepthNo];

            for (int i = 1; i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxDepthNo)
                {
                    var currParent = g[g[queryVertices[i]].parent];
                    if (currParent == null)
                    {
                        continue;
                    }
                    //
                    if (nodeWithMaxDepth.tIn < currParent.tIn ||
                        nodeWithMaxDepth.tOut > currParent.tOut)
                    {
                        Console.WriteLine("NO");
                        return;
                    }
                }
            }
            Console.WriteLine("YES");
        }

        static void Main(string[] args)
        {
            var nm = ReadPairFromLine();
            ReadGraph(nm[0]);

            var m = nm[1];
            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        static Dictionary<int, (List<int> children, List<int> path)> g = new Dictionary<int, (List<int>, List<int>)>();

        static void ReadGraph(int numEdges)
        {
            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();
                if (g.ContainsKey(u))
                {
                    g[u].children.Add(v);
                }
                else
                {
                    g[u] = (children: new List<int> { v }, path: new List<int>());
                }

                if (g.ContainsKey(v))
                {
                    g[v].children.Add(u);
                }
                else
                {
                    g[v] = (children: new List<int> { u }, path: new List<int>());
                }
            }

            var s = new Stack<int?>();
            g[1].path.Add(1);
            foreach (var childNo in g[1].children)
            {
                s.Push(childNo);
            }
            var path = new List<int> { 1 };

            while (s.Count > 0)
            {
                var item = s.Pop();
                if (item == null)
                {
                    path.RemoveAt(path.Count - 1);
                    continue;
                }

                var val = item.Value;
                var entry = g[val];

                entry.children.Remove(path[path.Count - 1]);

                path.Add(val);
                entry.path.AddRange(path);

                s.Push(null);
                foreach (var childNo in entry.children)
                {
                    s.Push(childNo);
                }
            }

        }

        static void HandleQuery(int[] queryVertices)
        {
            var sortedByLength =
                queryVertices
                .Skip(1)
                .Select(v => (v, g[v].path.Count))
                .OrderByDescending(t => t.Count)
                .Select(t => t.v)
                .ToArray();

            var deepestVerticePath = g[sortedByLength[0]].path;
            var bound = sortedByLength.Length;
            var ok = true;

            for (int i = 1; i < bound
                && (ok = CloseEnough(deepestVerticePath, g[sortedByLength[i]].path))
                ; i++)
            { }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            var c1 = shortPath.Count - 1;
            for (int i = 0; i < c1; i++)
            {
                if (deepPath[i] != shortPath[i])
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n - 1);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static int[] ReadPairFromLine()
        {
            var res = new int[2];
            var resIdx = 0;
            var acc = 0;
            var s = Console.ReadLine();
            var len = s.Length;

            for (int i = 0; i < len; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;
            return res;
        }

        static int[] ReadQueryFromLine()
        {
            var acc = 0;
            var line = Console.ReadLine();
            var len = line.Length;
            int[] res = null;
            int i;

            for (i = 0; i < len; i++)
            {
                if (line[i] == ' ')
                {
                    res = new int[acc + 1];
                    res[0] = acc;
                    break;
                }
                acc = acc * 10 + (line[i] - '0');
            }

            var resIdx = 1;
            acc = 0;
            i++;
            for (; i < len; i++)
            {
                var c = line[i];
                if (c == ' ')
                {
                    res[resIdx++] = acc;
                    acc = 0;
                }
                else
                {
                    acc = acc * 10 + (c - '0');
                }
            }
            res[resIdx++] = acc;

            return res;
        }

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var uv = ReadPairFromLine();
                var u = uv[0];
                var v = uv[1];

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            var nodeWithMaxPath = queryVertices[1];
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, queryVertices[i]))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();

            var c = path.Count - 1;
            for (int i = 0; i < c; i++)
            {
                pathCache[path[i]] = path.Take(i + 1).ToList();
            }
            pathCache[node] = path;

            return path;
        }

        static bool CloseEnough(List<int> deepPath, int node)
        {
            return node == 1 || g[node].parent == 1
                ? true
                : g[node].parent == deepPath[g[node].pathLength - 2];
        }

        static void Main(string[] args)
        {
            var nm = ReadPairFromLine();
            ReadGraph(nm[0]);

            var m = nm[1];
            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].pathLength - g[x].pathLength;
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            Array.Sort(queryVertices, 1, queryVertices[0], new EntryPathLengthComparer_Desc());

            var deepestVerticePath = GetPathFor(queryVertices[1]);
            var k1 = queryVertices[0] - 1;
            var ok = true;

            for (int i = k1; i >= 1
                && (ok = CloseEnough(deepestVerticePath, GetPathFor(queryVertices[1 + i])))
                ; i--)
            { }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node) {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            var c1 = shortPath.Count - 1;
            for (int i = 0; i < c1; i++)
            {
                if (deepPath[i] != shortPath[i])
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children;
            public List<int> path;
        }

        static Dictionary<int, Entry> g = new Dictionary<int, Entry>();

        static void ReadGraph(int numEdges)
        {
            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();
                if (g.ContainsKey(u))
                {
                    g[u].children.Add(v);
                }
                else
                {
                    g[u] = new Entry { children = new List<int> { v }, path = new List<int>() };
                }

                if (g.ContainsKey(v))
                {
                    g[v].children.Add(u);
                }
                else
                {
                    g[v] = new Entry { children = new List<int> { u }, path = new List<int>() };
                }
            }

            var s = new Stack<int?>();
            g[1].path.Add(1);
            foreach (var childNo in g[1].children)
            {
                s.Push(childNo);
            }
            var path = new List<int> { 1 };

            while (s.Count > 0)
            {
                var item = s.Pop();
                if (item == null)
                {
                    path.RemoveAt(path.Count - 1);
                    continue;
                }

                var val = item.Value;
                var entry = g[val];

                entry.children.Remove(path[path.Count - 1]);

                path.Add(val);
                entry.path.AddRange(path);

                s.Push(null);
                foreach (var childNo in entry.children)
                {
                    s.Push(childNo);
                }
            }

        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].path.Count - g[x].path.Count;
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            //var sortedByLengthInPlace
            Array.Sort(queryVertices, 1, queryVertices[0], new EntryPathLengthComparer_Desc());

            //
            //var sortedByLength =
            //    queryVertices
            //    .Skip(1)
            //    .OrderByDescending(v => g[v].path.Count)
            //    .ToArray();

            var deepestVerticePath = g[queryVertices[1]].path;
            var k1 = queryVertices[0] - 1;
            var ok = true;

            for (int i = k1; i >= 1
                && (ok = CloseEnough(deepestVerticePath, g[queryVertices[1 + i]].path))
                ; i--)
            { }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            var c1 = shortPath.Count - 1;
            for (int i = 0; i < c1; i++)
            {
                if (deepPath[i] != shortPath[i])
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n - 1);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E_2
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children = new List<int>();
            public int parent;
            public int pathLength;
        }

        static Entry[] g;

        static void ReadGraph(int n)
        {
            g = new Entry[n + 1];  // index 0 is unused
            var numEdges = n - 1;

            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();

                g[u] = g[u] ?? new Entry();
                g[u].children.Add(v);

                g[v] = g[v] ?? new Entry();
                g[v].children.Add(u);
            }

            g[1].pathLength = 1;

            var s = new Stack<(int, int)>();
            s.Push((1, 2));

            while (s.Count > 0)
            {
                var (curParentIdx, curPathLength) = s.Pop();
                foreach (var childIdx in g[curParentIdx].children)
                {
                    var child = g[childIdx];
                    child.parent = curParentIdx;
                    child.children.Remove(curParentIdx);
                    child.pathLength = curPathLength;
                    s.Push((childIdx, curPathLength + 1));
                }
            }
        }

        class EntryPathLengthComparer_Desc : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return g[y].pathLength - g[x].pathLength;
            }
        }

        static void HandleQuery(int[] queryVertices)
        {
            var len = queryVertices.Length;
            if (len == 2) { Console.WriteLine("YES"); return; }

            var nodeWithMaxPath = 1;
            var maxPathLength = g[nodeWithMaxPath].pathLength;

            for (int i = 2; i < len; i++)
            {
                var pl = g[queryVertices[i]].pathLength;
                if (pl > maxPathLength)
                {
                    maxPathLength = pl;
                    nodeWithMaxPath = queryVertices[i];
                }
            }
            var deepestVerticePath = GetPathFor(nodeWithMaxPath);
            var ok = true;

            for (int i = 1; ok && i < len; i++)
            {
                if (queryVertices[i] != nodeWithMaxPath &&
                    !CloseEnough(deepestVerticePath, GetPathFor(queryVertices[i])))
                {
                    ok = false;
                }
            }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static Dictionary<int, List<int>> pathCache = new Dictionary<int, List<int>>();

        static List<int> GetPathFor(int node)
        {
            if (pathCache.ContainsKey(node))
            {
                return pathCache[node];
            }
            var path = new List<int>();
            do
            {
                path.Add(node);
                node = g[node].parent;
            }
            while (node != 0);

            path.Reverse();
            pathCache[node] = path;
            return path;
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            return shortPath.Count == 1
                ? true
                : shortPath[shortPath.Count - 2] == deepPath[shortPath.Count - 2];
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces_Round_629__Div._3_
{
    class E
    {
        static (int, int) ReadPairFromLine()
        {
            var data = Console
                .ReadLine()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            return (data[0], data[1]);
        }

        static int[] ReadQueryFromLine() =>
                Console
                    .ReadLine()
                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(int.Parse)
                    .ToArray();

        class Entry
        {
            public List<int> children;
            public List<int> path;
        }

        static Dictionary<int, Entry> g = new Dictionary<int, Entry>();

        static void ReadGraph(int numEdges)
        {
            for (int i = 0; i < numEdges; i++)
            {
                var (u, v) = ReadPairFromLine();
                if (g.ContainsKey(u))
                {
                    g[u].children.Add(v);
                }
                else
                {
                    g[u] = new Entry { children = new List<int> { v }, path = new List<int>() };
                }

                if (g.ContainsKey(v))
                {
                    g[v].children.Add(u);
                }
                else
                {
                    g[v] = new Entry { children = new List<int> { u }, path = new List<int>() };
                }
            }

            var s = new Stack<int?>();
            g[1].path.Add(1);
            foreach (var childNo in g[1].children)
            {
                s.Push(childNo);
            }
            var path = new List<int> { 1 };

            while (s.Count > 0)
            {
                var item = s.Pop();
                if (item == null)
                {
                    path.RemoveAt(path.Count - 1);
                    continue;
                }

                var val = item.Value;
                var entry = g[val];

                entry.children.Remove(path[path.Count - 1]);

                path.Add(val);
                entry.path.AddRange(path);

                s.Push(null);
                foreach (var childNo in entry.children)
                {
                    s.Push(childNo);
                }
            }

        }

        static void HandleQuery(int[] queryVertices)
        {
            var sortedByLength =
                queryVertices
                .Skip(1)
                .OrderByDescending(v => g[v].path.Count)
                .ToArray();

            var deepestVerticePath = g[sortedByLength[0]].path;
            var bound = sortedByLength.Length;
            var ok = true;

            for (int i = 1; i < bound
                && (ok = CloseEnough(deepestVerticePath, g[sortedByLength[i]].path))
                ; i++)
            { }
            Console.WriteLine(ok ? "YES" : "NO");
        }

        static bool CloseEnough(List<int> deepPath, List<int> shortPath)
        {
            var c1 = shortPath.Count - 1;
            for (int i = 0; i < c1; i++)
            {
                if (deepPath[i] != shortPath[i])
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            var (n, m) = ReadPairFromLine();
            ReadGraph(n - 1);

            for (int i = 0; i < m; i++)
            {
                HandleQuery(ReadQueryFromLine());
            }

            //Console.WriteLine("END");
            //Console.ReadLine();
        }
    }
}