#include #include #include #include class Candidate { public: int id; int cost_A; int cost_B; Candidate(int candidateId) { id = candidateId; cost_A = 1; // start town cost cost_B = 1; } }; class Town { public: int id; std::vector neighbours; Candidate* winner_A; Candidate* winner_B; Town(int townId) { id = townId; winner_A = NULL; winner_B = NULL; } }; int main() { std::ios::sync_with_stdio(false); int towns_count = 0; int candidates_count = 0; int neighbour_id_1 = 0; int neighbour_id_2 = 0; int candidate_start_town_id = 0; int max_value = 0; Town* current_town = NULL; Town* current_neighbour = NULL; Candidate* max_candidate = NULL; std::map* current_round_towns = new std::map(); std::map* next_round_towns = new std::map(); std::map next_round_winner_A; std::map next_round_winner_B; std::map votes_A; std::map votes_B; Town** towns = NULL; Candidate** candidates = NULL; std::cin >> towns_count; towns = new Town*[towns_count]; for(register int i = 0; i < towns_count; i++) { towns[i] = new Town(i); } std::cin >> neighbour_id_1; std::cin >> neighbour_id_2; while(neighbour_id_1 != -1) { towns[neighbour_id_1]->neighbours.push_back(towns[neighbour_id_2]); towns[neighbour_id_2]->neighbours.push_back(towns[neighbour_id_1]); std::cin >> neighbour_id_1; std::cin >> neighbour_id_2; } std::cin >> candidates_count; candidates = new Candidate*[candidates_count]; for(register int i = 0; i < candidates_count; i++) { std::cin >> candidate_start_town_id; for(std::vector::iterator it = towns[candidate_start_town_id]->neighbours.begin(); it != towns[candidate_start_town_id]->neighbours.end(); it++) { (*next_round_towns)[*it] = '0'; } candidates[i] = new Candidate(i); towns[candidate_start_town_id]->winner_A = candidates[i]; towns[candidate_start_town_id]->winner_B = candidates[i]; } while(!next_round_towns->empty()) { delete current_round_towns; current_round_towns = next_round_towns; next_round_towns = new std::map(); for(std::map::iterator it = current_round_towns->begin(); it != current_round_towns->end(); it++) { current_town = it->first; if(current_town->winner_A == NULL) { votes_A.clear(); votes_B.clear(); for(std::vector::iterator it = current_town->neighbours.begin(); it != current_town->neighbours.end(); it++) { current_neighbour = *it; if(current_neighbour->winner_A) { votes_A[current_neighbour->winner_A]++; votes_B[current_neighbour->winner_B]++; } else (*next_round_towns)[current_neighbour] = '0'; } max_value = 2000000000; for(std::map::iterator it = votes_A.begin(); it != votes_A.end(); it++) { if(it->second < max_value || (it->second == max_value && it->first->id > max_candidate->id)) { max_candidate = it->first; max_value = it->second; } } next_round_winner_A[current_town] = max_candidate; max_candidate->cost_A++; max_value = 0; for(std::map::iterator it = votes_B.begin(); it != votes_B.end(); it++) { if(it->second > max_value || (it->second == max_value && it->first->id < max_candidate->id)) { max_candidate = it->first; max_value = it->second; } } next_round_winner_B[current_town] = max_candidate; max_candidate->cost_B++; } } for(std::map::iterator it = next_round_winner_A.begin(); it != next_round_winner_A.end(); it++) { it->first->winner_A = it->second; } next_round_winner_A.clear(); for(std::map::iterator it = next_round_winner_B.begin(); it != next_round_winner_B.end(); it++) { it->first->winner_B = it->second; } next_round_winner_B.clear(); } for(register int i = 0; i < candidates_count; i++) { std::cout << candidates[i]->cost_A << " " << candidates[i]->cost_B << std::endl; } return 0; }