#include <cstdio> #include <algorithm> #include <vector> #include <stack> using namespace std; const int maxn = 200000; int n, m, d; struct city { int conn_size_cache; int valid_conn; vector<int> conn; bool used; }; vector<city> cities; int visit_log[maxn+1]; int visit_log_size = 0; int visit_best = -1; int visit_len = 0; static inline void process_city(const int id) { stack<int> q; int log_start = visit_log_size; q.push(id); cities[id].used = true; while(!q.empty()) { const int p = q.top(); q.pop(); visit_log[visit_log_size++] = p; for (int i = 0; i < cities[p].conn_size_cache; i++) { const int r = cities[p].conn[i]; if (! (cities[r].used || cities[r].valid_conn < d)) { cities[r].used = true; q.push(r); } } } if (visit_log_size - log_start > visit_len) { visit_best = log_start; visit_len = visit_log_size - log_start; } } static inline void adjust_valid_conns() { stack<int> q; for (int i = 0; i < n; i++) { if (cities[i].valid_conn < d) { cities[i].used = true; q.push(i); } } while (!q.empty()) { const int c = q.top(); q.pop(); for (int i = 0; i < cities[c].conn_size_cache; i++) { const int r = cities[c].conn[i]; cities[r].valid_conn--; if (cities[r].valid_conn < d && !cities[r].used) { cities[r].used = true; q.push(r); } } } for (int i = 0; i < n; i++) cities[i].used = false; } int main() { struct city tmp; tmp.conn_size_cache = 0; scanf("%d %d %d", &n, &m, &d); cities.resize(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; cities[a].conn.push_back(b); cities[b].conn.push_back(a); } for (int i = 0; i < n; i++) { cities[i].conn_size_cache = cities[i].conn.size(); cities[i].valid_conn = cities[i].conn_size_cache; cities[i].used = false; } adjust_valid_conns(); for (int i = 0; i < n; i++) { if (cities[i].used) continue; if (cities[i].valid_conn < d) { cities[i].used = true; continue; } process_city(i); } if (visit_best == -1) printf("NIE\n"); else { sort(visit_log + visit_best, visit_log + visit_best + visit_len); printf("%d\n", visit_len); for (int i = 0; i < visit_len; i++) printf(i == (visit_len - 1) ? "%d\n" : "%d ", visit_log[visit_best + i] + 1); } 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 124 125 126 | #include <cstdio> #include <algorithm> #include <vector> #include <stack> using namespace std; const int maxn = 200000; int n, m, d; struct city { int conn_size_cache; int valid_conn; vector<int> conn; bool used; }; vector<city> cities; int visit_log[maxn+1]; int visit_log_size = 0; int visit_best = -1; int visit_len = 0; static inline void process_city(const int id) { stack<int> q; int log_start = visit_log_size; q.push(id); cities[id].used = true; while(!q.empty()) { const int p = q.top(); q.pop(); visit_log[visit_log_size++] = p; for (int i = 0; i < cities[p].conn_size_cache; i++) { const int r = cities[p].conn[i]; if (! (cities[r].used || cities[r].valid_conn < d)) { cities[r].used = true; q.push(r); } } } if (visit_log_size - log_start > visit_len) { visit_best = log_start; visit_len = visit_log_size - log_start; } } static inline void adjust_valid_conns() { stack<int> q; for (int i = 0; i < n; i++) { if (cities[i].valid_conn < d) { cities[i].used = true; q.push(i); } } while (!q.empty()) { const int c = q.top(); q.pop(); for (int i = 0; i < cities[c].conn_size_cache; i++) { const int r = cities[c].conn[i]; cities[r].valid_conn--; if (cities[r].valid_conn < d && !cities[r].used) { cities[r].used = true; q.push(r); } } } for (int i = 0; i < n; i++) cities[i].used = false; } int main() { struct city tmp; tmp.conn_size_cache = 0; scanf("%d %d %d", &n, &m, &d); cities.resize(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; cities[a].conn.push_back(b); cities[b].conn.push_back(a); } for (int i = 0; i < n; i++) { cities[i].conn_size_cache = cities[i].conn.size(); cities[i].valid_conn = cities[i].conn_size_cache; cities[i].used = false; } adjust_valid_conns(); for (int i = 0; i < n; i++) { if (cities[i].used) continue; if (cities[i].valid_conn < d) { cities[i].used = true; continue; } process_city(i); } if (visit_best == -1) printf("NIE\n"); else { sort(visit_log + visit_best, visit_log + visit_best + visit_len); printf("%d\n", visit_len); for (int i = 0; i < visit_len; i++) printf(i == (visit_len - 1) ? "%d\n" : "%d ", visit_log[visit_best + i] + 1); } return 0; } |