#include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; vector<int>tab[200100]; vector<int>cos1; queue <int> nie; queue <int> q; int bat[200100]; bool vis[200100]; bool vis1[200100]; bool vis2[200100]; int d; void wykresl(){ int v; while(!nie.empty()){ v = nie.front(); nie.pop(); if(vis[v] == false){ vis[v] = true; for(int i=0;i<tab[v].size();i++){ bat[tab[v][i]] = bat[tab[v][i]] - 1; if(bat[tab[v][i]] < d) nie.push(tab[v][i]); } } } } int bfs(int u){ q.push(u); int wyn=0; while(!q.empty()){ u = q.front(); q.pop(); if(vis1[u] == false){ wyn++; vis1[u] = true; for(int i=0;i<tab[u].size();i++){ if(bat[tab[u][i]] >= d) q.push(tab[u][i]); } } } return wyn; } void bfsw(int u){ q.push(u); while(!q.empty()){ u = q.front(); q.pop(); if(vis2[u] == false){ cos1.push_back(u); vis2[u] = true; for(int i=0;i<tab[u].size();i++){ if(bat[tab[u][i]] >= d) q.push(tab[u][i]); } } } } int main(){ int n,m,a,b; cin >> n >> m >> d; for(int i=0;i<m;i++){ cin >> a >> b; tab[a].push_back(b); tab[b].push_back(a); } for(int i=1;i<=n;i++){ bat[i] = tab[i].size(); if(tab[i].size() < d) nie.push(i); } wykresl(); int spr,maxx =0,maxw=0; for(int i=1;i<=n;i++){ if(vis1[i] == false ) if(bat[i]>=d){ spr = bfs(i); if(maxx < spr){ maxx = spr; maxw = i; } } } if(maxx == 0) cout << "NIE"; else{ cout << maxx << endl; bfsw(maxw); sort(cos1.begin(),cos1.end()); for(int i=0;i<cos1.size();i++) cout << cos1[i] << " "; } 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 | #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; vector<int>tab[200100]; vector<int>cos1; queue <int> nie; queue <int> q; int bat[200100]; bool vis[200100]; bool vis1[200100]; bool vis2[200100]; int d; void wykresl(){ int v; while(!nie.empty()){ v = nie.front(); nie.pop(); if(vis[v] == false){ vis[v] = true; for(int i=0;i<tab[v].size();i++){ bat[tab[v][i]] = bat[tab[v][i]] - 1; if(bat[tab[v][i]] < d) nie.push(tab[v][i]); } } } } int bfs(int u){ q.push(u); int wyn=0; while(!q.empty()){ u = q.front(); q.pop(); if(vis1[u] == false){ wyn++; vis1[u] = true; for(int i=0;i<tab[u].size();i++){ if(bat[tab[u][i]] >= d) q.push(tab[u][i]); } } } return wyn; } void bfsw(int u){ q.push(u); while(!q.empty()){ u = q.front(); q.pop(); if(vis2[u] == false){ cos1.push_back(u); vis2[u] = true; for(int i=0;i<tab[u].size();i++){ if(bat[tab[u][i]] >= d) q.push(tab[u][i]); } } } } int main(){ int n,m,a,b; cin >> n >> m >> d; for(int i=0;i<m;i++){ cin >> a >> b; tab[a].push_back(b); tab[b].push_back(a); } for(int i=1;i<=n;i++){ bat[i] = tab[i].size(); if(tab[i].size() < d) nie.push(i); } wykresl(); int spr,maxx =0,maxw=0; for(int i=1;i<=n;i++){ if(vis1[i] == false ) if(bat[i]>=d){ spr = bfs(i); if(maxx < spr){ maxx = spr; maxw = i; } } } if(maxx == 0) cout << "NIE"; else{ cout << maxx << endl; bfsw(maxw); sort(cos1.begin(),cos1.end()); for(int i=0;i<cos1.size();i++) cout << cos1[i] << " "; } return 0; } |