#include <cstdio> #include <algorithm> #include <vector> using namespace std; #define pb push_back #define si size() const int maxn=200005; vector<int> G[maxn]; bool vis[maxn]; int k[maxn]; int dfs(int u){ int res=1; vis[u]=1; for(int i=0;i<G[u].si;++i){ int v=G[u][i]; if(!vis[v]){ k[v]=k[u]; res+=dfs(v); } } return res; } int main() { int n, m, d, a, b; scanf("%d%d%d", &n, &m, &d); while(m--){ scanf("%d%d", &a, &b); G[a].pb(b); G[b].pb(a); ++k[a]; ++k[b]; } vector<int> Q; for(int i=1;i<=n;++i) if(k[i]<d){ vis[i]=1; Q.pb(i); } for(int j=0;j<Q.si;++j){ int u=Q[j]; for(int i=0;i<G[u].si;++i){ int v=G[u][i]; --k[v]; if(!vis[v] && k[v]<d){ vis[v]=1; Q.pb(v); } } } for(int i=1;i<=n;++i) k[i]=-1; int r, p=0, q=0; for(int i=1;i<=n;++i) if(!vis[i]){ k[i]=i; r=dfs(i); if(q<r){ q=r; p=i; } } Q.clear(); for(int i=1;i<=n;++i) if(k[i]==p) Q.pb(i); if(!Q.empty()){ printf("%d\n", Q.si); for(int i=0;i<Q.si;++i) printf("%d ", Q[i]); }else printf("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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; #define pb push_back #define si size() const int maxn=200005; vector<int> G[maxn]; bool vis[maxn]; int k[maxn]; int dfs(int u){ int res=1; vis[u]=1; for(int i=0;i<G[u].si;++i){ int v=G[u][i]; if(!vis[v]){ k[v]=k[u]; res+=dfs(v); } } return res; } int main() { int n, m, d, a, b; scanf("%d%d%d", &n, &m, &d); while(m--){ scanf("%d%d", &a, &b); G[a].pb(b); G[b].pb(a); ++k[a]; ++k[b]; } vector<int> Q; for(int i=1;i<=n;++i) if(k[i]<d){ vis[i]=1; Q.pb(i); } for(int j=0;j<Q.si;++j){ int u=Q[j]; for(int i=0;i<G[u].si;++i){ int v=G[u][i]; --k[v]; if(!vis[v] && k[v]<d){ vis[v]=1; Q.pb(v); } } } for(int i=1;i<=n;++i) k[i]=-1; int r, p=0, q=0; for(int i=1;i<=n;++i) if(!vis[i]){ k[i]=i; r=dfs(i); if(q<r){ q=r; p=i; } } Q.clear(); for(int i=1;i<=n;++i) if(k[i]==p) Q.pb(i); if(!Q.empty()){ printf("%d\n", Q.si); for(int i=0;i<Q.si;++i) printf("%d ", Q[i]); }else printf("NIE"); } |