#include using namespace std; struct empire{ //indexes of empires vector child; vector provinces; }; struct action{ int year, first, second; char type; bool operator < (action& other){ return year < other.year; } }; int main(){ ios_base::sync_with_stdio(false); int r, p, n; cin >> r >> p >> n; vector empires(r); vector> provinces(p, {-1, -1}); vector actions(n); for(int i = 0; i < n; i++){ int year; char type; cin >> year >> type; if(type == 'O'){ int prov, emp; cin >> prov >> emp; actions[i] = {year, prov - 1, emp - 1, type}; }else if(type == 'K'){ int prov; cin >> prov; actions[i] = {year, prov - 1, -1, type}; }else if(type == 'P'){ int win, lose; cin >> win >> lose; actions[i] = {year, win - 1, lose - 1, type}; }else if(type == 'Z'){ int emp; cin >> emp; actions[i] = {year, emp - 1, -1, type}; }else{ throw exception(); } } sort(actions.begin(), actions.end()); for(int i = 0; i < n; i++){ int year = actions[i].year; char type = actions[i].type; if(type == 'O'){ int prov = actions[i].first, emp = actions[i].second; empires[emp].provinces.push_back(prov); provinces[prov].first = year; }else if(type == 'K'){ int prov = actions[i].first; provinces[prov].second = year; }else if(type == 'P'){ int win = actions[i].first, lose = actions[i].second; empires[win].child.push_back(lose); }else if(type == 'Z'){ int emp = actions[i].first; queue que; que.push(emp); while(!que.empty()){ empire e = empires[que.front()]; que.pop(); for(auto child : e.child) que.push(child); for(auto prov : e.provinces){ if(provinces[prov].second == -1) provinces[prov].second = year; } } } } int mx = -1, bestProv = -1; for(int i = 0; i < p; i++){ int length = provinces[i].second - provinces[i].first; if(length > mx){ mx = length; bestProv = i; } } cout << bestProv + 1 << " " << mx << "\n"; }