#include <cstdio> #include <vector> #include <queue> using namespace std; class CoherentSet { public: vector<int> vertices; int counter; CoherentSet(){ counter = 0; } void incrementCounter() { counter++; } void addVertex(int vertexNumber){ this->vertices.push_back(vertexNumber); } void printVertices(){ for(int i=0; i<vertices.size(); i++){ printf("%d ", vertices[i]+1); } } }; class Vertex { public: vector<Vertex*> connections; bool active; bool visited; int activeConnections; int id; int color; Vertex() { this->active = true; this->activeConnections = 0; this->color = 0; this->visited = false; } int getActiveConnections(){ return this->activeConnections; } void addActiveConnection(){ this->activeConnections++; } void removeActiveConnection(){ this->activeConnections--; } void setInactive(){ this->active = false; } bool isActive(){ return this->active; } int getColor(){ return this->color; } void setColor(int color){ this->color = color; } bool isVisited() const { return visited; } void setVisited(bool visited) { Vertex::visited = visited; } }; int uncolouredCounter = 0; Vertex* getFirstUncoloredVertex(Vertex *vertices, int verticesNumber){ for(int i=uncolouredCounter; i<verticesNumber; i++){ if(vertices[i].getColor() == 0 && vertices[i].isActive()){ uncolouredCounter = i; return &vertices[i]; } } return NULL; } int main() { int verticesNumber = 0; int edgesNumber = 0; int minimumRoads = 0; int fromVertex; int toVertex; scanf("%d", &verticesNumber); scanf("%d", &edgesNumber); scanf("%d", &minimumRoads); Vertex vertices[verticesNumber]; for(int i=0; i<verticesNumber; i++){ vertices[i].id = i; } for(int i=0; i<edgesNumber; i++){ scanf("%d", &fromVertex); scanf("%d", &toVertex); fromVertex--; toVertex--; vertices[fromVertex].connections.push_back(&vertices[toVertex]); vertices[fromVertex].addActiveConnection(); vertices[toVertex].connections.push_back(&vertices[fromVertex]); vertices[toVertex].addActiveConnection(); } queue<Vertex*> bfsQueue; // insert all vertices with less than minimumRoads for(int i=0; i<verticesNumber; i++){ if(vertices[i].getActiveConnections() < minimumRoads){ vertices[i].setInactive(); bfsQueue.push(&vertices[i]); } } while(bfsQueue.size() != 0) { Vertex* current = bfsQueue.front(); bfsQueue.pop(); for(int i=0; i<current->connections.size(); i++){ current->connections[i]->removeActiveConnection(); if(current->connections[i]->getActiveConnections() < minimumRoads && current->connections[i]->isActive()){ current->connections[i]->setInactive(); bfsQueue.push(current->connections[i]); } } } int colorCounter = 0; int biggestSetId = -1; int biggestSetCounter = 0; vector<CoherentSet> sets; Vertex* firstUncolored = getFirstUncoloredVertex(vertices, verticesNumber); while(firstUncolored != NULL){ CoherentSet set; colorCounter++; bfsQueue.push(firstUncolored); while(bfsQueue.size() != 0) { Vertex* current = bfsQueue.front(); bfsQueue.pop(); if (current->getColor() != 0) { continue; } set.addVertex(current->id); set.incrementCounter(); current->setColor(colorCounter); for (int i = 0; i < current->connections.size(); i++) { if(current->connections[i]->isActive() && current->getColor() != 0){ bfsQueue.push(current->connections[i]); } } } sets.push_back(set); if(set.counter > biggestSetCounter){ biggestSetId = sets.size()-1; biggestSetCounter = set.counter; } firstUncolored = getFirstUncoloredVertex(vertices, verticesNumber); } if(sets.empty()){ printf("NIE\n"); } else { printf("%d\n", biggestSetCounter); sets[biggestSetId].printVertices(); } return 0; }
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #include <cstdio> #include <vector> #include <queue> using namespace std; class CoherentSet { public: vector<int> vertices; int counter; CoherentSet(){ counter = 0; } void incrementCounter() { counter++; } void addVertex(int vertexNumber){ this->vertices.push_back(vertexNumber); } void printVertices(){ for(int i=0; i<vertices.size(); i++){ printf("%d ", vertices[i]+1); } } }; class Vertex { public: vector<Vertex*> connections; bool active; bool visited; int activeConnections; int id; int color; Vertex() { this->active = true; this->activeConnections = 0; this->color = 0; this->visited = false; } int getActiveConnections(){ return this->activeConnections; } void addActiveConnection(){ this->activeConnections++; } void removeActiveConnection(){ this->activeConnections--; } void setInactive(){ this->active = false; } bool isActive(){ return this->active; } int getColor(){ return this->color; } void setColor(int color){ this->color = color; } bool isVisited() const { return visited; } void setVisited(bool visited) { Vertex::visited = visited; } }; int uncolouredCounter = 0; Vertex* getFirstUncoloredVertex(Vertex *vertices, int verticesNumber){ for(int i=uncolouredCounter; i<verticesNumber; i++){ if(vertices[i].getColor() == 0 && vertices[i].isActive()){ uncolouredCounter = i; return &vertices[i]; } } return NULL; } int main() { int verticesNumber = 0; int edgesNumber = 0; int minimumRoads = 0; int fromVertex; int toVertex; scanf("%d", &verticesNumber); scanf("%d", &edgesNumber); scanf("%d", &minimumRoads); Vertex vertices[verticesNumber]; for(int i=0; i<verticesNumber; i++){ vertices[i].id = i; } for(int i=0; i<edgesNumber; i++){ scanf("%d", &fromVertex); scanf("%d", &toVertex); fromVertex--; toVertex--; vertices[fromVertex].connections.push_back(&vertices[toVertex]); vertices[fromVertex].addActiveConnection(); vertices[toVertex].connections.push_back(&vertices[fromVertex]); vertices[toVertex].addActiveConnection(); } queue<Vertex*> bfsQueue; // insert all vertices with less than minimumRoads for(int i=0; i<verticesNumber; i++){ if(vertices[i].getActiveConnections() < minimumRoads){ vertices[i].setInactive(); bfsQueue.push(&vertices[i]); } } while(bfsQueue.size() != 0) { Vertex* current = bfsQueue.front(); bfsQueue.pop(); for(int i=0; i<current->connections.size(); i++){ current->connections[i]->removeActiveConnection(); if(current->connections[i]->getActiveConnections() < minimumRoads && current->connections[i]->isActive()){ current->connections[i]->setInactive(); bfsQueue.push(current->connections[i]); } } } int colorCounter = 0; int biggestSetId = -1; int biggestSetCounter = 0; vector<CoherentSet> sets; Vertex* firstUncolored = getFirstUncoloredVertex(vertices, verticesNumber); while(firstUncolored != NULL){ CoherentSet set; colorCounter++; bfsQueue.push(firstUncolored); while(bfsQueue.size() != 0) { Vertex* current = bfsQueue.front(); bfsQueue.pop(); if (current->getColor() != 0) { continue; } set.addVertex(current->id); set.incrementCounter(); current->setColor(colorCounter); for (int i = 0; i < current->connections.size(); i++) { if(current->connections[i]->isActive() && current->getColor() != 0){ bfsQueue.push(current->connections[i]); } } } sets.push_back(set); if(set.counter > biggestSetCounter){ biggestSetId = sets.size()-1; biggestSetCounter = set.counter; } firstUncolored = getFirstUncoloredVertex(vertices, verticesNumber); } if(sets.empty()){ printf("NIE\n"); } else { printf("%d\n", biggestSetCounter); sets[biggestSetId].printVertices(); } return 0; } |