#include <iostream> #include <set> #include <vector> #include <algorithm> #define MAX_NUMBER_OF_CITIES 200000 #define MAX_NUMBER_OF_ROADS 200000 using namespace std; int numberOfCities; int numberOfRoads; int minNumberOfRoadsToQualify; bool visited [MAX_NUMBER_OF_CITIES + 1] = {}; vector <int> connectedZbior [MAX_NUMBER_OF_CITIES + 1]; set<int> neighbourhoodList[MAX_NUMBER_OF_CITIES + 1]; void readInput() { int startPoint; int endPoint; cin >> numberOfCities >> numberOfRoads >> minNumberOfRoadsToQualify; for(int i = 0; i < numberOfRoads; i++) { cin >> startPoint >> endPoint; neighbourhoodList[startPoint].insert(endPoint); neighbourhoodList[endPoint].insert(startPoint); } } void removeAllRoadsForIthCityIfDisqualified(int i) { vector <int> backup; if(neighbourhoodList[i].size() < minNumberOfRoadsToQualify) { for(set<int>::iterator it = neighbourhoodList[i].begin(); it != neighbourhoodList[i].end(); ++it) { backup.push_back(*it); } for(int j = 0; j < backup.size(); j++) { neighbourhoodList[i].erase(backup[j]); neighbourhoodList[backup[j]].erase(i); } for(int i = 0; i < backup.size(); i++) { removeAllRoadsForIthCityIfDisqualified(backup[i]); } } } void coutFor(int i, int beneficent) { if(visited[i] == false) { visited[i] = true; connectedZbior[beneficent].push_back(i); for(set<int>::iterator it = neighbourhoodList[i].begin(); it != neighbourhoodList[i].end(); ++it) { coutFor(*it, beneficent); } } } int main() { readInput(); for(int i = 1; i <= numberOfCities; i++) removeAllRoadsForIthCityIfDisqualified(i); for(int i = 1; i <= numberOfCities; i++) coutFor(i, i); int max = 0; int maxIndex = 1; for(int i = 1; i <= numberOfCities; i++) { if(connectedZbior[i].size() > max) { max = connectedZbior[i].size(); maxIndex = i; } } if(max == 1) cout << "NIE"; else { cout << max << endl; sort(connectedZbior[maxIndex].begin(), connectedZbior[maxIndex].end()); for(int i = 0; i < max; i++) cout << connectedZbior[maxIndex][i] << " "; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #include <iostream> #include <set> #include <vector> #include <algorithm> #define MAX_NUMBER_OF_CITIES 200000 #define MAX_NUMBER_OF_ROADS 200000 using namespace std; int numberOfCities; int numberOfRoads; int minNumberOfRoadsToQualify; bool visited [MAX_NUMBER_OF_CITIES + 1] = {}; vector <int> connectedZbior [MAX_NUMBER_OF_CITIES + 1]; set<int> neighbourhoodList[MAX_NUMBER_OF_CITIES + 1]; void readInput() { int startPoint; int endPoint; cin >> numberOfCities >> numberOfRoads >> minNumberOfRoadsToQualify; for(int i = 0; i < numberOfRoads; i++) { cin >> startPoint >> endPoint; neighbourhoodList[startPoint].insert(endPoint); neighbourhoodList[endPoint].insert(startPoint); } } void removeAllRoadsForIthCityIfDisqualified(int i) { vector <int> backup; if(neighbourhoodList[i].size() < minNumberOfRoadsToQualify) { for(set<int>::iterator it = neighbourhoodList[i].begin(); it != neighbourhoodList[i].end(); ++it) { backup.push_back(*it); } for(int j = 0; j < backup.size(); j++) { neighbourhoodList[i].erase(backup[j]); neighbourhoodList[backup[j]].erase(i); } for(int i = 0; i < backup.size(); i++) { removeAllRoadsForIthCityIfDisqualified(backup[i]); } } } void coutFor(int i, int beneficent) { if(visited[i] == false) { visited[i] = true; connectedZbior[beneficent].push_back(i); for(set<int>::iterator it = neighbourhoodList[i].begin(); it != neighbourhoodList[i].end(); ++it) { coutFor(*it, beneficent); } } } int main() { readInput(); for(int i = 1; i <= numberOfCities; i++) removeAllRoadsForIthCityIfDisqualified(i); for(int i = 1; i <= numberOfCities; i++) coutFor(i, i); int max = 0; int maxIndex = 1; for(int i = 1; i <= numberOfCities; i++) { if(connectedZbior[i].size() > max) { max = connectedZbior[i].size(); maxIndex = i; } } if(max == 1) cout << "NIE"; else { cout << max << endl; sort(connectedZbior[maxIndex].begin(), connectedZbior[maxIndex].end()); for(int i = 0; i < max; i++) cout << connectedZbior[maxIndex][i] << " "; } } |