public void Solve() { int N = Reader.Int(), M = Reader.Int(); var E = Reader.IntTable(M); int NQ = Reader.Int(); var Q = Reader.IntTable(NQ); var comp = new TwoEdgeConnectedComponent(N, E, 1); var hld = new HLDecomposition(comp.Components.Count); for (int a = 0; a < comp.Components.Count; a++) { foreach (int b in comp.Components[a].Edges) { if (a < b) { hld.AddEdge(a, b); } } } hld.Build(); var ans = new bool[NQ]; for (int i = 0; i < NQ; i++) { int a = comp.VtoComponentId(Q[i][0] - 1); int b = comp.VtoComponentId(Q[i][1] - 1); int c = comp.VtoComponentId(Q[i][2] - 1); ans[i] = a == b && a == c || hld.Dist(a, b) + hld.Dist(b, c) == hld.Dist(a, c); } Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG"))); }
void Test() { int N = (int)1e5; int M = N - 1; // (int)2e5; var E = new int[M][]; int ei = 0; for (int i = 0; i < M; i++) { E[i] = new int[] { i + 1, i + 2 }; } int NQ = (int)1e5; var Q = new int[NQ][]; var random = new Random(0); for (int i = 0; i < NQ; i++) { Q[i] = new[] { random.Next(N) + 1, random.Next(N) + 1, random.Next(N) + 1 } } ; var sw = Stopwatch.StartNew(); var comp = new TwoEdgeConnectedComponent(N, E, 1); Console.WriteLine("TwoEdge: " + sw.ElapsedMilliseconds); sw.Restart(); var hld = new HLDecomposition(comp.Count); for (int a = 0; a < comp.Count; a++) { foreach (int b in comp[a].Edges) { if (a < b) { hld.AddEdge(a, b); } } } hld.Build(); Console.WriteLine("HLD: " + sw.ElapsedMilliseconds); sw.Restart(); var ans = new bool[NQ]; for (int i = 0; i < NQ; i++) { int a = comp.VtoComponentId(Q[i][0] - 1); int b = comp.VtoComponentId(Q[i][1] - 1); int c = comp.VtoComponentId(Q[i][2] - 1); ans[i] = a == b && a == c || hld.Dist(a, b) + hld.Dist(b, c) == hld.Dist(a, c); } Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } }
public void Solve() { int N = Reader.Int(); var E = Reader.IntTable(N - 1); int NQ = Reader.Int(); var Q = Reader.IntTable(NQ); var g = new HLDecomposition(N, E, 1); var ans = new StringBuilder(); foreach (var q in Q) { int a = q[0] - 1, b = q[1] - 1; int len = 1 + g.Dist(a, b); ans.AppendLine(len + ""); } Console.Write(ans); }
public void Solve() { int N = NextInt(), M = NextInt(); var E = new int[M][]; for (int i = 0; i < M; i++) { E[i] = new[] { NextInt(), NextInt() } } ; int NQ = NextInt(); var Q = new int[NQ][]; for (int i = 0; i < NQ; i++) { Q[i] = new[] { NextInt(), NextInt(), NextInt() } } ; var comp = new TwoEdgeConnectedComponent(N, E, 1); var hld = new HLDecomposition(comp.Components.Count); for (int a = 0; a < comp.Components.Count; a++) { foreach (int b in comp.Components[a].Edges) { if (a < b) { hld.AddEdge(a, b); } } } hld.Build(); var ans = new bool[NQ]; for (int i = 0; i < NQ; i++) { int a = comp.VtoComponentId(Q[i][0] - 1); int b = comp.VtoComponentId(Q[i][1] - 1); int c = comp.VtoComponentId(Q[i][2] - 1); ans[i] = a == b && a == c || hld.Dist(a, b) + hld.Dist(b, c) == hld.Dist(a, c); } Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG"))); } TextReader _reader = Console.In; int NextInt() { int c; while ((c = _reader.Read()) < '0' || c > '9') { } int res = c - '0'; while ((c = _reader.Read()) >= '0' && c <= '9') { res = res * 10 + c - '0'; } return(res); } }