/// <summary> /// Perform a Quick Find on a small text file. /// </summary> /// <param name="unionFind">The union find to be tested.</param> internal static void CommonUnionFindTiny(IUnionFind unionFind) { using (In input = new In("Algs4-Data\\TinyUF.txt")) { int initialComponentCount = input.ReadInt(); unionFind.IsolateComponents(initialComponentCount); while (!input.IsEmpty()) { int siteP = input.ReadInt(); int siteQ = input.ReadInt(); if (unionFind.Connected(siteP, siteQ)) { continue; } unionFind.Union(siteP, siteQ); } } Assert.AreEqual(2, unionFind.Count); }
/// <summary> /// Common portion of file input UnionFind tests. /// </summary> /// <param name="streamName">The name for the input stream.</param> /// <param name="unionFind">The union find to be tested.</param> /// <returns>The Union Find structure.</returns> public static IUnionFind UnionFindCommon(string streamName, IUnionFind unionFind) { if (null == streamName) { throw new ArgumentNullException("streamName"); } if (null == unionFind) { throw new ArgumentNullException("unionFind"); } using (In input = new In(streamName)) { int initialComponentCount = input.ReadInt(); unionFind.IsolateComponents(initialComponentCount); while (!input.IsEmpty()) { int siteP = input.ReadInt(); int siteQ = input.ReadInt(); if (unionFind.Connected(siteP, siteQ)) { continue; } unionFind.Union(siteP, siteQ); } return unionFind; } }
public void BinarySearchTiny() { int[] expected = { 5, -1, 0, -1, 4, 5, 15, 14, 1, 0, 9, 13, -1, 10, 15, 13, 13, 12 }; int[] whiteList; // read the integers from a file and sort them using (In inWhiteList = new In("Algs4-Data\\TinyW.txt")) { whiteList = inWhiteList.ReadAllInts(); } Array.Sort(whiteList); // read integer keys from a file; search in whitelist using (In inKeys = new In("Algs4-Data\\TinyT.txt")) { int expectedIndex = 0; while (!inKeys.IsEmpty() && expected.Length > expectedIndex) { int key = inKeys.ReadInt(); int position = BinarySearch.Rank(key, whiteList); Assert.AreEqual(expected[expectedIndex++], position); } } }
static void Main(string[] args) { var opts = new SimpleInputOpt("Enter your age: ", "You must digit a valid number!\n"); int age = In.ReadInt(opts).Value; float height = In.ReadFloat(opts.SetMsg("Enter your height: ")).Value; Out.Println($"You are {age} years old and your height is {height}m!\n"); string[] words = In.ReadInput <string[]>(opts.SetMsg("Enter your favorite words: "), GetWords); Out.Println("\nYour favorite words are:"); foreach (var word in words) { Out.Println("- " + word.I(Red)); } }
public static void run(int testCaseNum) { N = In.ReadInt(); K = In.ReadInt(); adj = new List <int> [N]; exclude = new bool[N]; ans = 0; cnt = new int[K + 1]; toInsert = new List <int>(); toErase = new List <int>(); for (int i = 0; i <= K; i++) { cnt[K] = 0; } for (int i = 0; i < N; i++) { adj[i] = new List <int>(); exclude[i] = false; } for (int i = 0; i < N - 1; i++) { int a = In.ReadInt() - 1, b = In.ReadInt() - 1; adj[a].Add(b); adj[b].Add(a); } Queue <int> q = new Queue <int>(); q.Enqueue(0); while (q.Count() > 0) { int v = q.Dequeue(); int c = getCentroid(v, -1, getSize(v, -1)); toErase.Clear(); cnt[0] = 1; toErase.Add(0); foreach (int w in adj[c]) { if (exclude[w]) { continue; } toInsert.Clear(); dfs(w, c, 1); foreach (int d in toInsert) { cnt[d]++; toErase.Add(d); } } foreach (int d in toErase) { cnt[d] = 0; } exclude[c] = true; foreach (int w in adj[c]) { if (!exclude[w]) { q.Enqueue(w); } } } Out.WriteLine(ans); }