#include <cstdio> #include <vector> #include <queue> typedef unsigned U; const U memory = 200001, save = 0x3FFFFFFF; std::vector<U> Graph[memory]; U graphSize[memory], n, d, nodesTest; std::queue<U> ToDelete; void bFS() { U v; while (!ToDelete.empty()) { v = ToDelete.front(); ToDelete.pop(); for (U u, i = 0; i < Graph[v].size(); ++i) { u = Graph[v][i]; if (--graphSize[u] < d) { graphSize[u] = save; ToDelete.push(u); } } } } void dFS(U v, U nodesSign) { ++nodesTest; graphSize[v] = nodesSign; ToDelete.push(v); while (!ToDelete.empty()) { v = ToDelete.front(); ToDelete.pop(); for (U u, i = 0; i < Graph[v].size(); ++i) { u = Graph[v][i]; if (graphSize[u] < n) { ++nodesTest; graphSize[u] = nodesSign; ToDelete.push(u); } } } } int main() { U m; scanf("%u%u%u", &n, &m, &d); for (U a, b, i = 0; i < m; ++i) { scanf("%u%u", &a, &b); Graph[a].push_back(b); Graph[b].push_back(a); } for (U i = 1; i <= n; ++i) { if (Graph[i].size() < d) { graphSize[i] = save; ToDelete.push(i); } else graphSize[i] = Graph[i].size(); } bFS(); U nodesMax = 0, nodesSign = n - 1, nodesSignMax; for (U i = 1; i <= n; ++i) { if (graphSize[i] < n) { nodesTest = 0; dFS(i, ++nodesSign); if (nodesMax < nodesTest) { nodesMax = nodesTest; nodesSignMax = nodesSign; } } } if (nodesMax) { printf("%u\n", nodesMax); for (U i = 1; i <= n; ++i) if (graphSize[i] == nodesSignMax) printf("%u ", i); } else printf("NIE"); printf("\n"); 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 | #include <cstdio> #include <vector> #include <queue> typedef unsigned U; const U memory = 200001, save = 0x3FFFFFFF; std::vector<U> Graph[memory]; U graphSize[memory], n, d, nodesTest; std::queue<U> ToDelete; void bFS() { U v; while (!ToDelete.empty()) { v = ToDelete.front(); ToDelete.pop(); for (U u, i = 0; i < Graph[v].size(); ++i) { u = Graph[v][i]; if (--graphSize[u] < d) { graphSize[u] = save; ToDelete.push(u); } } } } void dFS(U v, U nodesSign) { ++nodesTest; graphSize[v] = nodesSign; ToDelete.push(v); while (!ToDelete.empty()) { v = ToDelete.front(); ToDelete.pop(); for (U u, i = 0; i < Graph[v].size(); ++i) { u = Graph[v][i]; if (graphSize[u] < n) { ++nodesTest; graphSize[u] = nodesSign; ToDelete.push(u); } } } } int main() { U m; scanf("%u%u%u", &n, &m, &d); for (U a, b, i = 0; i < m; ++i) { scanf("%u%u", &a, &b); Graph[a].push_back(b); Graph[b].push_back(a); } for (U i = 1; i <= n; ++i) { if (Graph[i].size() < d) { graphSize[i] = save; ToDelete.push(i); } else graphSize[i] = Graph[i].size(); } bFS(); U nodesMax = 0, nodesSign = n - 1, nodesSignMax; for (U i = 1; i <= n; ++i) { if (graphSize[i] < n) { nodesTest = 0; dFS(i, ++nodesSign); if (nodesMax < nodesTest) { nodesMax = nodesTest; nodesSignMax = nodesSign; } } } if (nodesMax) { printf("%u\n", nodesMax); for (U i = 1; i <= n; ++i) if (graphSize[i] == nodesSignMax) printf("%u ", i); } else printf("NIE"); printf("\n"); return 0; } |