#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <vector> #include <utility> #include <cstdlib> #include <iostream> #include <algorithm> #define ST first #define ND second #define MP make_pair #define PB push_back #define QI queue <int> #define VI vector <int> #define PII pair <int,int> #define SZ(x) (int)(x).size() #define ALL(x) x.begin(),x.end() #define RALL(x) x.rbegin(),x.rend() #define SYNC ios_base::sync_with_stdio(0) #define MAX(a,b) ((a) > (b)) ? a : b using namespace std; typedef long long LL; typedef long double LD; typedef unsigned int UI; typedef unsigned long long ULL; const LL INF = 1e18 + 7; const int BASE = 1 << 19; int a, b, c, d, e, f, n, m, q, res; bool Bad[300000]; bool Vis[300000]; bool Liczba[300000]; int Drogi[300000]; queue <int> Q; vector <int> Graph[300000]; vector <int> Res[300000]; void DFS(int,int); int main() { scanf("%d %d %d", &n, &m, &d); for(int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); Graph[a].PB(b); Graph[b].PB(a); Drogi[a]++; Drogi[b]++; } for(int i = 1; i <= n; i++) { if(Drogi[i] < d) { Q.push(i); Bad[i] = true; } } while(!Q.empty()) { int v = Q.front(); Q.pop(); for(int i = 0; i < SZ(Graph[v]); i++) { int u = Graph[v][i]; Drogi[u]--; if(Drogi[u] < d && !Bad[u]) { Bad[u] = true; Q.push(u); } } } for(int i = 1; i <= n; i++) { if(!Vis[i] && !Bad[i]) { DFS(i,i); if(res == 0) { res = i; } else if(SZ(Res[res]) < SZ(Res[i])) { res = i; } } } if(res == 0) { puts("NIE"); } else { printf("%d\n", SZ(Res[res])); for(int i = 0; i < SZ(Res[res]); i++) { Liczba[Res[res][i]] = true; } for(int i = 1; i <= n; i++) { if(Liczba[i]) { printf("%d ", i); } } printf("\n"); } } void DFS(int v, int start) { Vis[v] = true; Res[start].PB(v); for(int i = 0; i < SZ(Graph[v]); i++) { int u = Graph[v][i]; if(!Vis[u] && !Bad[u]) { DFS(u, start); } } }
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 123 124 125 126 127 128 129 130 131 132 133 | #include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <vector> #include <utility> #include <cstdlib> #include <iostream> #include <algorithm> #define ST first #define ND second #define MP make_pair #define PB push_back #define QI queue <int> #define VI vector <int> #define PII pair <int,int> #define SZ(x) (int)(x).size() #define ALL(x) x.begin(),x.end() #define RALL(x) x.rbegin(),x.rend() #define SYNC ios_base::sync_with_stdio(0) #define MAX(a,b) ((a) > (b)) ? a : b using namespace std; typedef long long LL; typedef long double LD; typedef unsigned int UI; typedef unsigned long long ULL; const LL INF = 1e18 + 7; const int BASE = 1 << 19; int a, b, c, d, e, f, n, m, q, res; bool Bad[300000]; bool Vis[300000]; bool Liczba[300000]; int Drogi[300000]; queue <int> Q; vector <int> Graph[300000]; vector <int> Res[300000]; void DFS(int,int); int main() { scanf("%d %d %d", &n, &m, &d); for(int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); Graph[a].PB(b); Graph[b].PB(a); Drogi[a]++; Drogi[b]++; } for(int i = 1; i <= n; i++) { if(Drogi[i] < d) { Q.push(i); Bad[i] = true; } } while(!Q.empty()) { int v = Q.front(); Q.pop(); for(int i = 0; i < SZ(Graph[v]); i++) { int u = Graph[v][i]; Drogi[u]--; if(Drogi[u] < d && !Bad[u]) { Bad[u] = true; Q.push(u); } } } for(int i = 1; i <= n; i++) { if(!Vis[i] && !Bad[i]) { DFS(i,i); if(res == 0) { res = i; } else if(SZ(Res[res]) < SZ(Res[i])) { res = i; } } } if(res == 0) { puts("NIE"); } else { printf("%d\n", SZ(Res[res])); for(int i = 0; i < SZ(Res[res]); i++) { Liczba[Res[res][i]] = true; } for(int i = 1; i <= n; i++) { if(Liczba[i]) { printf("%d ", i); } } printf("\n"); } } void DFS(int v, int start) { Vis[v] = true; Res[start].PB(v); for(int i = 0; i < SZ(Graph[v]); i++) { int u = Graph[v][i]; if(!Vis[u] && !Bad[u]) { DFS(u, start); } } } |