#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<int> V[200000];
vector<int> S[200000];
bool odwiedzonyBFS[200000] = {0};
int n, m, d, w;
void BFS(int v)
{
queue<int> Q;
odwiedzonyBFS[v] = true;
Q.push(v);
while(!Q.empty())
{
v = Q.front();
Q.pop();
S[w].push_back(v);
for(int i = 0; i < V[v].size(); i++)
{
int u = V[v][i];
if(!odwiedzonyBFS[u])
{
odwiedzonyBFS[u] = true;
Q.push(u);
}
}
}
w++;
}
void f()
{
for(int i = 1; i < n; i++)
if(!odwiedzonyBFS[i] && !V[i].empty())
BFS(i);
}
void CleanGraph()
{
int s = 0;
vector<int> stos;
for(int i = 1; i <= n; i++)
{
if(V[i].size() < d)
{
V[i].clear();
stos.push_back(i);
odwiedzonyBFS[i] = true;
}
}
}
int main()
{
std::ios_base::sync_with_stdio(false);
cin >> n >> m >> d;
for(int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
V[a].push_back(b);
V[b].push_back(a);
}
CleanGraph();
f();
int maxc = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < S[i].size(); j++)
{
if(S[i].size() > maxc)
maxc = i;
}
}
if(S[maxc].size() <= 1)
cout << "NIE" << endl;
else
{
sort(S[maxc].begin(), S[maxc].end());
cout << S[maxc].size() << endl;
for(int i = 0; i < S[maxc].size(); i++)
cout << S[maxc][i] << " ";
}
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; vector<int> V[200000]; vector<int> S[200000]; bool odwiedzonyBFS[200000] = {0}; int n, m, d, w; void BFS(int v) { queue<int> Q; odwiedzonyBFS[v] = true; Q.push(v); while(!Q.empty()) { v = Q.front(); Q.pop(); S[w].push_back(v); for(int i = 0; i < V[v].size(); i++) { int u = V[v][i]; if(!odwiedzonyBFS[u]) { odwiedzonyBFS[u] = true; Q.push(u); } } } w++; } void f() { for(int i = 1; i < n; i++) if(!odwiedzonyBFS[i] && !V[i].empty()) BFS(i); } void CleanGraph() { int s = 0; vector<int> stos; for(int i = 1; i <= n; i++) { if(V[i].size() < d) { V[i].clear(); stos.push_back(i); odwiedzonyBFS[i] = true; } } } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m >> d; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; V[a].push_back(b); V[b].push_back(a); } CleanGraph(); f(); int maxc = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j < S[i].size(); j++) { if(S[i].size() > maxc) maxc = i; } } if(S[maxc].size() <= 1) cout << "NIE" << endl; else { sort(S[maxc].begin(), S[maxc].end()); cout << S[maxc].size() << endl; for(int i = 0; i < S[maxc].size(); i++) cout << S[maxc][i] << " "; } return 0; } |
English