#include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <list> using namespace std; class city { public: int N,f; set<int> d; city(int n); }; city::city(int n) { N = n; f = 0; } bool cmp(const city* first, const city* second) { return ( first->d.size() < second->d.size() ); } vector<city*> x; int nstep(int n,int f,int c) { int tc = c,mc = c; set<int>::iterator is; if (x[n-1]->f == f) return c; x[n-1]->f = f; mc++; for (is = x[n-1]->d.begin(); is != x[n-1]->d.end(); ++is) { tc = nstep(*is,f,mc); if (mc < tc) mc = tc; } return mc; } int main() { int N,M,D,i,a,b,c,F,mc,mF; city *c_t; vector<city*>::iterator ix; list<city*> y; list<city*>::iterator iy; set<int>::iterator is; scanf("%d %d %d",&N,&M,&D); for (i = 0; i < N; i++) { c_t = new city(i+1); x.push_back(c_t); y.push_back(c_t); } for (i = 0; i < M; i++) { scanf("%d %d",&a,&b); x[a-1]->d.insert(b); x[b-1]->d.insert(a); } y.sort(cmp); iy = y.begin(); c = 0; while (iy != y.end()) { c_t = *iy; if (c_t->f == 0 && c_t->d.size() < D) { y.pop_front(); for (is = c_t->d.begin(); is != c_t->d.end(); ++is) { x[*is-1]->d.erase(c_t->N); x[*is-1]->f = 0; c_t->f = 0; c = 0; } } else if (c_t->f == 0 && c_t->d.size() >= D) { c_t->f = 1; } else { c++; } if (c == y.size()) iy = y.end(); else iy = y.begin(); } //spojnosc for (iy = y.begin(); iy != y.end(); ++iy) (*iy)->f = 0; F = 1; mc = 0; for (iy = y.begin(); iy != y.end(); ++iy) { if ((*iy)->f == 0) { c = nstep((*iy)->N,F,0); // printf("cnt: %d\n",c); if (c > mc) {mc = c; mF = F;} F++; } } if (mc > 0) { printf("%d\n",mc); for (ix = x.begin(); ix != x.end(); ++ix) { if ((*ix)->f == mF) { printf("%d ",(*ix)->N); } } printf("\n"); } else printf("NIE\n"); for (i = 0; i < N; i++) { delete x[i]; } 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <list> using namespace std; class city { public: int N,f; set<int> d; city(int n); }; city::city(int n) { N = n; f = 0; } bool cmp(const city* first, const city* second) { return ( first->d.size() < second->d.size() ); } vector<city*> x; int nstep(int n,int f,int c) { int tc = c,mc = c; set<int>::iterator is; if (x[n-1]->f == f) return c; x[n-1]->f = f; mc++; for (is = x[n-1]->d.begin(); is != x[n-1]->d.end(); ++is) { tc = nstep(*is,f,mc); if (mc < tc) mc = tc; } return mc; } int main() { int N,M,D,i,a,b,c,F,mc,mF; city *c_t; vector<city*>::iterator ix; list<city*> y; list<city*>::iterator iy; set<int>::iterator is; scanf("%d %d %d",&N,&M,&D); for (i = 0; i < N; i++) { c_t = new city(i+1); x.push_back(c_t); y.push_back(c_t); } for (i = 0; i < M; i++) { scanf("%d %d",&a,&b); x[a-1]->d.insert(b); x[b-1]->d.insert(a); } y.sort(cmp); iy = y.begin(); c = 0; while (iy != y.end()) { c_t = *iy; if (c_t->f == 0 && c_t->d.size() < D) { y.pop_front(); for (is = c_t->d.begin(); is != c_t->d.end(); ++is) { x[*is-1]->d.erase(c_t->N); x[*is-1]->f = 0; c_t->f = 0; c = 0; } } else if (c_t->f == 0 && c_t->d.size() >= D) { c_t->f = 1; } else { c++; } if (c == y.size()) iy = y.end(); else iy = y.begin(); } //spojnosc for (iy = y.begin(); iy != y.end(); ++iy) (*iy)->f = 0; F = 1; mc = 0; for (iy = y.begin(); iy != y.end(); ++iy) { if ((*iy)->f == 0) { c = nstep((*iy)->N,F,0); // printf("cnt: %d\n",c); if (c > mc) {mc = c; mF = F;} F++; } } if (mc > 0) { printf("%d\n",mc); for (ix = x.begin(); ix != x.end(); ++ix) { if ((*ix)->f == mF) { printf("%d ",(*ix)->N); } } printf("\n"); } else printf("NIE\n"); for (i = 0; i < N; i++) { delete x[i]; } return 0; } |