#include <iostream> #include <set> using namespace std; set <int> v[200005]; set <int> del, del_e[200005]; set <int> bfs_out, bfs; bool odw[200005]; int main(){ int n, m, k; cin >> n >> m >> k; for(int i=0;i<m;i++){ int a, b; cin >> a >> b; v[a].insert(b); v[b].insert(a); } for(int i=1;i<n+1;i++){ if(v[i].size()<k){ for(set<int>::iterator it=v[i].begin(); it!=v[i].end(); ++it){ del.insert(*it); del_e[*it].insert(i); } v[i].clear(); } } while(!del.empty()){ int act = *del.begin(); del.erase(del.begin()); while(!del_e[act].empty()){ v[act].erase(*del_e[act].begin()); del_e[act].erase(del_e[act].begin()); } if(v[act].size()<k){ for(set<int>::iterator it=v[act].begin(); it!=v[act].end(); ++it){ del.insert(*it); del_e[*it].insert(act); } } } set <int> max_v; for(int i=1;i<n+1;i++){ if(odw[i]==0&&!v[i].empty()){ bfs.insert(i); odw[i] = 1; while(!bfs.empty()){ int act = *bfs.begin(); bfs_out.insert(act); bfs.erase(bfs.begin()); for(set<int>::iterator it=v[act].begin(); it!=v[act].end(); ++it){ if(odw[*it]==0){ odw[*it] = 1; bfs.insert(*it); } } } if(max_v.size()<bfs_out.size()) max_v.swap(bfs_out); bfs_out.clear(); } } if(max_v.empty()) cout << "NIE"; else{ cout << max_v.size() << "\n"; for(set<int>::iterator it=max_v.begin(); it!=max_v.end(); ++it) cout << *it << " "; } 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 | #include <iostream> #include <set> using namespace std; set <int> v[200005]; set <int> del, del_e[200005]; set <int> bfs_out, bfs; bool odw[200005]; int main(){ int n, m, k; cin >> n >> m >> k; for(int i=0;i<m;i++){ int a, b; cin >> a >> b; v[a].insert(b); v[b].insert(a); } for(int i=1;i<n+1;i++){ if(v[i].size()<k){ for(set<int>::iterator it=v[i].begin(); it!=v[i].end(); ++it){ del.insert(*it); del_e[*it].insert(i); } v[i].clear(); } } while(!del.empty()){ int act = *del.begin(); del.erase(del.begin()); while(!del_e[act].empty()){ v[act].erase(*del_e[act].begin()); del_e[act].erase(del_e[act].begin()); } if(v[act].size()<k){ for(set<int>::iterator it=v[act].begin(); it!=v[act].end(); ++it){ del.insert(*it); del_e[*it].insert(act); } } } set <int> max_v; for(int i=1;i<n+1;i++){ if(odw[i]==0&&!v[i].empty()){ bfs.insert(i); odw[i] = 1; while(!bfs.empty()){ int act = *bfs.begin(); bfs_out.insert(act); bfs.erase(bfs.begin()); for(set<int>::iterator it=v[act].begin(); it!=v[act].end(); ++it){ if(odw[*it]==0){ odw[*it] = 1; bfs.insert(*it); } } } if(max_v.size()<bfs_out.size()) max_v.swap(bfs_out); bfs_out.clear(); } } if(max_v.empty()) cout << "NIE"; else{ cout << max_v.size() << "\n"; for(set<int>::iterator it=max_v.begin(); it!=max_v.end(); ++it) cout << *it << " "; } return 0; } |