#include <cstdio> #include <algorithm> #include <vector> #include <list> using namespace std; typedef vector<int> iv; typedef iv::iterator ivi; struct ln; typedef list<ln> lnl; typedef lnl::iterator lnli; struct ln {int t;lnli b;}; struct nd {lnl l;int ls;int c;nd():ls(0),c(-1){}} g[200000]; void dfs1(int i,int d){ iv m;int j; m.push_back(i); while(!m.empty()){ j=m.back(); m.pop_back(); if(!g[j].ls||g[j].ls>=d)continue; for(lnli it=g[j].l.begin();it!=g[j].l.end();++it){ g[it->t].l.erase(it->b);--g[it->t].ls; if(g[it->t].ls&&g[it->t].ls<d) m.push_back(it->t); } g[j].l.clear();g[j].ls=0; } } int dfs2(int i,int c){ iv m;int s=0,j; m.push_back(i); while(!m.empty()){ j=m.back(); m.pop_back(); if(g[j].c!=-1)continue; g[j].c=c;++s; for(lnli it=g[j].l.begin();it!=g[j].l.end();++it) if(!g[it->t].l.empty()&&g[it->t].c==-1)m.push_back(it->t); } return s; } int main(){ int n,m,d,i,u,v;iv r; scanf("%d%d%d",&n,&m,&d); for(i=0;i<m;++i){ scanf("%d%d",&u,&v);--u;--v; lnli ai=g[u].l.insert(g[u].l.end(),ln()),bi=g[v].l.insert(g[v].l.end(),ln()); ++g[u].ls;++g[v].ls; ai->t=v;ai->b=bi;bi->t=u;bi->b=ai; } for(i=0;i<n;++i) if(g[i].ls&&g[i].ls<d) dfs1(i,d); int c=0,bc=-1,bs=0; for(i=0;i<n;++i){ if(!g[i].l.empty()&&g[i].c==-1){ int s=dfs2(i,c); if(s>bs){bs=s;bc=c;} ++c; } } if(!bs)return!printf("NIE\n"); for(i=0;i<n;++i)if(g[i].c==bc)r.push_back(i); sort(r.begin(),r.end()); printf("%d\n",static_cast<int>(r.size())); for(ivi it=r.begin();it!=r.end();++it)if(it==r.begin())printf("%d",1+*it);else printf(" %d",1+*it); return!printf("\n"); }
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 | #include <cstdio> #include <algorithm> #include <vector> #include <list> using namespace std; typedef vector<int> iv; typedef iv::iterator ivi; struct ln; typedef list<ln> lnl; typedef lnl::iterator lnli; struct ln {int t;lnli b;}; struct nd {lnl l;int ls;int c;nd():ls(0),c(-1){}} g[200000]; void dfs1(int i,int d){ iv m;int j; m.push_back(i); while(!m.empty()){ j=m.back(); m.pop_back(); if(!g[j].ls||g[j].ls>=d)continue; for(lnli it=g[j].l.begin();it!=g[j].l.end();++it){ g[it->t].l.erase(it->b);--g[it->t].ls; if(g[it->t].ls&&g[it->t].ls<d) m.push_back(it->t); } g[j].l.clear();g[j].ls=0; } } int dfs2(int i,int c){ iv m;int s=0,j; m.push_back(i); while(!m.empty()){ j=m.back(); m.pop_back(); if(g[j].c!=-1)continue; g[j].c=c;++s; for(lnli it=g[j].l.begin();it!=g[j].l.end();++it) if(!g[it->t].l.empty()&&g[it->t].c==-1)m.push_back(it->t); } return s; } int main(){ int n,m,d,i,u,v;iv r; scanf("%d%d%d",&n,&m,&d); for(i=0;i<m;++i){ scanf("%d%d",&u,&v);--u;--v; lnli ai=g[u].l.insert(g[u].l.end(),ln()),bi=g[v].l.insert(g[v].l.end(),ln()); ++g[u].ls;++g[v].ls; ai->t=v;ai->b=bi;bi->t=u;bi->b=ai; } for(i=0;i<n;++i) if(g[i].ls&&g[i].ls<d) dfs1(i,d); int c=0,bc=-1,bs=0; for(i=0;i<n;++i){ if(!g[i].l.empty()&&g[i].c==-1){ int s=dfs2(i,c); if(s>bs){bs=s;bc=c;} ++c; } } if(!bs)return!printf("NIE\n"); for(i=0;i<n;++i)if(g[i].c==bc)r.push_back(i); sort(r.begin(),r.end()); printf("%d\n",static_cast<int>(r.size())); for(ivi it=r.begin();it!=r.end();++it)if(it==r.begin())printf("%d",1+*it);else printf(" %d",1+*it); return!printf("\n"); } |