#include<bits/stdc++.h> using namespace std; const int max_n = 200006; vector< pair<int, int> > city[max_n]; vector<int> cities_ans; /// city[i].first - do którego miasta idzie krawedz/// city[i].second - ktora to jest krawedz na liscie tamatego miasta int n,m, d, kolor[max_n]; queue<int> Q_del; /// wierzcholki do usuniecia bool na_Q_del[max_n]; int main() { scanf("%d%d%d", &n, &m, &d); for(int i=0; i<m; ++i) { int a, b, size_a, size_b; scanf("%d%d", &a, &b); size_a = city[a].size(); size_b = city[b].size(); city[a].push_back(make_pair(b, size_b)); city[b].push_back(make_pair(a, size_a)); } for(int i=0; i<n; ++i) if(city[i].size()< d ) { Q_del.push(i); na_Q_del[i] =1; } while(!Q_del.empty()) /// usowanie krawedzi z grafu, tak aby zaden wierzcholek mial 0 kraw lub wiecej= niż d { int w = Q_del.front(); Q_del.pop(); for( int i =0; i< city[w].size(); ++i) { /// usuowanie krawedzi z city[w][i].first do w int idzie_do = city[w][i].first, numer_w_liscie = city[w][i].second; if( numer_w_liscie != city[idzie_do].size()-1) { int ostatinia = city[idzie_do].back().first, jej_nr = city[idzie_do].back().second; city[ostatinia][jej_nr].second = numer_w_liscie; swap(city[idzie_do][numer_w_liscie], city[idzie_do].back()); } if( city[idzie_do].size()>0) city[idzie_do].resize(city[idzie_do].size()-1); if(city[idzie_do].size()<d and na_Q_del[idzie_do]==0) { Q_del.push(idzie_do); na_Q_del[idzie_do]=1; } } } int ans = 0, runda =0, ans_runda=0; for( int i =1; i<=n; ++i) { if(na_Q_del[i] == 0) { runda++; kolor[i] = runda; int curr_ans =1; Q_del.push(i); na_Q_del[i]=1; while(!Q_del.empty()) { int w = Q_del.front(); Q_del.pop(); for(int i=0; i<city[w].size(); ++i) if( na_Q_del[ city[w][i].first] ==0 ) { kolor[city[w][i].first] = runda; curr_ans++; Q_del.push(city[w][i].first); na_Q_del[city[w][i].first] = 1; } } if( curr_ans > ans) { ans = curr_ans; ans_runda = runda; } } } if(ans == 0) printf("NIE\n"); else { for( int i=1; i<=n; ++i) if( kolor[i] == ans_runda) cities_ans.push_back(i); sort(cities_ans.begin(), cities_ans.end()); printf("%d\n", ans); for( int j=0; j<cities_ans.size(); ++j) printf("%d ", cities_ans[j]); } }
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 | #include<bits/stdc++.h> using namespace std; const int max_n = 200006; vector< pair<int, int> > city[max_n]; vector<int> cities_ans; /// city[i].first - do którego miasta idzie krawedz/// city[i].second - ktora to jest krawedz na liscie tamatego miasta int n,m, d, kolor[max_n]; queue<int> Q_del; /// wierzcholki do usuniecia bool na_Q_del[max_n]; int main() { scanf("%d%d%d", &n, &m, &d); for(int i=0; i<m; ++i) { int a, b, size_a, size_b; scanf("%d%d", &a, &b); size_a = city[a].size(); size_b = city[b].size(); city[a].push_back(make_pair(b, size_b)); city[b].push_back(make_pair(a, size_a)); } for(int i=0; i<n; ++i) if(city[i].size()< d ) { Q_del.push(i); na_Q_del[i] =1; } while(!Q_del.empty()) /// usowanie krawedzi z grafu, tak aby zaden wierzcholek mial 0 kraw lub wiecej= niż d { int w = Q_del.front(); Q_del.pop(); for( int i =0; i< city[w].size(); ++i) { /// usuowanie krawedzi z city[w][i].first do w int idzie_do = city[w][i].first, numer_w_liscie = city[w][i].second; if( numer_w_liscie != city[idzie_do].size()-1) { int ostatinia = city[idzie_do].back().first, jej_nr = city[idzie_do].back().second; city[ostatinia][jej_nr].second = numer_w_liscie; swap(city[idzie_do][numer_w_liscie], city[idzie_do].back()); } if( city[idzie_do].size()>0) city[idzie_do].resize(city[idzie_do].size()-1); if(city[idzie_do].size()<d and na_Q_del[idzie_do]==0) { Q_del.push(idzie_do); na_Q_del[idzie_do]=1; } } } int ans = 0, runda =0, ans_runda=0; for( int i =1; i<=n; ++i) { if(na_Q_del[i] == 0) { runda++; kolor[i] = runda; int curr_ans =1; Q_del.push(i); na_Q_del[i]=1; while(!Q_del.empty()) { int w = Q_del.front(); Q_del.pop(); for(int i=0; i<city[w].size(); ++i) if( na_Q_del[ city[w][i].first] ==0 ) { kolor[city[w][i].first] = runda; curr_ans++; Q_del.push(city[w][i].first); na_Q_del[city[w][i].first] = 1; } } if( curr_ans > ans) { ans = curr_ans; ans_runda = runda; } } } if(ans == 0) printf("NIE\n"); else { for( int i=1; i<=n; ++i) if( kolor[i] == ans_runda) cities_ans.push_back(i); sort(cities_ans.begin(), cities_ans.end()); printf("%d\n", ans); for( int j=0; j<cities_ans.size(); ++j) printf("%d ", cities_ans[j]); } } |