// https://pydefis.callicode.fr/defis/AmisEwoks/txt #include #include #include #include #include #include using namespace std; const int Infini = numeric_limits::max(); const int N = 8; // 8-amis map> adj; // liste d'adjacences map intToString; map stringToInt; void print() { for(auto &elt: adj) { cout << elt.first << " : "; for(auto &ami: elt.second) cout << ami << ", "; cout << "\b\b \n"; } } // print() // https://www.geeksforgeeks.org/floyd-warshall-algorithm-dp-16/ void floydWarshall(vector> &mat) { int n = mat.size(); for(int k = 0; k < n; k++) for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(mat[i][k] != Infini && mat[k][j] != Infini && mat[i][j] > mat[i][k] + mat[k][j]) mat[i][j] = mat[i][k] + mat[k][j]; } // floydWarshall() int main() { string s, t; while(cin >> s >> t) { adj[s].insert(t); adj[t].insert(s); } int i = 0; for(auto &elt: adj) { intToString[i] = elt.first; stringToInt[elt.first] = i; ++i; } //print(); // matrice d'adjacences auto mat = vector>(adj.size(), vector(adj.size(), Infini)); for(size_t i = 0; i < adj.size(); i++) mat[i][i] = 0; for(auto &elt: adj) { int ind1 = stringToInt[elt.first]; for(auto &ami: elt.second) { int ind2 = stringToInt[ami]; mat[ind1][ind2] = mat[ind2][ind1] = 1; } } floydWarshall(mat); // couples N-amis ? for(size_t i = 0; i < mat.size(); i++) for(size_t j = i + 1; j < mat.size(); j++) if(mat[i][j] == N) // i et j sont N-amis { cout << intToString[i] << ", " << intToString[j] << '\n'; } return 0; } // main()