#include <cstdio> #include <set> #include <vector> #include <climits> #include <stack> #include <algorithm> using namespace std; stack<int> s; void upheap(int *heap, int *hpos, set<int> *nei, int n, int i) { int l = i / 2; unsigned v = nei[heap[i]].size(); int w = heap[i]; while (l > 0 && nei[heap[l]].size() > v) { heap[i] = heap[l]; hpos[heap[i]] = i; i = l; l /= 2; } heap[i] = w; hpos[heap[i]] = i; } void downheap(int *heap, int *hpos, set<int> *nei, int n, int i) { unsigned v = nei[heap[i]].size(); int w = heap[i]; int k = i; int j; while (k <= n / 2) { j = 2 * k; if (j < n) { if (nei[heap[j]].size() > nei[heap[j+1]].size()) j++; } if (v <= nei[heap[j]].size()) break; heap[k] = heap[j]; hpos[heap[k]] = k; k = j; } heap[k] = w; hpos[heap[k]] = k; } void make_heap(int *heap, int *hpos, set<int> *nei, int n) { for (int i = n / 2; i >= 1; i--) downheap(heap, hpos, nei, n, i); } int getmin(int *heap) { return heap[1]; } void deletemin(int *heap, int *hpos, set<int> *nei, int &n) { heap[1] = heap[n]; hpos[heap[1]] = 1; n--; downheap(heap, hpos, nei, n, 1); } void mark(int node, set<int> *nei, int marker, int *mark, int *visi) { while (!s.empty()) s.pop(); s.push(node); while (!s.empty()) { int v = s.top(); s.pop(); if (visi[v] == 0) { mark[marker]++; for (set<int>::iterator it = nei[v].begin(); it != nei[v].end(); it++) { if (visi[*it] == 0) s.push(*it); } } visi[v] = marker; } } int max(int a, int b) { return a > b ? a : b; } int main() { int n,m,a,b; unsigned d; scanf("%d%d%u", &n, &m, &d); set<int> *NEI = new set<int>[n + 1]; int *HEAP = new int[n + 1]; int *HPOS = new int[n + 1]; int *MARK = new int[n + 1]; int *VISI = new int[n + 1]; for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); NEI[a].insert(b); NEI[b].insert(a); } for (int i = 1; i <= n; i++) { HEAP[i] = i; HPOS[i] = i; MARK[i] = 0; VISI[i] = 0; } make_heap(HEAP, HPOS, NEI, n); while (n > 0 && NEI[getmin(HEAP)].size() < d) { int min = getmin(HEAP); deletemin(HEAP, HPOS, NEI, n); for (set<int>::iterator it = NEI[min].begin(); it != NEI[min].end(); it++) { NEI[*it].erase(min); upheap(HEAP, HPOS, NEI, n, HPOS[*it]); } } int marker = 1; for (int i = 1; i <= n; i++) { if (VISI[HEAP[i]] == 0) { mark(HEAP[i], NEI, marker++, MARK, VISI); } } int max_marker = 0; int mmarker = 0; for (int i = 1; i < marker; i++) { if (MARK[i] > max_marker) { max_marker = MARK[i]; mmarker = i; } } vector<int> solution; for (int i = 1; i <= n; i++) { if (VISI[HEAP[i]] == mmarker ) { solution.push_back(HEAP[i]); } } sort(solution.begin(), solution.end()); if (max_marker > 0) { printf("%d\n", max_marker); for (vector<int>::iterator it = solution.begin(); it != solution.end(); it++) { printf("%d ", *it); } printf("\n"); } else { printf("NIE\n"); } delete [] HEAP; delete [] HPOS; delete [] NEI; delete [] MARK; delete [] VISI; 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 | #include <cstdio> #include <set> #include <vector> #include <climits> #include <stack> #include <algorithm> using namespace std; stack<int> s; void upheap(int *heap, int *hpos, set<int> *nei, int n, int i) { int l = i / 2; unsigned v = nei[heap[i]].size(); int w = heap[i]; while (l > 0 && nei[heap[l]].size() > v) { heap[i] = heap[l]; hpos[heap[i]] = i; i = l; l /= 2; } heap[i] = w; hpos[heap[i]] = i; } void downheap(int *heap, int *hpos, set<int> *nei, int n, int i) { unsigned v = nei[heap[i]].size(); int w = heap[i]; int k = i; int j; while (k <= n / 2) { j = 2 * k; if (j < n) { if (nei[heap[j]].size() > nei[heap[j+1]].size()) j++; } if (v <= nei[heap[j]].size()) break; heap[k] = heap[j]; hpos[heap[k]] = k; k = j; } heap[k] = w; hpos[heap[k]] = k; } void make_heap(int *heap, int *hpos, set<int> *nei, int n) { for (int i = n / 2; i >= 1; i--) downheap(heap, hpos, nei, n, i); } int getmin(int *heap) { return heap[1]; } void deletemin(int *heap, int *hpos, set<int> *nei, int &n) { heap[1] = heap[n]; hpos[heap[1]] = 1; n--; downheap(heap, hpos, nei, n, 1); } void mark(int node, set<int> *nei, int marker, int *mark, int *visi) { while (!s.empty()) s.pop(); s.push(node); while (!s.empty()) { int v = s.top(); s.pop(); if (visi[v] == 0) { mark[marker]++; for (set<int>::iterator it = nei[v].begin(); it != nei[v].end(); it++) { if (visi[*it] == 0) s.push(*it); } } visi[v] = marker; } } int max(int a, int b) { return a > b ? a : b; } int main() { int n,m,a,b; unsigned d; scanf("%d%d%u", &n, &m, &d); set<int> *NEI = new set<int>[n + 1]; int *HEAP = new int[n + 1]; int *HPOS = new int[n + 1]; int *MARK = new int[n + 1]; int *VISI = new int[n + 1]; for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); NEI[a].insert(b); NEI[b].insert(a); } for (int i = 1; i <= n; i++) { HEAP[i] = i; HPOS[i] = i; MARK[i] = 0; VISI[i] = 0; } make_heap(HEAP, HPOS, NEI, n); while (n > 0 && NEI[getmin(HEAP)].size() < d) { int min = getmin(HEAP); deletemin(HEAP, HPOS, NEI, n); for (set<int>::iterator it = NEI[min].begin(); it != NEI[min].end(); it++) { NEI[*it].erase(min); upheap(HEAP, HPOS, NEI, n, HPOS[*it]); } } int marker = 1; for (int i = 1; i <= n; i++) { if (VISI[HEAP[i]] == 0) { mark(HEAP[i], NEI, marker++, MARK, VISI); } } int max_marker = 0; int mmarker = 0; for (int i = 1; i < marker; i++) { if (MARK[i] > max_marker) { max_marker = MARK[i]; mmarker = i; } } vector<int> solution; for (int i = 1; i <= n; i++) { if (VISI[HEAP[i]] == mmarker ) { solution.push_back(HEAP[i]); } } sort(solution.begin(), solution.end()); if (max_marker > 0) { printf("%d\n", max_marker); for (vector<int>::iterator it = solution.begin(); it != solution.end(); it++) { printf("%d ", *it); } printf("\n"); } else { printf("NIE\n"); } delete [] HEAP; delete [] HPOS; delete [] NEI; delete [] MARK; delete [] VISI; return 0; } |