#include #include #include inline void fastRead_int(long long int *number) { int chr = getchar_unlocked(); bool negate = false; while((chr < 48 || chr > 57) && chr != '-') { chr = getchar_unlocked(); } *number = 0; if(chr == '-') { negate = true; chr = getchar_unlocked(); } while(chr > 47 && chr < 58) { *number = (*number) * 10 + chr - 48; chr = getchar_unlocked(); } if(negate) { *number=-(*number); } } class Candidate; class Town { public: int id; std::vector neighbours; Candidate* winB; Candidate* winA; Town(int tid) { winA = NULL; winB = NULL; id = tid; } }; class Candidate { public: int id; int campaignA; int campaignB; Candidate(int cid) { campaignA = 0; campaignB = 0; id = cid; } }; int main(int ac, char** av){ int townsListCount, candidCount, startTown, neigh1, neigh2, i; std::map voteNow, voteNext; std::map neighCountA, neighCountB; std::map nextWinA, nextWinB; fscanf(stdin, "%d", &townsListCount); Town* townsList[townsListCount]; for(i = 0; i < townsListCount; ++i) { townsList[i] = new Town(i); } fscanf(stdin, "%d", &neigh1); fscanf(stdin, "%d", &neigh2); while(neigh1 != -1) { townsList[neigh1]->neighbours.push_back(townsList[neigh2]); townsList[neigh2]->neighbours.push_back(townsList[neigh1]); fscanf(stdin, "%d", &neigh1); fscanf(stdin, "%d", &neigh2); } fscanf(stdin, "%d", &candidCount); Candidate* candids[candidCount]; for(i = 0; i < candidCount; ++i) { fscanf(stdin, "%d", &startTown); Candidate* candid = new Candidate(i); townsList[startTown]->winA = candid; townsList[startTown]->winB = candid; candid->campaignA++; candid->campaignB++; candids[i] = candid; for(std::vector::iterator it = townsList[startTown]->neighbours.begin(); it != townsList[startTown]->neighbours.end(); ++it) { voteNext[*it] = ' '; } } while(voteNext.size() > 0) { nextWinA.clear(); nextWinB.clear(); voteNow = voteNext; voteNext.clear(); for(std::map::iterator it = voteNow.begin(); it != voteNow.end(); ++it) { Town* town = it->first; if(town->winA == NULL || town->winB == NULL) { neighCountA.clear(); neighCountB.clear(); Town* neighbour; for(std::vector::iterator it = town->neighbours.begin(); it != town->neighbours.end(); ++it) { neighbour = *it; if(!neighbour->winA && !neighbour->winB) { voteNext[neighbour] = ' '; } if(neighbour->winA) { neighCountA[neighbour->winA]++; } if(neighbour->winB) { neighCountB[neighbour->winB]++; } } int minV = 1000000; Candidate* minC = NULL; for(std::map::iterator it = neighCountA.begin(); it != neighCountA.end(); ++it) { if((it->second == minV && it->first->id > minC->id) || it->second < minV) { minV = it->second; minC = it->first; } } nextWinA[town] = minC; int maxV = 0; Candidate* maxC = NULL; for(std::map::iterator it = neighCountB.begin(); it != neighCountB.end(); ++it) { if((it->second == maxV && it->first->id < maxC->id) || it->second > maxV) { maxV = it->second; maxC = it->first; } } nextWinB[town] = maxC; } } voteNow.clear(); for(std::map::iterator it = nextWinA.begin(); it != nextWinA.end(); ++it) { it->first->winA = it->second; it->second->campaignA++; } for(std::map::iterator it = nextWinB.begin(); it != nextWinB.end(); ++it) { it->first->winB = it->second; it->second->campaignB++; } } for(i = 0; i < candidCount; ++i) { printf("%d %d\n",candids[i]->campaignA, candids[i]->campaignB); } }