#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,d;
int ** graph;
vector <bool> visited;
vector <bool> visited2;
vector <int> c;
vector <int> size;
int l=0;
void DFS(int v)
{
visited[v]=1;
c[v]=l;
for(int i=0; i<n; i++)
if(!visited[i] && graph[v][i]) DFS(i);
}
void DFS2(int v)
{
visited2[v]=1;
for(int i=0; i<n; i++)
if(graph[v][i] == 1)
{
graph[v][i]=0;
graph[i][v]=0;
size[i]--;
if(size[i] < d && !visited2[i]) DFS2(i);
}
}
int main()
{
cin >> n >> m >> d;
graph = new int * [n];
for(int i=0; i<n; i++)
{
size.push_back(0);
visited.push_back(0);
visited2.push_back(0);
c.push_back(0);
graph[i] = new int [n];
for(int j=0; j<n; j++) graph[i][j]=0;
}
for(int i=0; i<m; i++)
{
int a, b;
cin >> a >> b;
graph[a-1][b-1]=1;
graph[b-1][a-1]=1;
size[a-1]++;
size[b-1]++;
}
for(int i=0; i<n; i++)
if(size[i] < d && !visited2[i]) DFS2(i);
for(int i=0; i<n; i++)
{
if(c[i]==0)
{
l++;
DFS(i);
}
}
int spojne[l+1];
for(int i=1; i<=l; i++) spojne[i]=0;
for(int i=1; i<=l; i++)
for(int j=0; j<n; j++) if(c[j] == i) spojne[i]++;
int maxx=-1, best;
for(int i=1; i<=l; i++)
if(spojne[i]>1 && spojne[i]>maxx)
{
maxx = spojne[i];
best = i;
}
if(maxx == -1) cout << "NIE";
else
{
vector<int> wynik;
for(int i=0; i<n; i++) if(c[i]==best) wynik.push_back(i);
sort(wynik.begin(), wynik.end());
cout << maxx << "\n";
for(int i=0; i<wynik.size(); i++) cout << wynik[i]+1 << " ";
}
}
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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int n,m,d; int ** graph; vector <bool> visited; vector <bool> visited2; vector <int> c; vector <int> size; int l=0; void DFS(int v) { visited[v]=1; c[v]=l; for(int i=0; i<n; i++) if(!visited[i] && graph[v][i]) DFS(i); } void DFS2(int v) { visited2[v]=1; for(int i=0; i<n; i++) if(graph[v][i] == 1) { graph[v][i]=0; graph[i][v]=0; size[i]--; if(size[i] < d && !visited2[i]) DFS2(i); } } int main() { cin >> n >> m >> d; graph = new int * [n]; for(int i=0; i<n; i++) { size.push_back(0); visited.push_back(0); visited2.push_back(0); c.push_back(0); graph[i] = new int [n]; for(int j=0; j<n; j++) graph[i][j]=0; } for(int i=0; i<m; i++) { int a, b; cin >> a >> b; graph[a-1][b-1]=1; graph[b-1][a-1]=1; size[a-1]++; size[b-1]++; } for(int i=0; i<n; i++) if(size[i] < d && !visited2[i]) DFS2(i); for(int i=0; i<n; i++) { if(c[i]==0) { l++; DFS(i); } } int spojne[l+1]; for(int i=1; i<=l; i++) spojne[i]=0; for(int i=1; i<=l; i++) for(int j=0; j<n; j++) if(c[j] == i) spojne[i]++; int maxx=-1, best; for(int i=1; i<=l; i++) if(spojne[i]>1 && spojne[i]>maxx) { maxx = spojne[i]; best = i; } if(maxx == -1) cout << "NIE"; else { vector<int> wynik; for(int i=0; i<n; i++) if(c[i]==best) wynik.push_back(i); sort(wynik.begin(), wynik.end()); cout << maxx << "\n"; for(int i=0; i<wynik.size(); i++) cout << wynik[i]+1 << " "; } } |
English