#include<bits/stdc++.h>
using namespace std;
int n, m, d;
bool czy[200001];
int sasiedzi[200001];
int vtd[200001];
int w1, w2;
vector<int>V[200001];
int N;
queue<int>Q;
//DFS gdyby by? graf roz??czny!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
void DFS(int x, int &ile, int nr){
vtd[x] = nr;
++ile;
for(int i = 0; i < V[x].size(); i++){
if(vtd[V[x][i]] == 0 && !czy[V[x][i]]){
DFS(V[x][i], ile, nr);
}
}
}
int main(){
scanf("%d%d%d", &n, &m, &d);
N = n;
for(int i = 0; i < m; i++){
int a, b;
scanf("%d%d", &a, &b);
V[a].push_back(b);
V[b].push_back(a);
++sasiedzi[a];
++sasiedzi[b];
}
for(int i = 1; i <= n; i++){
if(sasiedzi[i] < d){
Q.push(i);
czy[i] = 1;
}
}
while(!Q.empty()){
--N;
//cout << N << endl;
if(N <= d){
break;
}
int x = Q.front();
//czy[x] = 1;
Q.pop();
for(int i = 0; i < V[x].size(); i++){
sasiedzi[V[x][i]]--;
if(!czy[V[x][i]] && sasiedzi[V[x][i]] < d){
czy[V[x][i]] = 1;
Q.push(V[x][i]);
}
}
}
if(N <= d){
printf("NIE");
}
else{
//printf("%d\n", N);
for(int i = 1; i <= n; i++){
if(!czy[i] && vtd[i] == 0){
int ile = 0;
DFS(i, ile, i);
if(ile > w1){
w2 = i;
w1 = ile;
}
}
}
printf("%d\n", w1);
for(int i = w2; i <= n; i++){
if(vtd[i] == w2){
printf("%d ", 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 | #include<bits/stdc++.h> using namespace std; int n, m, d; bool czy[200001]; int sasiedzi[200001]; int vtd[200001]; int w1, w2; vector<int>V[200001]; int N; queue<int>Q; //DFS gdyby by? graf roz??czny!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11 void DFS(int x, int &ile, int nr){ vtd[x] = nr; ++ile; for(int i = 0; i < V[x].size(); i++){ if(vtd[V[x][i]] == 0 && !czy[V[x][i]]){ DFS(V[x][i], ile, nr); } } } int main(){ scanf("%d%d%d", &n, &m, &d); N = n; for(int i = 0; i < m; i++){ int a, b; scanf("%d%d", &a, &b); V[a].push_back(b); V[b].push_back(a); ++sasiedzi[a]; ++sasiedzi[b]; } for(int i = 1; i <= n; i++){ if(sasiedzi[i] < d){ Q.push(i); czy[i] = 1; } } while(!Q.empty()){ --N; //cout << N << endl; if(N <= d){ break; } int x = Q.front(); //czy[x] = 1; Q.pop(); for(int i = 0; i < V[x].size(); i++){ sasiedzi[V[x][i]]--; if(!czy[V[x][i]] && sasiedzi[V[x][i]] < d){ czy[V[x][i]] = 1; Q.push(V[x][i]); } } } if(N <= d){ printf("NIE"); } else{ //printf("%d\n", N); for(int i = 1; i <= n; i++){ if(!czy[i] && vtd[i] == 0){ int ile = 0; DFS(i, ile, i); if(ile > w1){ w2 = i; w1 = ile; } } } printf("%d\n", w1); for(int i = w2; i <= n; i++){ if(vtd[i] == w2){ printf("%d ", i); } } } return 0; } |
English