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 = 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); } }