#include <cstdio> #include <queue> #include <set> #include <vector> using namespace std; #define edge(a,b) V[(a)].insert(b); V[(b)].insert(a) #define FOREACH(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) const int max_N = 200010; int N[max_N]; set<int> V[max_N]; int n,m,d; struct Cmp { bool operator()(const int& a, const int& b) { return V[a].size() > V[b].size(); } }; int dfs(int x, bool* vis, std::set<int>* st = NULL) { int size = 1; vis[x] = true; if (st) st->insert(x); FOREACH(i, V[x]) { if (!vis[*i]) { size += dfs(*i, vis, st); } } return size; } int main() { scanf("%d %d %d",&n,&m,&d); for(int i=0;i<m;++i) { int x,y; scanf("%d %d", &x, &y); edge(x-1,y-1); } priority_queue<int, std::vector<int>, Cmp> pq; bool vis[n]; bool pushed[n]; bool removed[n]; for(int i=0;i<n;++i) { vis[i] = pushed[i] = false; if (V[i].size() < d) { pushed[i] = true; pq.push(i); } } while (pq.size()) { int node = pq.top(); pq.pop(); pushed[node] = false; if (!vis[node]) { FOREACH(it, V[node]) { V[*it].erase(node); if (!vis[*it] && !pushed[*it] && V[*it].size() < d) { pushed[*it] = true; pq.push(*it); } } vis[node] = true; V[node].clear(); } } for (int i=0;i<n;++i) vis[i] = (V[i].size() < d); int x = -1; int mx = 0; for(int i=0;i<n;++i) { if (!vis[i]) { int size = dfs(i, vis); if (size > mx) { mx = size; x = i; } } } if (mx) { for (int i=0;i<n;++i) vis[i] = (V[i].size() < d); std::set<int> res; dfs(x, vis, &res); printf("%d\n",mx); FOREACH(i, res) printf("%d ",*i+1); puts(""); } else puts("NIE"); }
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 | #include <cstdio> #include <queue> #include <set> #include <vector> using namespace std; #define edge(a,b) V[(a)].insert(b); V[(b)].insert(a) #define FOREACH(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) const int max_N = 200010; int N[max_N]; set<int> V[max_N]; int n,m,d; struct Cmp { bool operator()(const int& a, const int& b) { return V[a].size() > V[b].size(); } }; int dfs(int x, bool* vis, std::set<int>* st = NULL) { int size = 1; vis[x] = true; if (st) st->insert(x); FOREACH(i, V[x]) { if (!vis[*i]) { size += dfs(*i, vis, st); } } return size; } int main() { scanf("%d %d %d",&n,&m,&d); for(int i=0;i<m;++i) { int x,y; scanf("%d %d", &x, &y); edge(x-1,y-1); } priority_queue<int, std::vector<int>, Cmp> pq; bool vis[n]; bool pushed[n]; bool removed[n]; for(int i=0;i<n;++i) { vis[i] = pushed[i] = false; if (V[i].size() < d) { pushed[i] = true; pq.push(i); } } while (pq.size()) { int node = pq.top(); pq.pop(); pushed[node] = false; if (!vis[node]) { FOREACH(it, V[node]) { V[*it].erase(node); if (!vis[*it] && !pushed[*it] && V[*it].size() < d) { pushed[*it] = true; pq.push(*it); } } vis[node] = true; V[node].clear(); } } for (int i=0;i<n;++i) vis[i] = (V[i].size() < d); int x = -1; int mx = 0; for(int i=0;i<n;++i) { if (!vis[i]) { int size = dfs(i, vis); if (size > mx) { mx = size; x = i; } } } if (mx) { for (int i=0;i<n;++i) vis[i] = (V[i].size() < d); std::set<int> res; dfs(x, vis, &res); printf("%d\n",mx); FOREACH(i, res) printf("%d ",*i+1); puts(""); } else puts("NIE"); } |