#include <cstdio> #include <vector> using namespace std; const int MAX_N = 200000; vector<int> adj[MAX_N + 1]; int deg[MAX_N + 1]; bool deleted[MAX_N + 1]; int visited[MAX_N + 1]; int component[MAX_N + 1]; int component_cnt[MAX_N + 1]; int heap[MAX_N]; int heap_size = 0; int heap_pos[MAX_N + 1]; inline int parent(int i) { return i / 2; } inline int left(int i) { return i * 2; } inline int right(int i) { return (i * 2) + 1; } void heapify(int i) { int smallest = i; int l = left(i); int r = right(i); if ((l < heap_size) && (deg[heap[l]] < deg[heap[smallest]])) smallest = l; if ((r < heap_size) && (deg[heap[r]] < deg[heap[smallest]])) smallest = r; if (smallest != i) { heap_pos[heap[i]] = smallest; heap_pos[heap[smallest]] = i; int tmp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = tmp; heapify(smallest); } } void build_heap(int n) { heap_size = n; for (int i = 0; i < n; i++) { heap[i] = i + 1; heap_pos[i + 1] = i; } for (int i = n / 2; i >= 0; i--) heapify(i); } int extract_min() { int res = heap[0]; heap[0] = heap[--heap_size]; heap_pos[heap[0]] = 0; heapify(0); return res; } bool empty() { return !(heap_size > 0); } void decrease_key(int i) { while ((i > 0) && (deg[heap[parent(i)]] > deg[heap[i]])) { heap_pos[heap[i]] = parent(i); heap_pos[heap[parent(i)]] = i; int tmp = heap[i]; heap[i] = heap[parent(i)]; heap[parent(i)] = tmp; i = parent(i); } } void dfs_visit(int u, int curr_component) { visited[u] = true; component[u] = curr_component; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if ((!deleted[v]) && (!visited[v])) { dfs_visit(v, curr_component); } } } void dfs(int n) { for (int u = 1; u <= n; u++) { visited[u] = false; component[u] = 0; } int curr_component = 0; for (int u = 1; u <= n; u++) { if ((!deleted[u]) && (!visited[u])) { curr_component += 1; dfs_visit(u, curr_component); } } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 1; i <= n; i++) { deg[i] = 0; deleted[i] = false; } for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); deg[a] += 1; deg[b] += 1; adj[a].push_back(b); adj[b].push_back(a); } build_heap(n); int min_deg = extract_min(); while ((!empty()) && (deg[min_deg] < d)) { // printf("Deleting %d (%d)\n", min_deg, deg[min_deg]); deleted[min_deg] = true; for (int i = 0; i < adj[min_deg].size(); i++) { deg[adj[min_deg][i]] -= 1; decrease_key(heap_pos[adj[min_deg][i]]); } min_deg = extract_min(); } if (empty()) { printf("NIE\n"); } else { dfs(n); for (int i = 1; i <= n; i++) component_cnt[i] = 0; for (int u = 1; u <= n; u++) { if (component[u]) { component_cnt[component[u]]++; } } int res = 0; int res_component; for (int i = 1; i <= n; i++) { if (component_cnt[i] > res) { res = component_cnt[i]; res_component = i; } } printf("%d\n", res); int i = 0; for (int u = 1; u <= n; u++) { if (component[u] == res_component) { i++; if (i == res) printf("%d\n", u); else printf("%d ", u); } } } 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 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 | #include <cstdio> #include <vector> using namespace std; const int MAX_N = 200000; vector<int> adj[MAX_N + 1]; int deg[MAX_N + 1]; bool deleted[MAX_N + 1]; int visited[MAX_N + 1]; int component[MAX_N + 1]; int component_cnt[MAX_N + 1]; int heap[MAX_N]; int heap_size = 0; int heap_pos[MAX_N + 1]; inline int parent(int i) { return i / 2; } inline int left(int i) { return i * 2; } inline int right(int i) { return (i * 2) + 1; } void heapify(int i) { int smallest = i; int l = left(i); int r = right(i); if ((l < heap_size) && (deg[heap[l]] < deg[heap[smallest]])) smallest = l; if ((r < heap_size) && (deg[heap[r]] < deg[heap[smallest]])) smallest = r; if (smallest != i) { heap_pos[heap[i]] = smallest; heap_pos[heap[smallest]] = i; int tmp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = tmp; heapify(smallest); } } void build_heap(int n) { heap_size = n; for (int i = 0; i < n; i++) { heap[i] = i + 1; heap_pos[i + 1] = i; } for (int i = n / 2; i >= 0; i--) heapify(i); } int extract_min() { int res = heap[0]; heap[0] = heap[--heap_size]; heap_pos[heap[0]] = 0; heapify(0); return res; } bool empty() { return !(heap_size > 0); } void decrease_key(int i) { while ((i > 0) && (deg[heap[parent(i)]] > deg[heap[i]])) { heap_pos[heap[i]] = parent(i); heap_pos[heap[parent(i)]] = i; int tmp = heap[i]; heap[i] = heap[parent(i)]; heap[parent(i)] = tmp; i = parent(i); } } void dfs_visit(int u, int curr_component) { visited[u] = true; component[u] = curr_component; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if ((!deleted[v]) && (!visited[v])) { dfs_visit(v, curr_component); } } } void dfs(int n) { for (int u = 1; u <= n; u++) { visited[u] = false; component[u] = 0; } int curr_component = 0; for (int u = 1; u <= n; u++) { if ((!deleted[u]) && (!visited[u])) { curr_component += 1; dfs_visit(u, curr_component); } } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 1; i <= n; i++) { deg[i] = 0; deleted[i] = false; } for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); deg[a] += 1; deg[b] += 1; adj[a].push_back(b); adj[b].push_back(a); } build_heap(n); int min_deg = extract_min(); while ((!empty()) && (deg[min_deg] < d)) { // printf("Deleting %d (%d)\n", min_deg, deg[min_deg]); deleted[min_deg] = true; for (int i = 0; i < adj[min_deg].size(); i++) { deg[adj[min_deg][i]] -= 1; decrease_key(heap_pos[adj[min_deg][i]]); } min_deg = extract_min(); } if (empty()) { printf("NIE\n"); } else { dfs(n); for (int i = 1; i <= n; i++) component_cnt[i] = 0; for (int u = 1; u <= n; u++) { if (component[u]) { component_cnt[component[u]]++; } } int res = 0; int res_component; for (int i = 1; i <= n; i++) { if (component_cnt[i] > res) { res = component_cnt[i]; res_component = i; } } printf("%d\n", res); int i = 0; for (int u = 1; u <= n; u++) { if (component[u] == res_component) { i++; if (i == res) printf("%d\n", u); else printf("%d ", u); } } } return 0; } |