#include <cstdio> #include <algorithm> #include <cstdlib> #include <deque> #include <vector> #include <set> using namespace std; struct Node { set<int> neighbours; int degree; int group; bool isRemoved; int howManyTimesWasVisited; }; Node nodes[200000]; deque<int> aloneNodes; int n, m, d; vector<int> solution; deque<int> dfsNodes; void removeNode(int id) { if (nodes[id].isRemoved) { return; } nodes[id].isRemoved = true; for (auto destination = nodes[id].neighbours.begin(); destination != nodes[id].neighbours.end(); ++destination) { int target = *destination; nodes[target].neighbours.erase(nodes[target].neighbours.find(id)); nodes[target].degree--; if (nodes[target].degree < d) { aloneNodes.push_back(target); } } } int dfs(int id) { dfsNodes.clear(); dfsNodes.push_back(id); int sum = 0; while (!dfsNodes.empty()) { int node = dfsNodes.front(); dfsNodes.pop_front(); nodes[node].howManyTimesWasVisited = 2; sum++; for (auto destination = nodes[node].neighbours.begin(); destination != nodes[node].neighbours.end(); ++destination) { if (nodes[*destination].howManyTimesWasVisited == 0) { dfsNodes.push_back(*destination); nodes[*destination].howManyTimesWasVisited = 1; } } } return sum; } void findSolution(int id) { dfsNodes.clear(); dfsNodes.push_back(id); while (!dfsNodes.empty()) { int node = dfsNodes.front(); dfsNodes.pop_front(); solution.push_back(node); nodes[node].howManyTimesWasVisited = 4; for (auto destination = nodes[node].neighbours.begin(); destination != nodes[node].neighbours.end(); ++destination) { if (nodes[*destination].howManyTimesWasVisited == 2) { dfsNodes.push_back(*destination); nodes[*destination].howManyTimesWasVisited = 3; } } } } int main() { scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; ++i) { int source, destination; scanf("%d %d", &source, &destination); source--; destination--; nodes[source].neighbours.insert(destination); nodes[source].degree++; nodes[destination].neighbours.insert(source); nodes[destination].degree++; } for (int i = 0; i < n; ++i) { if (nodes[i].degree < d) { aloneNodes.push_back(i); } } while (!aloneNodes.empty()) { int nodeId = aloneNodes.front(); aloneNodes.pop_front(); removeNode(nodeId); } int groupSize = -1; int groupId = -1; for (int i = 0; i < n; ++i) { if (!nodes[i].isRemoved && nodes[i].howManyTimesWasVisited == 0) { int currentSize = dfs(i); if (currentSize > groupSize) { groupSize = currentSize; groupId = i; } } } if (groupId == -1) { printf("NIE"); return 0; } findSolution(groupId); sort(solution.begin(), solution.end()); printf("%d\n", solution.size()); int size = solution.size(); for (int i = 0; i < size; ++i) { printf("%d", solution[i]+1); if (i < size - 1) { printf(" "); } } 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 128 129 130 | #include <cstdio> #include <algorithm> #include <cstdlib> #include <deque> #include <vector> #include <set> using namespace std; struct Node { set<int> neighbours; int degree; int group; bool isRemoved; int howManyTimesWasVisited; }; Node nodes[200000]; deque<int> aloneNodes; int n, m, d; vector<int> solution; deque<int> dfsNodes; void removeNode(int id) { if (nodes[id].isRemoved) { return; } nodes[id].isRemoved = true; for (auto destination = nodes[id].neighbours.begin(); destination != nodes[id].neighbours.end(); ++destination) { int target = *destination; nodes[target].neighbours.erase(nodes[target].neighbours.find(id)); nodes[target].degree--; if (nodes[target].degree < d) { aloneNodes.push_back(target); } } } int dfs(int id) { dfsNodes.clear(); dfsNodes.push_back(id); int sum = 0; while (!dfsNodes.empty()) { int node = dfsNodes.front(); dfsNodes.pop_front(); nodes[node].howManyTimesWasVisited = 2; sum++; for (auto destination = nodes[node].neighbours.begin(); destination != nodes[node].neighbours.end(); ++destination) { if (nodes[*destination].howManyTimesWasVisited == 0) { dfsNodes.push_back(*destination); nodes[*destination].howManyTimesWasVisited = 1; } } } return sum; } void findSolution(int id) { dfsNodes.clear(); dfsNodes.push_back(id); while (!dfsNodes.empty()) { int node = dfsNodes.front(); dfsNodes.pop_front(); solution.push_back(node); nodes[node].howManyTimesWasVisited = 4; for (auto destination = nodes[node].neighbours.begin(); destination != nodes[node].neighbours.end(); ++destination) { if (nodes[*destination].howManyTimesWasVisited == 2) { dfsNodes.push_back(*destination); nodes[*destination].howManyTimesWasVisited = 3; } } } } int main() { scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; ++i) { int source, destination; scanf("%d %d", &source, &destination); source--; destination--; nodes[source].neighbours.insert(destination); nodes[source].degree++; nodes[destination].neighbours.insert(source); nodes[destination].degree++; } for (int i = 0; i < n; ++i) { if (nodes[i].degree < d) { aloneNodes.push_back(i); } } while (!aloneNodes.empty()) { int nodeId = aloneNodes.front(); aloneNodes.pop_front(); removeNode(nodeId); } int groupSize = -1; int groupId = -1; for (int i = 0; i < n; ++i) { if (!nodes[i].isRemoved && nodes[i].howManyTimesWasVisited == 0) { int currentSize = dfs(i); if (currentSize > groupSize) { groupSize = currentSize; groupId = i; } } } if (groupId == -1) { printf("NIE"); return 0; } findSolution(groupId); sort(solution.begin(), solution.end()); printf("%d\n", solution.size()); int size = solution.size(); for (int i = 0; i < size; ++i) { printf("%d", solution[i]+1); if (i < size - 1) { printf(" "); } } return 0; } |