#include <bits/stdc++.h> #define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++) //#define DEBUG_MODE #ifdef DEBUG_MODE #define print(x) cout << #x << " = " << x << endl #define debug(x) x #else #define print(x) #define debug(x) #endif #define hash_map unordered_map #define hash_multimap unordered_multimap #define hash_set unordered_set #define hash_multiset unordered_multiset using namespace std; typedef short int int16; typedef unsigned short int uint16; typedef int int32; typedef unsigned int uint32; typedef long long int64; typedef unsigned long long uint64; typedef pair <int, int> PII; typedef pair <uint32, uint32> PUU; typedef pair <uint32, int> PUI; typedef pair <int, uint32> PIU; typedef pair <int64, int64> PLL; typedef pair <int64, int> PLI; typedef pair <int, int64> PIL; typedef pair <int, int16> PIS; typedef pair <int16, int> PSI; const int UNDEF = -1; class Application { public: inline void Run(); private: // Methods inline void LoadData(); inline void Solve(); inline void IsolateGraph(); inline void FindAnswer(int start_node); inline void CoutAnswerNodes(); // Fields int size_; int edge_number_; int D; vector < vector <int> > graph_; vector < bool > active_; int best_answer_ = UNDEF; int best_start_ = UNDEF; }; int main() { ios_base::sync_with_stdio(false); Application app; app.Run(); } inline void Application::LoadData() { cin >> size_ >> edge_number_ >> D; graph_.resize(size_); active_.assign(size_, true); int a, b; REP(i, edge_number_) { cin >> a >> b; --a;--b; graph_[a].push_back(b); graph_[b].push_back(a); } } inline void Application::IsolateGraph() { stack <int> not_active; vector <int> degree_(size_); REP(i, size_) { if(graph_[i].size() < D) { not_active.push(i); active_[i] = false; } degree_[i] = graph_[i].size(); } while(!not_active.empty()) { int p = not_active.top(); not_active.pop(); for(auto it: graph_[p]) { if(active_[it]) { -- degree_[it]; if(degree_[it] < D) { not_active.push(it); active_[it] = false; } } } } vector < vector <int> > temp_graph(size_); REP(i, size_) { if(active_[i]) { for(auto it : graph_[i]) { if(active_[it]) temp_graph[i].push_back(it); } } } swap(temp_graph, graph_); } inline void Application::FindAnswer(int start_node) { int result = 1; queue <int> q; q.push(start_node); active_[start_node] = false; while(!q.empty()) { int p = q.front(); q.pop(); for(auto it: graph_[p]) { if(active_[it]) { ++ result; active_[it] = false; q.push(it); } } } if(result > best_answer_) { best_answer_ = result; best_start_ = start_node; } } inline void Application::CoutAnswerNodes() { active_.assign(size_, true); queue <int> q; q.push(best_start_); active_[best_start_] = false; while(!q.empty()) { int p = q.front(); q.pop(); for(auto it: graph_[p]) { if(active_[it]) { active_[it] = false; q.push(it); } } } REP(i, size_) { if(!active_[i]) cout << i + 1 << " "; } } inline void Application::Solve() { IsolateGraph(); REP(i, size_) { if(active_[i]) { FindAnswer(i); } } if(best_answer_ == UNDEF) cout << "NIE\n"; else { cout << best_answer_ << "\n"; CoutAnswerNodes(); } } inline void Application::Run() { LoadData(); Solve(); }
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #include <bits/stdc++.h> #define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++) //#define DEBUG_MODE #ifdef DEBUG_MODE #define print(x) cout << #x << " = " << x << endl #define debug(x) x #else #define print(x) #define debug(x) #endif #define hash_map unordered_map #define hash_multimap unordered_multimap #define hash_set unordered_set #define hash_multiset unordered_multiset using namespace std; typedef short int int16; typedef unsigned short int uint16; typedef int int32; typedef unsigned int uint32; typedef long long int64; typedef unsigned long long uint64; typedef pair <int, int> PII; typedef pair <uint32, uint32> PUU; typedef pair <uint32, int> PUI; typedef pair <int, uint32> PIU; typedef pair <int64, int64> PLL; typedef pair <int64, int> PLI; typedef pair <int, int64> PIL; typedef pair <int, int16> PIS; typedef pair <int16, int> PSI; const int UNDEF = -1; class Application { public: inline void Run(); private: // Methods inline void LoadData(); inline void Solve(); inline void IsolateGraph(); inline void FindAnswer(int start_node); inline void CoutAnswerNodes(); // Fields int size_; int edge_number_; int D; vector < vector <int> > graph_; vector < bool > active_; int best_answer_ = UNDEF; int best_start_ = UNDEF; }; int main() { ios_base::sync_with_stdio(false); Application app; app.Run(); } inline void Application::LoadData() { cin >> size_ >> edge_number_ >> D; graph_.resize(size_); active_.assign(size_, true); int a, b; REP(i, edge_number_) { cin >> a >> b; --a;--b; graph_[a].push_back(b); graph_[b].push_back(a); } } inline void Application::IsolateGraph() { stack <int> not_active; vector <int> degree_(size_); REP(i, size_) { if(graph_[i].size() < D) { not_active.push(i); active_[i] = false; } degree_[i] = graph_[i].size(); } while(!not_active.empty()) { int p = not_active.top(); not_active.pop(); for(auto it: graph_[p]) { if(active_[it]) { -- degree_[it]; if(degree_[it] < D) { not_active.push(it); active_[it] = false; } } } } vector < vector <int> > temp_graph(size_); REP(i, size_) { if(active_[i]) { for(auto it : graph_[i]) { if(active_[it]) temp_graph[i].push_back(it); } } } swap(temp_graph, graph_); } inline void Application::FindAnswer(int start_node) { int result = 1; queue <int> q; q.push(start_node); active_[start_node] = false; while(!q.empty()) { int p = q.front(); q.pop(); for(auto it: graph_[p]) { if(active_[it]) { ++ result; active_[it] = false; q.push(it); } } } if(result > best_answer_) { best_answer_ = result; best_start_ = start_node; } } inline void Application::CoutAnswerNodes() { active_.assign(size_, true); queue <int> q; q.push(best_start_); active_[best_start_] = false; while(!q.empty()) { int p = q.front(); q.pop(); for(auto it: graph_[p]) { if(active_[it]) { active_[it] = false; q.push(it); } } } REP(i, size_) { if(!active_[i]) cout << i + 1 << " "; } } inline void Application::Solve() { IsolateGraph(); REP(i, size_) { if(active_[i]) { FindAnswer(i); } } if(best_answer_ == UNDEF) cout << "NIE\n"; else { cout << best_answer_ << "\n"; CoutAnswerNodes(); } } inline void Application::Run() { LoadData(); Solve(); } |