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 lca = new LowestCommonAncestor(comp.Components.Count); for (int a = 0; a < comp.Components.Count; a++) { foreach (int b in comp.Components[a].Edges) { if (a < b) { lca.AddEdge(a, b); } } } lca.Init(0); 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 || lca.Dist(a, b) + lca.Dist(b, c) == lca.Dist(a, c); } Console.WriteLine(string.Join("\n", ans.Select(b => b ? "OK" : "NG"))); }
public void Solve() { int N = Reader.Int(), A = Reader.Int() - 1, B = Reader.Int() - 1; var E = new List <int> [2][]; for (int who = 0; who < 2; who++) { E[who] = new List <int> [N]; for (int i = 0; i < N; i++) { E[who][i] = new List <int>(); } for (int i = 0; i < N - 1; i++) { int a = Reader.Int() - 1, b = Reader.Int() - 1; E[who][a].Add(b); E[who][b].Add(a); } } var lca = new LowestCommonAncestor(B, E[1]); var seen = new bool[N]; var que = new Queue <int>(); que.Enqueue(A); int ans = lca.Depth(A) * 2; for (int steps = 1; que.Count > 0; steps++) { var nextQue = new Queue <int>(); while (que.Count > 0) { int at = que.Dequeue(); foreach (int next in E[0][at]) { if (!seen[next]) { if (lca.Dist(at, next) > 2) { Console.WriteLine(-1); return; } seen[next] = true; int nextDepth = lca.Depth(next); if (nextDepth >= steps) { ans = Math.Max(ans, nextDepth * 2); } if (nextDepth > steps) { nextQue.Enqueue(next); } } } } que = nextQue; } Console.WriteLine(ans); }
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 LowestCommonAncestor(N, E, 0, 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.Append(len + "\n"); } 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 lca = new LowestCommonAncestor(comp.Components.Count); for (int a = 0; a < comp.Components.Count; a++) { foreach (int b in comp.Components[a].Edges) { if (a < b) { lca.AddEdge(a, b); } } } lca.Init(0); 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 || lca.Dist(a, b) + lca.Dist(b, c) == lca.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); } }