namespace AtCoder; #nullable enable using System.Numerics; class Graph { public struct Edge { public int Ab { get; set; } public int Ad { get; set; } public T Distance { get; set; } } public int N { get; private init; } public bool Directed { get; private init; } public List> AdjacencyList { get; private init; } public List Edges { get; private init; } public Graph(int verticals, IReadOnlyList edges, bool directed) { N = verticals; Directed = directed; AdjacencyList = new List>(); for (var i = 0; i < N; i++) AdjacencyList.Add(new List<(int, int)>()); Edges = new List(); for (var i = 0; i < edges.Count; i++) { var edge = edges[i]; Edges.Add(edge); AdjacencyList[edge.Ab].Add((edge.Ad, i)); if (!Directed) AdjacencyList[edge.Ad].Add((edge.Ab, i)); } } } class GraphBuilder { public static Graph SimpleGraph(int verticals, (int ab, int ad)[] edges, bool directed) { var e = edges.Select(e => new Graph.Edge { Ab = e.ab, Ad = e.ad, Distance = 1 }).ToList(); return new(verticals, e, directed); } public static Graph Graph(int verticals, (int ab, int ad, T edge)[] edges, bool directed) { var e = edges.Select(e => new Graph.Edge { Ab = e.ab, Ad = e.ad, Distance = e.edge }).ToList(); return new(verticals, e, directed); } } class GraphBase { public required Graph Graph { get; init; } } class DirectedGraph : GraphBase { public static DirectedGraph? TryConstruct(Graph graph) { if (!graph.Directed) return null; return new DirectedGraph { Graph = graph }; } } static class DirectedGraphExtensions { public static int[] StronglyConnectedComponents(this DirectedGraph graph) { var Graph = graph.Graph; var n = Graph.N; var orders = new int[n]; var lowLinks = new int[n]; var roots = new int[n]; for (var i = 0; i < n; i++) (orders[i], lowLinks[i], roots[i]) = (n, n, n); var order = 0; bool Visited(int v) => orders[v] != n; var stack = new Stack(); void S(int v) { orders[v] = lowLinks[v] = order++; stack.Push(v); foreach (var (next, _) in Graph.AdjacencyList[v]) { if (!Visited(next)) { S(next); lowLinks[v] = Math.Min(lowLinks[v], lowLinks[next]); } else if (roots[next] == n) lowLinks[v] = Math.Min(lowLinks[v], orders[next]); } if (orders[v] == lowLinks[v]) { var u = v; while ((u = stack.Pop()) != v) roots[u] = v; roots[v] = v; } } for (var i = 0; i < n; i++) if (!Visited(i)) S(i); return roots; } } class DynamicDirectedGraph { List<(int Ab, int Ad, bool Deleted)> Edges { get; init; } = new(); public HashSet[] AI { get; init; } public HashSet[] AO { get; init; } public List> DI { get; init; } public List> DO { get; init; } public DynamicDirectedGraph(int n) { { var a = new HashSet[n]; for (var i = 0; i < n; i++) a[i] = new(); AI = a; } { var a = new HashSet[n]; for (var i = 0; i < n; i++) a[i] = new(); AO = a; } { var s = new HashSet(); for (var j = 0; j < n; j++) s.Add(j); var d = new List>{ s }; DI = d; } { var s = new HashSet(); for (var j = 0; j < n; j++) s.Add(j); var d = new List>{ s }; DO = d; } } public int AddEdge(int ab, int ad) { if (ab == ad || AO[ab].Contains(ad)) return -1; Edges.Add((ab, ad, false)); var e = Edges.Count - 1; UCI(ad, 1); AI[ad].Add(e); UCO(ab, 1); AO[ab].Add(e); return e; } public void RemoveEdge(int edge) { var (ab, ad, deleted) = Edges[edge]; if (deleted) return; Edges[edge] = (ab, ad, true); UCI(ad, -1); AI[ad].Remove(edge); UCO(ab, -1); AO[ab].Remove(edge); } public void RemoveNode(int node) { var (ez, ai, ao) = (Edges, AI, AO); var (ei, eo) = (ai[node], ao[node]); var (ci, co) = (ei.Count, eo.Count); foreach (var e in ei) { var ab = ez[e].Ab; UCO(ab, -1); ao[ab].Remove(e); ez[e] = (ab, node, true); } foreach (var e in eo) { var ad = ez[e].Ad; UCI(ad, -1); ai[ad].Remove(e); ez[e] = (node, ad, true); } UCI(node, -ci); UCO(node, -co); } static void UD(HashSet[] a, List> d, int v, int diff) { var current = a[v].Count; d[current].Remove(v); while (current + diff >= d.Count) d.Add(new()); d[current + diff].Add(v); } void UCI(int v, int diff) => UD(AI, DI, v, diff); void UCO(int v, int diff) => UD(AO, DO, v, diff); } static class Extensions { public static T[] Repeat(this int time, Func F) => Enumerable.Range(0, time).Select(_ => F()).ToArray(); } class AtCoder { object? Solve() { var n = Int(); var edges = new List<(int, int)>(); for (var i = 0; i < n; i++) { var str = Console.ReadLine()!.Trim().Split(' '); var m = int.Parse(str[0]); for (var j = 1; j <= m; j++) { var a = int.Parse(str[j]) - 1; edges.Add((i, a)); } } var g = GraphBuilder.SimpleGraph(n, edges.ToArray(), true); var scc = DirectedGraph.TryConstruct(g)!.StronglyConnectedComponents(); var dg = new DynamicDirectedGraph(n); foreach (var (u, v) in edges) { var ab = scc[u]; var ad = scc[v]; dg.AddEdge(ab, ad); } for (var i = 0; i < n; i++) if (scc[i] != i) dg.DO[0].Remove(i); while (true) { if (dg.DO[0].Count == 0) return true; if (dg.DO[0].Count > 1) return false; var v = dg.DO[0].First(); dg.RemoveNode(v); dg.DO[0].Remove(v); if (v == scc[0]) break; } return dg.DO[0].Count == 0; } public static void Main() => new AtCoder().Run(); public void Run() { var res = Solve(); if (res != null) { if (res is bool yes) res = yes ? "Yes" : "No"; sw.WriteLine(res); } sw.Flush(); } string[] input = Array.Empty(); int iter = 0; readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false }; string String() { while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0); return input[iter++]; } T Input() where T : IParsable => T.Parse(String(), null); int Int() => Input(); void Out(object? x, string? separator = null) { separator ??= Environment.NewLine; if (x is System.Collections.IEnumerable obj and not string) { var firstLine = true; foreach (var item in obj) { if (!firstLine) sw.Write(separator); firstLine = false; sw.Write(item); } } else sw.Write(x); sw.WriteLine(); } }