#include <iostream> #include <vector> #include <queue> using namespace std; class Edge{ public: int v; Edge* next; }; struct CohesivePart{ int verticesC; int mainVertex; queue<int> toDelete; }; class UndirectedGraph{ public: UndirectedGraph(int size) { this->size = size; graph.resize(size); roadsConnected.resize(size); deleted.resize(size); for (int i = 0; i < size; ++i){ graph[i] = nullptr; roadsConnected[i] = 0; deleted[i] = false; } } void addEdge(int v1, int v2) { Edge* e = new Edge; e->v = v2; e->next = graph[v1]; graph[v1] = e; e = new Edge; e->v = v1; e->next = graph[v2]; graph[v2] = e; roadsConnected[v1]++; roadsConnected[v2]++; } vector<CohesivePart> findCohesiveParts(int paramD) { vector<CohesivePart> result; bool* visited = new bool[size]; for (int i = 0; i < size; ++i) visited[i] = false; int pos = 0; queue<int> toCheck; while (pos < size){ //szukamy kolejnej spójnej składowej while (pos < size && (visited[pos] || deleted[pos])) ++pos; if (pos < size){ //tworzymy nową spójną składową CohesivePart cohesivePart; cohesivePart.mainVertex = pos; cohesivePart.verticesC = 0; //przeglądamy wszystkie wierzcholki toCheck.push(pos); visited[pos] = true; while (!toCheck.empty()){ int v = toCheck.front(); toCheck.pop(); cohesivePart.verticesC++; for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next) if (!visited[edge->v] && !deleted[edge->v]){ visited[edge->v] = true; toCheck.push(edge->v); } } result.push_back(cohesivePart); } } return result; } void clean(int paramD) { queue<int> toDelete; //Dodajemy pierwsze wierzcholki do wywalenia for (int i = 0; i < size; ++i){ if (roadsConnected[i] < paramD){ toDelete.push(i); deleted[i] = true; } } while (!toDelete.empty()){ int v = toDelete.front(); toDelete.pop(); for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next){ if (!deleted[edge->v]){ --roadsConnected[edge->v]; if (roadsConnected[edge->v] < paramD){ deleted[edge->v] = true; toDelete.push(edge->v); } } } } } void printCohesivePart(CohesivePart& cohesivePart) { bool* visited = new bool[size]; for (int i = 0; i < size; ++i) visited[i] = false; queue<int> toCheck; toCheck.push(cohesivePart.mainVertex); visited[cohesivePart.mainVertex] = true; while (!toCheck.empty()){ int v = toCheck.front(); toCheck.pop(); for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next) if (!visited[edge->v] && !deleted[edge->v]){ visited[edge->v] = true; toCheck.push(edge->v); } } for (int i = 0; i < size; ++i) if (visited[i]) cout << i + 1 << " "; cout << endl; } private: int size; vector<Edge*> graph; vector<int> roadsConnected; vector<bool> deleted; }; int main(){ ios_base::sync_with_stdio(false); int citiesC, roadsC, paramD; cin >> citiesC >> roadsC >> paramD; UndirectedGraph graph(citiesC); for (int i = 0; i < roadsC; ++i){ int v1, v2; cin >> v1 >> v2; graph.addEdge(v1-1, v2-1); } graph.clean(paramD); vector<CohesivePart> cohesiveParts = graph.findCohesiveParts(paramD); if (cohesiveParts.empty()){ cout << "NIE" << endl; } else { int maxVerticesCount = 0; int maxCohesivePart; for (int i = 0; i < cohesiveParts.size(); ++i){ if (cohesiveParts[i].verticesC > maxVerticesCount){ maxVerticesCount = cohesiveParts[i].verticesC; maxCohesivePart = i; } } if (maxVerticesCount == 0) cout << "NIE\n"; else { cout << maxVerticesCount << endl; graph.printCohesivePart(cohesiveParts[maxCohesivePart]); } } }
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 | #include <iostream> #include <vector> #include <queue> using namespace std; class Edge{ public: int v; Edge* next; }; struct CohesivePart{ int verticesC; int mainVertex; queue<int> toDelete; }; class UndirectedGraph{ public: UndirectedGraph(int size) { this->size = size; graph.resize(size); roadsConnected.resize(size); deleted.resize(size); for (int i = 0; i < size; ++i){ graph[i] = nullptr; roadsConnected[i] = 0; deleted[i] = false; } } void addEdge(int v1, int v2) { Edge* e = new Edge; e->v = v2; e->next = graph[v1]; graph[v1] = e; e = new Edge; e->v = v1; e->next = graph[v2]; graph[v2] = e; roadsConnected[v1]++; roadsConnected[v2]++; } vector<CohesivePart> findCohesiveParts(int paramD) { vector<CohesivePart> result; bool* visited = new bool[size]; for (int i = 0; i < size; ++i) visited[i] = false; int pos = 0; queue<int> toCheck; while (pos < size){ //szukamy kolejnej spójnej składowej while (pos < size && (visited[pos] || deleted[pos])) ++pos; if (pos < size){ //tworzymy nową spójną składową CohesivePart cohesivePart; cohesivePart.mainVertex = pos; cohesivePart.verticesC = 0; //przeglądamy wszystkie wierzcholki toCheck.push(pos); visited[pos] = true; while (!toCheck.empty()){ int v = toCheck.front(); toCheck.pop(); cohesivePart.verticesC++; for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next) if (!visited[edge->v] && !deleted[edge->v]){ visited[edge->v] = true; toCheck.push(edge->v); } } result.push_back(cohesivePart); } } return result; } void clean(int paramD) { queue<int> toDelete; //Dodajemy pierwsze wierzcholki do wywalenia for (int i = 0; i < size; ++i){ if (roadsConnected[i] < paramD){ toDelete.push(i); deleted[i] = true; } } while (!toDelete.empty()){ int v = toDelete.front(); toDelete.pop(); for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next){ if (!deleted[edge->v]){ --roadsConnected[edge->v]; if (roadsConnected[edge->v] < paramD){ deleted[edge->v] = true; toDelete.push(edge->v); } } } } } void printCohesivePart(CohesivePart& cohesivePart) { bool* visited = new bool[size]; for (int i = 0; i < size; ++i) visited[i] = false; queue<int> toCheck; toCheck.push(cohesivePart.mainVertex); visited[cohesivePart.mainVertex] = true; while (!toCheck.empty()){ int v = toCheck.front(); toCheck.pop(); for (Edge* edge = graph[v]; edge != nullptr; edge = edge->next) if (!visited[edge->v] && !deleted[edge->v]){ visited[edge->v] = true; toCheck.push(edge->v); } } for (int i = 0; i < size; ++i) if (visited[i]) cout << i + 1 << " "; cout << endl; } private: int size; vector<Edge*> graph; vector<int> roadsConnected; vector<bool> deleted; }; int main(){ ios_base::sync_with_stdio(false); int citiesC, roadsC, paramD; cin >> citiesC >> roadsC >> paramD; UndirectedGraph graph(citiesC); for (int i = 0; i < roadsC; ++i){ int v1, v2; cin >> v1 >> v2; graph.addEdge(v1-1, v2-1); } graph.clean(paramD); vector<CohesivePart> cohesiveParts = graph.findCohesiveParts(paramD); if (cohesiveParts.empty()){ cout << "NIE" << endl; } else { int maxVerticesCount = 0; int maxCohesivePart; for (int i = 0; i < cohesiveParts.size(); ++i){ if (cohesiveParts[i].verticesC > maxVerticesCount){ maxVerticesCount = cohesiveParts[i].verticesC; maxCohesivePart = i; } } if (maxVerticesCount == 0) cout << "NIE\n"; else { cout << maxVerticesCount << endl; graph.printCohesivePart(cohesiveParts[maxCohesivePart]); } } } |