#include <iostream>
#include <cstdint>
#include <set>
#include <map>
struct Data
{
Data() : count(0) {}
uint32_t count;
std::set<uint32_t> neightbours;
};
typedef std::map<uint32_t, Data> Graph;
void search(uint32_t v, Graph &g, std::set<uint32_t> &s)
{
g[v].count = 0;
s.insert(v);
for (const auto &i : g[v].neightbours)
{
if (g[i].count > 0)
search(i, g, s);
}
}
int main()
{
std::ios_base::sync_with_stdio(0);
//
Graph graph;
uint32_t N = 0, M = 0, D = 0;
std::cin >> N >> M >> D;
for (uint32_t i = 0; i < M; ++i)
{
uint32_t a = 0, b = 0;
std::cin >> a >> b;
graph[a].count++;
graph[a].neightbours.insert(b);
graph[b].count++;
graph[b].neightbours.insert(a);
}
// DEBUG
// std::cout << N << " " << M << " " << D << std::endl;
// for (const auto &i : graph) {
// printf("%d %d \n", i.first, i.second.count);
// for (const auto &j : i.second.neightbours)
// printf("\t%d\n", j);
// }
// return 0;
//
std::set<uint32_t> to_delete;
for (const auto &i : graph)
if (i.second.count < D)
to_delete.insert(i.first);
// DEBUG
// for (const auto &i : to_delete)
// printf("%d\n", i);
//
while (to_delete.empty() == false)
{
uint32_t id = *(to_delete.begin());
for (const auto &i : graph[id].neightbours) {
graph[i].count--;
graph[i].neightbours.erase(id);
if (graph[i].count == D - 1)
to_delete.insert(i);
}
graph[id].count = 0;
graph[id].neightbours.clear();
to_delete.erase(id);
}
// DEBUG
// std::cout << N << " " << M << " " << D << std::endl;
// for (const auto &i : graph) {
// printf("%d %d \n", i.first, i.second.count);
// for (const auto &j : i.second.neightbours)
// printf("\t%d\n", j);
// }
// return 0;
//
std::set<uint32_t> S;
for (auto &i : graph)
if (i.second.count > 0) {
std::set<uint32_t> s;
search(i.first, graph, s);
if (s.size() > S.size())
S = s;
}
if (S.empty() == true)
{
std::cout << "NIE" << std::endl;
}
else
{
std::cout << S.size() << std::endl;
for (const auto &i : S)
std::cout << i << " ";
}
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 | #include <iostream> #include <cstdint> #include <set> #include <map> struct Data { Data() : count(0) {} uint32_t count; std::set<uint32_t> neightbours; }; typedef std::map<uint32_t, Data> Graph; void search(uint32_t v, Graph &g, std::set<uint32_t> &s) { g[v].count = 0; s.insert(v); for (const auto &i : g[v].neightbours) { if (g[i].count > 0) search(i, g, s); } } int main() { std::ios_base::sync_with_stdio(0); // Graph graph; uint32_t N = 0, M = 0, D = 0; std::cin >> N >> M >> D; for (uint32_t i = 0; i < M; ++i) { uint32_t a = 0, b = 0; std::cin >> a >> b; graph[a].count++; graph[a].neightbours.insert(b); graph[b].count++; graph[b].neightbours.insert(a); } // DEBUG // std::cout << N << " " << M << " " << D << std::endl; // for (const auto &i : graph) { // printf("%d %d \n", i.first, i.second.count); // for (const auto &j : i.second.neightbours) // printf("\t%d\n", j); // } // return 0; // std::set<uint32_t> to_delete; for (const auto &i : graph) if (i.second.count < D) to_delete.insert(i.first); // DEBUG // for (const auto &i : to_delete) // printf("%d\n", i); // while (to_delete.empty() == false) { uint32_t id = *(to_delete.begin()); for (const auto &i : graph[id].neightbours) { graph[i].count--; graph[i].neightbours.erase(id); if (graph[i].count == D - 1) to_delete.insert(i); } graph[id].count = 0; graph[id].neightbours.clear(); to_delete.erase(id); } // DEBUG // std::cout << N << " " << M << " " << D << std::endl; // for (const auto &i : graph) { // printf("%d %d \n", i.first, i.second.count); // for (const auto &j : i.second.neightbours) // printf("\t%d\n", j); // } // return 0; // std::set<uint32_t> S; for (auto &i : graph) if (i.second.count > 0) { std::set<uint32_t> s; search(i.first, graph, s); if (s.size() > S.size()) S = s; } if (S.empty() == true) { std::cout << "NIE" << std::endl; } else { std::cout << S.size() << std::endl; for (const auto &i : S) std::cout << i << " "; } return 0; } |
English