#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> graph[250000]; int connections[250000]; int clusters[250000]; int cluster_sizes[250000]; int N, M, d; void init() { cin >> N >> M >> d; for (int i = 0; i <= N; i++) { connections[i] = clusters[i] = cluster_sizes[i] = 0; } for (int j = 0; j < M; j++) { int a, b; scanf("%d %d", &a, &b); graph[a].push_back(b); graph[b].push_back(a); } for (int i = 1; i <= N; i++) { connections[i] = graph[i].size(); } } void dfs(int node, int cluster_id) { clusters[node] = cluster_id; for (int j = 0; j < graph[node].size(); j++) { int neigh = graph[node][j]; if (clusters[neigh] == 0) { dfs(neigh, cluster_id); } } } void find_clusters() { int cluster_id = 1; for (int j = 1; j <= N; j++) { if (clusters[j] == 0) { dfs(j, cluster_id); cluster_id++; } } } void remove_from_clusters() { vector<int> to_remove; int vec_pos = 0; for (int j = 1; j <= N; j++) { if (connections[j] < d && clusters[j] > 0) { clusters[j] = -1; to_remove.push_back(j); while (vec_pos < to_remove.size()) { int el_to_process = to_remove[vec_pos++]; for (int k = 0; k < graph[el_to_process].size(); k++) { int neigh = graph[el_to_process][k]; connections[neigh]--; if (connections[neigh] < d && clusters[neigh] > 0) { clusters[neigh] = -1; to_remove.push_back(neigh); } } } } } } void find_max_cluster() { int v = 0; int best_id = 0; for (int j = 1; j <= N; j++) { if (clusters[j] > 0) { cluster_sizes[clusters[j]]++; } } for (int j = 1; j <= N; j++) { if (cluster_sizes[j] > v) { v = cluster_sizes[j]; best_id = j; } } if (v != 0) { int r = 0; cout << v << endl; for (int j = 1; j <= N; j++) { if (clusters[j] == best_id) { cout << j << " "; } } cout << endl; } else { cout << "NIE" << endl; } } void solve() { find_clusters(); remove_from_clusters(); for (int i = 1; i <= N; i++){ if (clusters[i] > 0) { clusters[i] = 0; } } find_clusters(); find_max_cluster(); } int main() { init(); solve(); 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 123 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> graph[250000]; int connections[250000]; int clusters[250000]; int cluster_sizes[250000]; int N, M, d; void init() { cin >> N >> M >> d; for (int i = 0; i <= N; i++) { connections[i] = clusters[i] = cluster_sizes[i] = 0; } for (int j = 0; j < M; j++) { int a, b; scanf("%d %d", &a, &b); graph[a].push_back(b); graph[b].push_back(a); } for (int i = 1; i <= N; i++) { connections[i] = graph[i].size(); } } void dfs(int node, int cluster_id) { clusters[node] = cluster_id; for (int j = 0; j < graph[node].size(); j++) { int neigh = graph[node][j]; if (clusters[neigh] == 0) { dfs(neigh, cluster_id); } } } void find_clusters() { int cluster_id = 1; for (int j = 1; j <= N; j++) { if (clusters[j] == 0) { dfs(j, cluster_id); cluster_id++; } } } void remove_from_clusters() { vector<int> to_remove; int vec_pos = 0; for (int j = 1; j <= N; j++) { if (connections[j] < d && clusters[j] > 0) { clusters[j] = -1; to_remove.push_back(j); while (vec_pos < to_remove.size()) { int el_to_process = to_remove[vec_pos++]; for (int k = 0; k < graph[el_to_process].size(); k++) { int neigh = graph[el_to_process][k]; connections[neigh]--; if (connections[neigh] < d && clusters[neigh] > 0) { clusters[neigh] = -1; to_remove.push_back(neigh); } } } } } } void find_max_cluster() { int v = 0; int best_id = 0; for (int j = 1; j <= N; j++) { if (clusters[j] > 0) { cluster_sizes[clusters[j]]++; } } for (int j = 1; j <= N; j++) { if (cluster_sizes[j] > v) { v = cluster_sizes[j]; best_id = j; } } if (v != 0) { int r = 0; cout << v << endl; for (int j = 1; j <= N; j++) { if (clusters[j] == best_id) { cout << j << " "; } } cout << endl; } else { cout << "NIE" << endl; } } void solve() { find_clusters(); remove_from_clusters(); for (int i = 1; i <= N; i++){ if (clusters[i] > 0) { clusters[i] = 0; } } find_clusters(); find_max_cluster(); } int main() { init(); solve(); return 0; } |