#include #include #include #include class Candidate { public: int id; int costA; int costB; Candidate(int candidate_id) { id = candidate_id; costA = 1; costB = 1; } }; class Town { public: int id; std::vector neighbours_list; Candidate* winnerA; Candidate* winnerB; Town(int townId) { id = townId; winnerA = NULL; winnerB = NULL; } }; class NextWinners { public: Town* town; Candidate* winnerA; Candidate* winnerB; }; int main(int argc, char *argv[]) { // fast std::cin, declare in main() std::ios::sync_with_stdio(false); int towns_count = 0; int candidates_count = 0; int town_1 = 0; int town_2 = 0; int start_town_id = 0; Town* current_town = NULL; Town* current_neighbour = NULL; NextWinners* next_winners = NULL; std::map current_round_towns; std::map next_round_towns; std::vector next_winners_list; std::map candidate_votes_A; std::map candidate_votes_B; std::cin >> towns_count; Town* towns_list[towns_count]; for(int town_id = 0; town_id < towns_count; town_id++) towns_list[town_id] = new Town(town_id); std::cin >> town_1; std::cin >> town_2; while(town_1 != -1 && town_2 != -1) { towns_list[town_1]->neighbours_list.push_back(towns_list[town_2]); towns_list[town_2]->neighbours_list.push_back(towns_list[town_1]); std::cin >> town_1; std::cin >> town_2; } std::cin >> candidates_count; Candidate* candidates_list[candidates_count]; for(int candidate_id = 0; candidate_id < candidates_count; candidate_id++) { std::cin >> start_town_id; candidates_list[candidate_id] = new Candidate(candidate_id); towns_list[start_town_id]->winnerA = candidates_list[candidate_id]; towns_list[start_town_id]->winnerB = candidates_list[candidate_id]; for(std::vector::iterator it = towns_list[start_town_id]->neighbours_list.begin(); it != towns_list[start_town_id]->neighbours_list.end(); it++) next_round_towns.insert(std::pair(*it, *it)); } while(!next_round_towns.empty()) { current_round_towns.clear(); current_round_towns = next_round_towns; next_round_towns.clear(); for(std::map::iterator it = current_round_towns.begin(); it != current_round_towns.end(); it++) { current_town = it->first; if(current_town->winnerA == NULL && current_town->winnerB == NULL) { candidate_votes_A.clear(); candidate_votes_B.clear(); for(std::vector::iterator it = current_town->neighbours_list.begin(); it != current_town->neighbours_list.end(); it++) { current_neighbour = *it; if(current_neighbour->winnerA) { candidate_votes_A[current_neighbour->winnerA]++; candidate_votes_B[current_neighbour->winnerB]++; } else next_round_towns.insert(std::pair(current_neighbour, current_neighbour)); } next_winners = new NextWinners(); next_winners->town = current_town; int best_candidate_votes_A = 2147483647; Candidate* best_candidate_A; for(std::map::iterator it=candidate_votes_A.begin(); it!=candidate_votes_A.end(); it++) if(it->second < best_candidate_votes_A || (it->second == best_candidate_votes_A && it->first->id > best_candidate_A->id)) { best_candidate_A = it->first; best_candidate_votes_A = it->second; } next_winners->winnerA = best_candidate_A; best_candidate_A->costA++; int best_candidate_votes_B = 0; Candidate* best_candidate_B; for(std::map::iterator it=candidate_votes_B.begin(); it!=candidate_votes_B.end(); ++it) if(it->second > best_candidate_votes_B || (it->second == best_candidate_votes_B && it->first->id < best_candidate_B->id)) { best_candidate_B = it->first; best_candidate_votes_B = it->second; } next_winners->winnerB = best_candidate_B; best_candidate_B->costB++; next_winners_list.push_back(next_winners); } } for(std::vector::iterator it=next_winners_list.begin(); it!=next_winners_list.end(); ++it) { (*it)->town->winnerA = (*it)->winnerA; (*it)->town->winnerB = (*it)->winnerB; } next_winners_list.clear(); } for(int i = 0; i < candidates_count; i++) { std::cout << candidates_list[i]->costA; std::cout << " "; std::cout << candidates_list[i]->costB; std::cout << "\n"; } }