// Does a BFS on the pairs of locations to find a shortest difference // Too slow because it is O(n^2) #include #include #include #include #include using namespace std; struct node { int left, right; bool tower; }; int main(){ int n,a,b; cin >> n >> a >> b; vector graph(n); for(auto & n : graph) { cin >> n.left >> n.right >> n.tower; } // ((A, B), distance) queue, int>> work; set> visited; work.push({{a, b}, 0}); while(!work.empty()){ const auto p = work.front(); work.pop(); if(visited.count(p.first) > 0) continue; const auto d = p.second; const auto & a = graph[p.first.first]; const auto & b = graph[p.first.second]; if(a.tower != b.tower) { cout << d << endl; return 0; } visited.insert(p.first); // add next states work.push({{a.left, b.left}, d+1}); work.push({{a.right, b.right}, d+1}); } cout << "indistinguishable" << endl; }