#include #include #include #include using namespace std; typedef map > > silenost; /* bool recu(silenost &theMap, const string &cur, set used, string orig_cur, pair ratio) { if (cur == orig_cur) { if (ratio.first==ratio.second) { return false; } else { return true; } } if (orig_cur == "") orig_cur = cur; if (used.find(cur) != used.end()) return false; used.insert(cur); //bool ret = false; for (map >::iterator i = theMap[cur].begin(); i!=theMap[cur].end(); i++) { pair rat2 = make_pair(ratio.first*i->second.first,ratio.second*i->second.second); if(recu(theMap, i->first, used, orig_cur, rat2)) { return true; } } return false; } */ bool recu2(silenost &theMap, const string &cur, set &used, string orig_cur, double ratio) { if (cur == orig_cur) { if (ratio>=1) { return false; } else { return true; } } if (orig_cur == "") orig_cur = cur; if (used.find(cur) != used.end()) return false; used.insert(cur); //bool ret = false; for (map >::iterator i = theMap[cur].begin(); i!=theMap[cur].end(); i++) { double rat2 = ratio * i->second.first/i->second.second; if(recu2(theMap, i->first, used, orig_cur, rat2)) { used.erase(cur); return true; } } used.erase(cur); return false; } int main() { while (cin) { int mcurs; cin >> mcurs; if (mcurs == 0) break; set curs; for (int i = 0; i < mcurs; i++) { string cur; cin >> cur; curs.insert(cur); } int lines; cin >> lines; silenost theMap; for (int i = 0; i < lines; i++) { string cur1, cur2; int rate1, rate2; char t; cin >> cur1 >> cur2 >> rate1 >> t >> rate2; //cout << rate1 << " " << rate2 << endl; theMap[cur1][cur2] = make_pair(rate1, rate2); } bool priznak = false; for (set::iterator i=curs.begin(); i!=curs.end(); i++) { set used; //cout << i->first << endl; if (recu2(theMap, *i, used, "", 1)) { cout << "Arbitrage" << endl; priznak = true; break; } } if (!priznak) cout << "Ok" << endl; } return 0; }