#include<iostream> #include<vector> #include<algorithm> using namespace std; const int MAX=200002; vector<int> tab[MAX]; int iter[MAX], iter_count_; int count_[MAX], odp[MAX]; int n, m, d, x, y; int main(){ cin >> n >> m >> d; for (int i = 0; i <= n; ++i) iter[i]=i+1; for (int i = 0; i < m; ++i) { cin >> x >> y; tab[x].push_back(y); tab[y].push_back(x); count_[x]++; count_[y]++; }; bool found = true; iter_count_ = n; while (found) { found = false; for (int x = 0; x < iter_count_;) { int i = iter[x]; if (count_[i] < d && count_[i]>0) { for (int j = 0; j < tab[i].size(); ++j) { int y = tab[i].at(j); if (count_[y]>0) { count_[y]--; found = true; }; }; count_[i] = 0; iter[x] = iter[--iter_count_]; } else ++x; }; }; vector<int> st, bestst; int bestret = 0, beststart= 0; for (int i = 1; i<=n; ++i) { //cout << "start=" << i << endl; st.clear(); if (count_[i]>=d) { st.push_back(i); count_[i]=0; }; //let's go! for (int x = 0; x < st.size(); ++x) { int p = st.at(x); for (int j = 0; j < tab[p].size(); ++j) { int q = tab[p].at(j); //p->q if (count_[q]>=d) { st.push_back(q); count_[q]=0; } }; }; if (st.size()>bestret) { bestret = st.size(); bestst.clear(); for (int x = 0; x < st.size(); ++x) { bestst.push_back(st.at(x)); }; }; }; if (bestret == 0) { cout << "NIE"; } else { cout << bestret << endl; //sort(bestst.begin(), bestst.end()); for (int i = 0; i < bestst.size(); ++i) odp[bestst.at(i)]=1; for (int i =1; i<=n;++i) if (odp[i]==1) cout << i << " "; }; /* int ret = 0; for (int i = 1; i <= n; ++i) if (count_[i] > 0) ret++; if (ret == 0) { cout << "NIE"; } else { cout << ret << endl; for (int i = 1; i <= n; ++i) if (count_[i] > 0) 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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; const int MAX=200002; vector<int> tab[MAX]; int iter[MAX], iter_count_; int count_[MAX], odp[MAX]; int n, m, d, x, y; int main(){ cin >> n >> m >> d; for (int i = 0; i <= n; ++i) iter[i]=i+1; for (int i = 0; i < m; ++i) { cin >> x >> y; tab[x].push_back(y); tab[y].push_back(x); count_[x]++; count_[y]++; }; bool found = true; iter_count_ = n; while (found) { found = false; for (int x = 0; x < iter_count_;) { int i = iter[x]; if (count_[i] < d && count_[i]>0) { for (int j = 0; j < tab[i].size(); ++j) { int y = tab[i].at(j); if (count_[y]>0) { count_[y]--; found = true; }; }; count_[i] = 0; iter[x] = iter[--iter_count_]; } else ++x; }; }; vector<int> st, bestst; int bestret = 0, beststart= 0; for (int i = 1; i<=n; ++i) { //cout << "start=" << i << endl; st.clear(); if (count_[i]>=d) { st.push_back(i); count_[i]=0; }; //let's go! for (int x = 0; x < st.size(); ++x) { int p = st.at(x); for (int j = 0; j < tab[p].size(); ++j) { int q = tab[p].at(j); //p->q if (count_[q]>=d) { st.push_back(q); count_[q]=0; } }; }; if (st.size()>bestret) { bestret = st.size(); bestst.clear(); for (int x = 0; x < st.size(); ++x) { bestst.push_back(st.at(x)); }; }; }; if (bestret == 0) { cout << "NIE"; } else { cout << bestret << endl; //sort(bestst.begin(), bestst.end()); for (int i = 0; i < bestst.size(); ++i) odp[bestst.at(i)]=1; for (int i =1; i<=n;++i) if (odp[i]==1) cout << i << " "; }; /* int ret = 0; for (int i = 1; i <= n; ++i) if (count_[i] > 0) ret++; if (ret == 0) { cout << "NIE"; } else { cout << ret << endl; for (int i = 1; i <= n; ++i) if (count_[i] > 0) cout << i << " "; cout << endl; }; */ return 0; }; |