#include <set> #include <map> #include <vector> #include <cstdio> #include <algorithm> using namespace std; int n, m, d, a, b; vector<set<int> > adj; vector<int> component, curr_component, max_component; void find_component(int v, int id) { component[v] = id; curr_component.push_back(v); for(set<int>::iterator it = adj[v].begin(); it!=adj[v].end(); ++it) { if (component[*it] != -1) continue; find_component(*it, id); } } int main() { scanf("%d %d %d", &n, &m, &d); adj.resize(n); for(int i=0; i<m; ++i) { scanf("%d %d", &a, &b); --a; --b; adj[a].insert(b); adj[b].insert(a); } vector<int> to_remove; for(int i=0; i<n; ++i) if (adj[i].size() < d) to_remove.push_back(i); while (!to_remove.empty()) { int v = to_remove.back(); to_remove.pop_back(); for(set<int>::iterator it=adj[v].begin(); it!=adj[v].end(); ++it) { adj[*it].erase(v); if (adj[*it].size() == d-1) { to_remove.push_back(*it); } } adj[v].clear(); } int max_id = -1; component.resize(n, -1); max_component.clear(); for(int i=0; i<n; ++i) { if (component[i] != -1) continue; max_id += 1; curr_component.clear(); find_component(i, max_id); if (curr_component.size() > max_component.size()) max_component = curr_component; } if (max_component.size() < d) printf("NIE\n"); else { printf("%d\n", max_component.size()); sort(max_component.begin(), max_component.end()); for(int i=0; i<max_component.size(); ++i) printf("%d ", max_component[i]+1); printf("\n"); } }
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 | #include <set> #include <map> #include <vector> #include <cstdio> #include <algorithm> using namespace std; int n, m, d, a, b; vector<set<int> > adj; vector<int> component, curr_component, max_component; void find_component(int v, int id) { component[v] = id; curr_component.push_back(v); for(set<int>::iterator it = adj[v].begin(); it!=adj[v].end(); ++it) { if (component[*it] != -1) continue; find_component(*it, id); } } int main() { scanf("%d %d %d", &n, &m, &d); adj.resize(n); for(int i=0; i<m; ++i) { scanf("%d %d", &a, &b); --a; --b; adj[a].insert(b); adj[b].insert(a); } vector<int> to_remove; for(int i=0; i<n; ++i) if (adj[i].size() < d) to_remove.push_back(i); while (!to_remove.empty()) { int v = to_remove.back(); to_remove.pop_back(); for(set<int>::iterator it=adj[v].begin(); it!=adj[v].end(); ++it) { adj[*it].erase(v); if (adj[*it].size() == d-1) { to_remove.push_back(*it); } } adj[v].clear(); } int max_id = -1; component.resize(n, -1); max_component.clear(); for(int i=0; i<n; ++i) { if (component[i] != -1) continue; max_id += 1; curr_component.clear(); find_component(i, max_id); if (curr_component.size() > max_component.size()) max_component = curr_component; } if (max_component.size() < d) printf("NIE\n"); else { printf("%d\n", max_component.size()); sort(max_component.begin(), max_component.end()); for(int i=0; i<max_component.size(); ++i) printf("%d ", max_component[i]+1); printf("\n"); } } |