#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> odpnode;
struct node
{
vector<int>sas;
int rozm;
bool jest;
bool odw;
int odp;
};
vector<node>tree;
int dfs(int start)
{
odpnode.push_back(start);
tree[start].odp=1;
tree[start].odw=true;
for(int i=0;i<(int)tree[start].sas.size();i++)
{
if((tree[tree[start].sas[i]].odw==false)&&(tree[tree[start].sas[i]].jest==true))
{
//cout<<" "<<start<<" "<<tree[start].sas[i]<<" "<<tree[tree[start].sas[i]].jest<<"\n";
tree[start].odp+=dfs(tree[start].sas[i]);
}
}
return tree[start].odp;
}
void dfs2(int start,int d)
{
if((tree[start].rozm<d)&&(tree[start].jest==true))
{
tree[start].jest=false;
for(int i=0;i<tree[start].sas.size();i++)
{
tree[tree[start].sas[i]].rozm--;
dfs2(tree[start].sas[i],d);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
int n,m,d;
int maxval;
maxval=-1;
cin>>n>>m>>d;
node miasto;
miasto.sas.clear();
for(int i=0;i<n;i++)
{
tree.push_back(miasto);
}
int a,b;
for(int i=0;i<m;i++)
{
cin>>a>>b;
a--;
b--;
tree[a].sas.push_back(b);
tree[b].sas.push_back(a);
}
for(int i=0;i<n;i++)
{
tree[i].rozm=(int)tree[i].sas.size();
tree[i].jest=true;
tree[i].odw=false;
tree[i].odp;
}
for(int i=0;i<n;i++)
{
dfs2(i,d);
}
/*for(int i=0;i<n;i++)
{
cout<<tree[i].jest<<" "<<tree[i].rozm<<"\n";
}*/
int wyn;
vector<int> maxodpnode;
for(int i=0;i<n;i++)
{
if((tree[i].odw==false)&&(tree[i].jest==true))
{
wyn=dfs(i);
if(wyn>maxval)
{
maxodpnode.clear();
maxval=wyn;
maxodpnode=odpnode;
odpnode.clear();
}
}
}
if(maxval>0)
{
sort(maxodpnode.begin(),maxodpnode.end());
cout<<maxval<<"\n";
for(int i=0;i<(int)maxodpnode.size();i++)
{
cout<<maxodpnode[i]+1<<" ";
}
}
else
{
cout<<"NIE";
}
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 120 121 122 | #include<iostream> #include<set> #include<vector> #include<algorithm> using namespace std; vector<int> odpnode; struct node { vector<int>sas; int rozm; bool jest; bool odw; int odp; }; vector<node>tree; int dfs(int start) { odpnode.push_back(start); tree[start].odp=1; tree[start].odw=true; for(int i=0;i<(int)tree[start].sas.size();i++) { if((tree[tree[start].sas[i]].odw==false)&&(tree[tree[start].sas[i]].jest==true)) { //cout<<" "<<start<<" "<<tree[start].sas[i]<<" "<<tree[tree[start].sas[i]].jest<<"\n"; tree[start].odp+=dfs(tree[start].sas[i]); } } return tree[start].odp; } void dfs2(int start,int d) { if((tree[start].rozm<d)&&(tree[start].jest==true)) { tree[start].jest=false; for(int i=0;i<tree[start].sas.size();i++) { tree[tree[start].sas[i]].rozm--; dfs2(tree[start].sas[i],d); } } } int main() { ios_base::sync_with_stdio(0); int n,m,d; int maxval; maxval=-1; cin>>n>>m>>d; node miasto; miasto.sas.clear(); for(int i=0;i<n;i++) { tree.push_back(miasto); } int a,b; for(int i=0;i<m;i++) { cin>>a>>b; a--; b--; tree[a].sas.push_back(b); tree[b].sas.push_back(a); } for(int i=0;i<n;i++) { tree[i].rozm=(int)tree[i].sas.size(); tree[i].jest=true; tree[i].odw=false; tree[i].odp; } for(int i=0;i<n;i++) { dfs2(i,d); } /*for(int i=0;i<n;i++) { cout<<tree[i].jest<<" "<<tree[i].rozm<<"\n"; }*/ int wyn; vector<int> maxodpnode; for(int i=0;i<n;i++) { if((tree[i].odw==false)&&(tree[i].jest==true)) { wyn=dfs(i); if(wyn>maxval) { maxodpnode.clear(); maxval=wyn; maxodpnode=odpnode; odpnode.clear(); } } } if(maxval>0) { sort(maxodpnode.begin(),maxodpnode.end()); cout<<maxval<<"\n"; for(int i=0;i<(int)maxodpnode.size();i++) { cout<<maxodpnode[i]+1<<" "; } } else { cout<<"NIE"; } return 0; } |
English