#include <cstdlib> #include <iostream> #include <vector> #include <cmath> #include <numeric> #include <string> #include <queue> #include <set> #include <map> #include <unordered_set> class World { private: unsigned int d; std::vector<std::vector<int> > paths; std::vector<unsigned int> ranks; std::vector<bool> removed; private: void readInput() { int n, m, d; std::cin >> n >> m>>d; this->d = d; paths.resize(n); ranks.resize(n); removed.resize(n); for (int i = 0; i < m; ++i) { int a, b; std::cin >> a>>b; a--; b--; paths[a].push_back(b); paths[b].push_back(a); ranks[a]++; ranks[b]++; } } void removeNotConnected() { std::queue<int> toRemove; for(unsigned int i = 0; i < ranks.size(); ++i) { if(ranks[i] < d) { toRemove.push(i); } } while(!toRemove.empty()) { int next = toRemove.front(); toRemove.pop(); if(removed[next]) { continue; } removed[next] = true; for(auto connected = paths[next].begin(); connected != paths[next].end(); ++connected) { ranks[*connected]--; if(ranks[*connected] < d) { toRemove.push(*connected); } } } } std::set<int> findSolution() { std::set<int> solution; std::set<int> toVisit; for(unsigned int i = 0; i < paths.size(); ++i) { if(!removed[i]) { toVisit.insert(i); } } while(!toVisit.empty()) { std::set<int> sol; int first = *toVisit.begin(); std::queue<int> q; q.push(first); while(!q.empty()) { int next = q.front(); q.pop(); toVisit.erase(next); if(removed[next]) { continue; } sol.insert(next); removed[next] = true; for(auto connected = paths[next].begin(); connected != paths[next].end(); ++connected) { if(!removed[*connected]) { q.push(*connected); } } } if(sol.size() > solution.size()) { solution = sol; } } return solution; } public: void solve() { readInput(); removeNotConnected(); std::set<int> solution = findSolution(); if (solution.empty()) { std::cout << "NIE" << std::endl; } else { std::cout << solution.size() << std::endl; for (auto item = solution.begin(); item != solution.end(); ++item) { if (item != solution.begin()) { std::cout << " "; } std::cout << (*item) + 1; } std::cout << std::endl; } } }; int main(int argc, char** argv) { std::ios_base::sync_with_stdio(false); World world; world.solve(); 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 | #include <cstdlib> #include <iostream> #include <vector> #include <cmath> #include <numeric> #include <string> #include <queue> #include <set> #include <map> #include <unordered_set> class World { private: unsigned int d; std::vector<std::vector<int> > paths; std::vector<unsigned int> ranks; std::vector<bool> removed; private: void readInput() { int n, m, d; std::cin >> n >> m>>d; this->d = d; paths.resize(n); ranks.resize(n); removed.resize(n); for (int i = 0; i < m; ++i) { int a, b; std::cin >> a>>b; a--; b--; paths[a].push_back(b); paths[b].push_back(a); ranks[a]++; ranks[b]++; } } void removeNotConnected() { std::queue<int> toRemove; for(unsigned int i = 0; i < ranks.size(); ++i) { if(ranks[i] < d) { toRemove.push(i); } } while(!toRemove.empty()) { int next = toRemove.front(); toRemove.pop(); if(removed[next]) { continue; } removed[next] = true; for(auto connected = paths[next].begin(); connected != paths[next].end(); ++connected) { ranks[*connected]--; if(ranks[*connected] < d) { toRemove.push(*connected); } } } } std::set<int> findSolution() { std::set<int> solution; std::set<int> toVisit; for(unsigned int i = 0; i < paths.size(); ++i) { if(!removed[i]) { toVisit.insert(i); } } while(!toVisit.empty()) { std::set<int> sol; int first = *toVisit.begin(); std::queue<int> q; q.push(first); while(!q.empty()) { int next = q.front(); q.pop(); toVisit.erase(next); if(removed[next]) { continue; } sol.insert(next); removed[next] = true; for(auto connected = paths[next].begin(); connected != paths[next].end(); ++connected) { if(!removed[*connected]) { q.push(*connected); } } } if(sol.size() > solution.size()) { solution = sol; } } return solution; } public: void solve() { readInput(); removeNotConnected(); std::set<int> solution = findSolution(); if (solution.empty()) { std::cout << "NIE" << std::endl; } else { std::cout << solution.size() << std::endl; for (auto item = solution.begin(); item != solution.end(); ++item) { if (item != solution.begin()) { std::cout << " "; } std::cout << (*item) + 1; } std::cout << std::endl; } } }; int main(int argc, char** argv) { std::ios_base::sync_with_stdio(false); World world; world.solve(); return 0; } |