#include <iostream> #include <set> #include <map> #include <stack> int s[200000]; int size[200000]; int main(){ int n, m, d; std::cin >> n >> m >> d; std::set<int> c[n]; while(m--){ int a, b; std::cin >> a >> b; c[a-1].insert(b-1); c[b-1].insert(a-1); } std::stack<int> w; for(int i=0; i<n; ++i){ if(c[i].size() < d){ s[i] = -1; w.push(i); } } while(! w.empty()){ int x = w.top(); w.pop(); for(std::set<int>::iterator it = c[x].begin(); it != c[x].end(); ++it){ c[*it].erase(x); if(c[*it].size() < d && s[*it] != -1){ s[*it] = -1; w.push(*it); } } } int tag = 1; int max = -1; int max_tag = 1; for(int i=0; i<n; ++i){ if(s[i]!=0) continue; w.push(i); s[i] = tag; while(!w.empty()){ int x = w.top(); w.pop(); ++size[tag]; for(std::set<int>::iterator it = c[x].begin(); it != c[x].end(); ++it){ if(s[*it]==0){ w.push(*it); s[*it] = tag;} } } if(size[tag] > max){ max = size[tag]; max_tag = tag; } ++tag; } if(max == -1){ printf("NIE\n"); return 0; } printf("%d\n", max); for(int i=0; i<n; ++i){ if(s[i] == max_tag) printf("%d ", i+1); } printf("\n"); 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 | #include <iostream> #include <set> #include <map> #include <stack> int s[200000]; int size[200000]; int main(){ int n, m, d; std::cin >> n >> m >> d; std::set<int> c[n]; while(m--){ int a, b; std::cin >> a >> b; c[a-1].insert(b-1); c[b-1].insert(a-1); } std::stack<int> w; for(int i=0; i<n; ++i){ if(c[i].size() < d){ s[i] = -1; w.push(i); } } while(! w.empty()){ int x = w.top(); w.pop(); for(std::set<int>::iterator it = c[x].begin(); it != c[x].end(); ++it){ c[*it].erase(x); if(c[*it].size() < d && s[*it] != -1){ s[*it] = -1; w.push(*it); } } } int tag = 1; int max = -1; int max_tag = 1; for(int i=0; i<n; ++i){ if(s[i]!=0) continue; w.push(i); s[i] = tag; while(!w.empty()){ int x = w.top(); w.pop(); ++size[tag]; for(std::set<int>::iterator it = c[x].begin(); it != c[x].end(); ++it){ if(s[*it]==0){ w.push(*it); s[*it] = tag;} } } if(size[tag] > max){ max = size[tag]; max_tag = tag; } ++tag; } if(max == -1){ printf("NIE\n"); return 0; } printf("%d\n", max); for(int i=0; i<n; ++i){ if(s[i] == max_tag) printf("%d ", i+1); } printf("\n"); return 0; } |