#include<iostream> #include<algorithm> #include<vector> #include<queue> //#define int long long using namespace std; const int MAX_N = 200002; int n, m, d, counting; int deg[MAX_N]; bool good[MAX_N]; bool vis[MAX_N]; queue<int> Q; vector<int> resset; struct edge { int dir, it; }; vector<edge> G[MAX_N]; void bfs() { while(!Q.empty()) { int act=Q.front(); Q.pop(); if(deg[act]>=d || deg[act]==0) continue; //guardian deg[act]=0; for(int i=0; i<G[act].size(); ++i) { int lui=G[act][i].dir; int ou=G[act][i].it; deg[lui]--; if(deg[lui]<d) good[lui]=false, Q.push(lui); swap(G[lui][ou], G[lui].back()); G[G[lui][ou].dir][G[lui][ou].it].it=ou; G[lui].pop_back(); } G[act].clear(); } } void resbfs(int start, bool printing) { vis[start]=true; queue<int> P; P.push(start); counting++; while(!P.empty()) { int act=P.front(); P.pop(); vis[act]=true; if(printing) resset.push_back(act); for(int i=0; i<G[act].size(); ++i) { int x=G[act][i].dir; if(!vis[x]) {vis[x]=true; P.push(x); counting++;} } } } main() { ios_base::sync_with_stdio(0); cin >> n >> m >> d; for(int i=0; i<=n+1; ++i) {deg[i]=0; good[i]=true; vis[i]=false;} for(int i=1; i<=m; ++i) { int a, b; cin >> a >> b; edge add; add.dir=b; add.it=G[b].size(); G[a].push_back(add); add.dir=a; add.it=G[a].size()-1; G[b].push_back(add); deg[a]++; deg[b]++; } for(int i=1; i<=n; ++i) { if(deg[i]<d) { Q.push(i); good[i]=false; } } bfs(); int res=0, numofvertex=-1; for(int i=1; i<=n; ++i) { if((good[i]) && (vis[i]==false)) { counting=0; resbfs(i, false); if(counting>res) { res=counting; numofvertex=i; } } } if(res>=2) { cout << res << endl; for(int i=0; i<=n+1; ++i) vis[i]=false; resbfs(numofvertex, true); sort(resset.begin(), resset.end()); for(int i=0; i<resset.size(); ++i) cout << resset[i] << " "; cout << endl; } else cout << "NIE" << endl; 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 | #include<iostream> #include<algorithm> #include<vector> #include<queue> //#define int long long using namespace std; const int MAX_N = 200002; int n, m, d, counting; int deg[MAX_N]; bool good[MAX_N]; bool vis[MAX_N]; queue<int> Q; vector<int> resset; struct edge { int dir, it; }; vector<edge> G[MAX_N]; void bfs() { while(!Q.empty()) { int act=Q.front(); Q.pop(); if(deg[act]>=d || deg[act]==0) continue; //guardian deg[act]=0; for(int i=0; i<G[act].size(); ++i) { int lui=G[act][i].dir; int ou=G[act][i].it; deg[lui]--; if(deg[lui]<d) good[lui]=false, Q.push(lui); swap(G[lui][ou], G[lui].back()); G[G[lui][ou].dir][G[lui][ou].it].it=ou; G[lui].pop_back(); } G[act].clear(); } } void resbfs(int start, bool printing) { vis[start]=true; queue<int> P; P.push(start); counting++; while(!P.empty()) { int act=P.front(); P.pop(); vis[act]=true; if(printing) resset.push_back(act); for(int i=0; i<G[act].size(); ++i) { int x=G[act][i].dir; if(!vis[x]) {vis[x]=true; P.push(x); counting++;} } } } main() { ios_base::sync_with_stdio(0); cin >> n >> m >> d; for(int i=0; i<=n+1; ++i) {deg[i]=0; good[i]=true; vis[i]=false;} for(int i=1; i<=m; ++i) { int a, b; cin >> a >> b; edge add; add.dir=b; add.it=G[b].size(); G[a].push_back(add); add.dir=a; add.it=G[a].size()-1; G[b].push_back(add); deg[a]++; deg[b]++; } for(int i=1; i<=n; ++i) { if(deg[i]<d) { Q.push(i); good[i]=false; } } bfs(); int res=0, numofvertex=-1; for(int i=1; i<=n; ++i) { if((good[i]) && (vis[i]==false)) { counting=0; resbfs(i, false); if(counting>res) { res=counting; numofvertex=i; } } } if(res>=2) { cout << res << endl; for(int i=0; i<=n+1; ++i) vis[i]=false; resbfs(numofvertex, true); sort(resset.begin(), resset.end()); for(int i=0; i<resset.size(); ++i) cout << resset[i] << " "; cout << endl; } else cout << "NIE" << endl; return 0; } |