/* * Copyright (C) 2015 Paweł Widera * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html */ #include <iostream> #include <vector> #include <deque> #include <unordered_set> #include <algorithm> using namespace std; void remove_node(vector<unordered_set<int>>& graph, int node, unordered_set<int>& nodes) { for (auto i : graph[node]) { graph[i].erase(node); } graph[node].clear(); nodes.erase(node); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, d; cin >> n >> m >> d; // read graph int source, target; vector<unordered_set<int>> graph(n); for (int i = 0; i < m; ++i) { cin >> source >> target; graph[source - 1].insert(target - 1); graph[target - 1].insert(source - 1); } // copy nodes (to track nodes left after removals) unordered_set<int> nodes; unordered_set<int> check; for (int i = 0; i < n; ++i) { nodes.insert(i); check.insert(i); } // remove nodes with degree < d while (!check.empty()) { int node = *begin(check); if ((int) graph[node].size() < d) { // make sure all connected nodes are checked later for (auto altered: graph[node]) { check.insert(altered); } remove_node(graph, node, nodes); } check.erase(node); } // graph too small if ((int) nodes.size() < d + 1) { cout << "NIE" << endl; return 0; } unordered_set<int> cities; deque<int> queue; vector<int> best; while (nodes.size() > best.size()) { // find a node with maximum degree int max_degree = *begin(nodes); for (auto node: nodes) { if (graph[max_degree].size() < graph[node].size()) { max_degree = node; } } // breadth-first search queue.push_back(max_degree); cities.insert(max_degree); while (!queue.empty()) { int node = queue[0]; queue.pop_front(); for (auto connected : graph[node]) { if (cities.count(connected) == 0) { cities.insert(connected); queue.push_back(connected); } } } // remove cities from the graph for (auto node: cities) { remove_node(graph, node, nodes); } // remember the largest set of cities if (best.size() < cities.size()) { best.clear(); best.reserve(cities.size()); for (auto node: cities) { best.push_back(node); } } cities.clear(); } cout << best.size() << endl; sort(begin(best), end(best)); for (auto i: best) { cout << i + 1 << " "; } cout << endl; 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 | /* * Copyright (C) 2015 Paweł Widera * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html */ #include <iostream> #include <vector> #include <deque> #include <unordered_set> #include <algorithm> using namespace std; void remove_node(vector<unordered_set<int>>& graph, int node, unordered_set<int>& nodes) { for (auto i : graph[node]) { graph[i].erase(node); } graph[node].clear(); nodes.erase(node); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, d; cin >> n >> m >> d; // read graph int source, target; vector<unordered_set<int>> graph(n); for (int i = 0; i < m; ++i) { cin >> source >> target; graph[source - 1].insert(target - 1); graph[target - 1].insert(source - 1); } // copy nodes (to track nodes left after removals) unordered_set<int> nodes; unordered_set<int> check; for (int i = 0; i < n; ++i) { nodes.insert(i); check.insert(i); } // remove nodes with degree < d while (!check.empty()) { int node = *begin(check); if ((int) graph[node].size() < d) { // make sure all connected nodes are checked later for (auto altered: graph[node]) { check.insert(altered); } remove_node(graph, node, nodes); } check.erase(node); } // graph too small if ((int) nodes.size() < d + 1) { cout << "NIE" << endl; return 0; } unordered_set<int> cities; deque<int> queue; vector<int> best; while (nodes.size() > best.size()) { // find a node with maximum degree int max_degree = *begin(nodes); for (auto node: nodes) { if (graph[max_degree].size() < graph[node].size()) { max_degree = node; } } // breadth-first search queue.push_back(max_degree); cities.insert(max_degree); while (!queue.empty()) { int node = queue[0]; queue.pop_front(); for (auto connected : graph[node]) { if (cities.count(connected) == 0) { cities.insert(connected); queue.push_back(connected); } } } // remove cities from the graph for (auto node: cities) { remove_node(graph, node, nodes); } // remember the largest set of cities if (best.size() < cities.size()) { best.clear(); best.reserve(cities.size()); for (auto node: cities) { best.push_back(node); } } cities.clear(); } cout << best.size() << endl; sort(begin(best), end(best)); for (auto i: best) { cout << i + 1 << " "; } cout << endl; return 0; } |