public static void Main() { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); int n = NextInt; int q = NextInt; DSU dsu = new DSU(n); for (int i = 0; i < q; i++) { var t = NextInt; var u = NextInt; var v = NextInt; if (t == 0) { dsu.Merge(u, v); } if (t == 1) { Console.WriteLine(dsu.Same(u, v) ? "1" : "0"); } } Console.Out.Flush(); }
public static void Main() { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); int n = NextInt; int q = NextInt; UnionFind uf = new UnionFind(n); DSU dsu = new DSU(n); void VerifyGroup() { var groups = dsu.Groups().ToArray().Select(x => x.OrderBy(x => x).ToArray()).OrderBy(x => x.First()).ToArray(); var ans = Enumerable.Range(0, n).GroupBy(uf.Find).Select(x => x.OrderBy(x => x).ToArray()).OrderBy(x => x.First()).ToArray(); if (groups.Length != ans.Length) { throw new Exception(); } if (groups.Zip(ans, (x, y) => x.SequenceEqual(y)).Any(x => !x)) { throw new Exception(); } } for (int i = 0; i < q; i++) { var(t, u, v) = (NextInt, NextInt, NextInt); if (t == 0) { dsu.Merge(u, v); uf.TryUnite(u, v); } else { Console.WriteLine(dsu.Same(u, v) ? "1" : "0"); } if ((i & -i) == 0) { VerifyGroup(); } } VerifyGroup(); Console.Out.Flush(); }
public override IEnumerable <object> Solve(TextReader inputStream) { var(n, queries) = inputStream.ReadValue <int, int>(); var dsu = new DSU(n); for (int q = 0; q < queries; q++) { var(t, u, v) = inputStream.ReadValue <int, int, int>(); if (t == 0) { dsu.Merge(u, v); } else { yield return(dsu.Same(u, v) ? 1 : 0); } } }
public void Solve(ConsoleReader cr, ConsoleWriter cw) { int n = cr; int q = cr; var dsu = new DSU(n); for (int i = 0; i < q; i++) { int t = cr; int u = cr; int v = cr; if (t == 0) { dsu.Merge(u, v); } else { cw.WriteLine(dsu.Same(u, v) ? 1 : 0); } } }