#include <iostream> #include <vector> #include <stdio.h> using namespace std; class c { public: c(void); bool good; bool checked; bool listed; long connections; vector<long> s; }; c::c() { good = true; checked = false; connections = 0; listed = false; } long n,m,d; c cits[200001]; long checkGraph(long current) { long res = 0; vector<long> checks; checks.push_back(current); while (checks.size()) { current = checks.back(); checks.pop_back(); if(!cits[current].good || cits[current].checked) continue; res++; cits[current].checked = true; vector<long>::const_iterator it; for(it=cits[current].s.begin(); it!=cits[current].s.end(); ++it) { checks.push_back(*it); } } return res; } void listGraph(long current, long n) { vector<long> checks; checks.push_back(current); while (checks.size()) { current = checks.back(); checks.pop_back(); if(!cits[current].good || cits[current].listed) continue; cits[current].listed = true; vector<long>::const_iterator it; for(it=cits[current].s.begin(); it!=cits[current].s.end(); ++it) { checks.push_back(*it); } } for(long i=1; i<=n; i++) { if(cits[i].listed) printf("%ld ", i); } } int main() { vector<long> checks; scanf("%ld %ld %ld", &n, &m, &d); for(int i=0; i<m; i++) { long a,b; scanf("%ld %ld", &a, &b); cits[a].s.push_back(b); cits[b].s.push_back(a); } //set initial list of connections for(long i=1; i<=n; i++) { cits[i].connections = cits[i].s.size(); } //add all nodes to check list for(long i=1; i<=n; i++) { checks.push_back(i); } while (checks.size()) { long a = checks.back(); checks.pop_back(); if(!cits[a].good) continue; if(cits[a].connections>=d) continue; cits[a].good = false; vector<long>::const_iterator it; for(it=cits[a].s.begin(); it!=cits[a].s.end(); ++it) { if(cits[*it].good) { cits[*it].connections--; checks.push_back(*it); } } } //find the biggest graph long res = 0; long best_node = 0; long res_cur; long cur = 1; for(long i = 1; i<=n; i++) { res_cur = checkGraph(i++); if(res_cur>res) { res = res_cur; best_node = i; } } if(res>0) { printf("%ld\n", res); listGraph(best_node, n); } 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 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 | #include <iostream> #include <vector> #include <stdio.h> using namespace std; class c { public: c(void); bool good; bool checked; bool listed; long connections; vector<long> s; }; c::c() { good = true; checked = false; connections = 0; listed = false; } long n,m,d; c cits[200001]; long checkGraph(long current) { long res = 0; vector<long> checks; checks.push_back(current); while (checks.size()) { current = checks.back(); checks.pop_back(); if(!cits[current].good || cits[current].checked) continue; res++; cits[current].checked = true; vector<long>::const_iterator it; for(it=cits[current].s.begin(); it!=cits[current].s.end(); ++it) { checks.push_back(*it); } } return res; } void listGraph(long current, long n) { vector<long> checks; checks.push_back(current); while (checks.size()) { current = checks.back(); checks.pop_back(); if(!cits[current].good || cits[current].listed) continue; cits[current].listed = true; vector<long>::const_iterator it; for(it=cits[current].s.begin(); it!=cits[current].s.end(); ++it) { checks.push_back(*it); } } for(long i=1; i<=n; i++) { if(cits[i].listed) printf("%ld ", i); } } int main() { vector<long> checks; scanf("%ld %ld %ld", &n, &m, &d); for(int i=0; i<m; i++) { long a,b; scanf("%ld %ld", &a, &b); cits[a].s.push_back(b); cits[b].s.push_back(a); } //set initial list of connections for(long i=1; i<=n; i++) { cits[i].connections = cits[i].s.size(); } //add all nodes to check list for(long i=1; i<=n; i++) { checks.push_back(i); } while (checks.size()) { long a = checks.back(); checks.pop_back(); if(!cits[a].good) continue; if(cits[a].connections>=d) continue; cits[a].good = false; vector<long>::const_iterator it; for(it=cits[a].s.begin(); it!=cits[a].s.end(); ++it) { if(cits[*it].good) { cits[*it].connections--; checks.push_back(*it); } } } //find the biggest graph long res = 0; long best_node = 0; long res_cur; long cur = 1; for(long i = 1; i<=n; i++) { res_cur = checkGraph(i++); if(res_cur>res) { res = res_cur; best_node = i; } } if(res>0) { printf("%ld\n", res); listGraph(best_node, n); } else printf("NIE"); return 0; } |