#include <cstdio> #include <vector> #define NOT_IN_HEAP 0 struct city { int no, deg; }; std::vector<int> G[200001]; city heap[200001]; int pos[200001]; void heapify(int p, int n) { if(2*p >n) { return; } city m = heap[p]; int pc = 2*p; city c = heap[pc]; if(pc+1 <=n && c.deg>heap[pc+1].deg) { pc ++; c = heap[pc]; } if(c.deg<m.deg) { heap[p] = c; heap[pc] = m; pos[c.no] = p; pos[m.no] = pc; heapify(pc, n); } } int main() { int n, N, m, d; scanf("%d %d %d", &n, &m, &d); N = n; for(int i = 0; i<m;i++) { int a, b; scanf("%d %d", &a, &b); G[a].push_back(b); G[b].push_back(a); } for(int i = 1; i<=n;i++) { heap[i].no = i; heap[i].deg = G[i].size(); pos[i] = i; } for(int i = n/2; i>=1;i--) { heapify(i, n); } while(n > d && heap[1].deg < d) { int k = heap[1].no; pos[k] = NOT_IN_HEAP; heap[1] = heap[n]; pos[heap[1].no] = 1; n--; heapify(1, n); for(int i = 0; i< G[k].size();i++) { int pzm = pos[G[k][i]]; if(pzm == NOT_IN_HEAP) { continue; } heap[pzm].deg --; while(pzm > 1 && heap[pzm>>1].deg > heap[pzm].deg) { pos[heap[pzm].no] = pzm >>1; pos[heap[pzm>>1].no] = pzm; city t = heap[pzm>>1]; heap[pzm>>1] = heap[pzm]; heap[pzm] = t; } } } if(n > d) { printf("%d\n", n); for(int i =1 ;i <= N; i++) { if(pos[i] != NOT_IN_HEAP) { printf("%d ", i); } } } else { printf("NIE"); } 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 | #include <cstdio> #include <vector> #define NOT_IN_HEAP 0 struct city { int no, deg; }; std::vector<int> G[200001]; city heap[200001]; int pos[200001]; void heapify(int p, int n) { if(2*p >n) { return; } city m = heap[p]; int pc = 2*p; city c = heap[pc]; if(pc+1 <=n && c.deg>heap[pc+1].deg) { pc ++; c = heap[pc]; } if(c.deg<m.deg) { heap[p] = c; heap[pc] = m; pos[c.no] = p; pos[m.no] = pc; heapify(pc, n); } } int main() { int n, N, m, d; scanf("%d %d %d", &n, &m, &d); N = n; for(int i = 0; i<m;i++) { int a, b; scanf("%d %d", &a, &b); G[a].push_back(b); G[b].push_back(a); } for(int i = 1; i<=n;i++) { heap[i].no = i; heap[i].deg = G[i].size(); pos[i] = i; } for(int i = n/2; i>=1;i--) { heapify(i, n); } while(n > d && heap[1].deg < d) { int k = heap[1].no; pos[k] = NOT_IN_HEAP; heap[1] = heap[n]; pos[heap[1].no] = 1; n--; heapify(1, n); for(int i = 0; i< G[k].size();i++) { int pzm = pos[G[k][i]]; if(pzm == NOT_IN_HEAP) { continue; } heap[pzm].deg --; while(pzm > 1 && heap[pzm>>1].deg > heap[pzm].deg) { pos[heap[pzm].no] = pzm >>1; pos[heap[pzm>>1].no] = pzm; city t = heap[pzm>>1]; heap[pzm>>1] = heap[pzm]; heap[pzm] = t; } } } if(n > d) { printf("%d\n", n); for(int i =1 ;i <= N; i++) { if(pos[i] != NOT_IN_HEAP) { printf("%d ", i); } } } else { printf("NIE"); } return 0; } |