#include <iostream> #include <vector> #include <set> #include <list> using namespace std; typedef long long LL; LL n, m, d; struct Node { set<Node*> neigbours; LL number; LL order; Node(){order = 0;} void addNeighbour(Node * n) { neigbours.insert(n); order++; }; void removeNeighbour(Node * n) { neigbours.erase(n); order--; } }; void remove(Node * n) { list<Node *> toRemove; n->order=0; toRemove.push_back(n); while (toRemove.size()) { Node * r = toRemove.back(); toRemove.pop_back(); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { (*i)->removeNeighbour(r); if (((*i)->order > 0) && ((*i)->order < d)) { toRemove.push_back(*i); (*i)->order=0; } } } } LL componentSize(Node * n) { LL size=1; list<Node *> toVisit; toVisit.push_back(n); while (toVisit.size()) { Node * r = toVisit.back(); toVisit.pop_back(); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { r->order=0; if ((*i)->order) { toVisit.push_back(*i); (*i)->order=0; size++; } } } return size; } int main() { LL max=0, nodeStart; std::cin >> n >> m >> d; if (d*d > 2*m) { cout << "NIE" << endl; return 0; } vector<Node> v(n); for (LL i=0; i<n; i++) v[i].number=i+1; LL a, b; for (LL i=0; i<m; i++) { std::cin >> a >> b; v[a-1].addNeighbour(&v[b-1]); v[b-1].addNeighbour(&v[a-1]); } for (LL i=0; i<n; i++) { if ((v[i].order > 0) && (v[i].order < d)) remove(&v[i]); } for (LL i=0; i<n; i++) { if ( v[i].order > 0 ) { LL bc = componentSize(&v[i]); if (bc > max) { max=bc; nodeStart = i; } } } if (max < d) { cout << "NIE" << endl; return 0; } cout << max << endl; list<LL> result; list<Node *> toVisit; toVisit.push_back(&v[nodeStart]); while (toVisit.size()) { Node * r = toVisit.back(); toVisit.pop_back(); result.push_back(r->number); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { r->order=1; if ((*i)->order!=1) { toVisit.push_back(*i); (*i)->order=1; } } } result.sort(); for(list<LL>::iterator i=result.begin(); i!=result.end(); i++) cout << *i << " "; 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <iostream> #include <vector> #include <set> #include <list> using namespace std; typedef long long LL; LL n, m, d; struct Node { set<Node*> neigbours; LL number; LL order; Node(){order = 0;} void addNeighbour(Node * n) { neigbours.insert(n); order++; }; void removeNeighbour(Node * n) { neigbours.erase(n); order--; } }; void remove(Node * n) { list<Node *> toRemove; n->order=0; toRemove.push_back(n); while (toRemove.size()) { Node * r = toRemove.back(); toRemove.pop_back(); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { (*i)->removeNeighbour(r); if (((*i)->order > 0) && ((*i)->order < d)) { toRemove.push_back(*i); (*i)->order=0; } } } } LL componentSize(Node * n) { LL size=1; list<Node *> toVisit; toVisit.push_back(n); while (toVisit.size()) { Node * r = toVisit.back(); toVisit.pop_back(); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { r->order=0; if ((*i)->order) { toVisit.push_back(*i); (*i)->order=0; size++; } } } return size; } int main() { LL max=0, nodeStart; std::cin >> n >> m >> d; if (d*d > 2*m) { cout << "NIE" << endl; return 0; } vector<Node> v(n); for (LL i=0; i<n; i++) v[i].number=i+1; LL a, b; for (LL i=0; i<m; i++) { std::cin >> a >> b; v[a-1].addNeighbour(&v[b-1]); v[b-1].addNeighbour(&v[a-1]); } for (LL i=0; i<n; i++) { if ((v[i].order > 0) && (v[i].order < d)) remove(&v[i]); } for (LL i=0; i<n; i++) { if ( v[i].order > 0 ) { LL bc = componentSize(&v[i]); if (bc > max) { max=bc; nodeStart = i; } } } if (max < d) { cout << "NIE" << endl; return 0; } cout << max << endl; list<LL> result; list<Node *> toVisit; toVisit.push_back(&v[nodeStart]); while (toVisit.size()) { Node * r = toVisit.back(); toVisit.pop_back(); result.push_back(r->number); for(set<Node *>::iterator i=r->neigbours.begin(); i!=r->neigbours.end(); i++) { r->order=1; if ((*i)->order!=1) { toVisit.push_back(*i); (*i)->order=1; } } } result.sort(); for(list<LL>::iterator i=result.begin(); i!=result.end(); i++) cout << *i << " "; cout << endl; return 0; } |