#include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; int n, m, d, a, b; struct Miasto{ vector<int> drogi; int c; int k; int nr; }; Miasto miasta[200013]; int stack[200013]; int numerki[200013]; int si, sc, nr; int main() { scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); miasta[a].drogi.push_back(b); miasta[a].c++; miasta[b].drogi.push_back(a); miasta[b].c++; } // for (int i = 1; i <= n; ++i) { if (miasta[i].c < d) { stack[sc++] = i; } } // while (si < sc) { int mw = stack[si++]; for (int j = 0; j < miasta[mw].drogi.size(); ++j) { int md = miasta[mw].drogi[j]; if (miasta[md].c == d) { stack[sc++] = md; } --miasta[md].c; } } // si = 0; sc = 0; nr = 0; for (int i = 1; i <= n; ++i) { if ((miasta[i].c >= d) && (miasta[i].k ==0)) { ++nr; si = 0; sc = 0; stack[sc++] = i; while (si < sc) { int md = stack[si++]; miasta[md].k = 2; miasta[md].nr = nr; for (int j = 0; j < miasta[md].drogi.size(); ++j) { int mk = miasta[md].drogi[j]; if ((miasta[mk].k == 0) && (miasta[mk].c >= d)) { stack[sc++] = mk; miasta[mk].k = 1; } } } } miasta[i].k = 2; } // for (int i = 1; i <= n; ++i) { numerki[miasta[i].nr]++; } // int maxnr = -1; int maxval = 0; for (int i = 1; i <= n; ++i) { if (numerki[i] > maxval) { maxval = numerki[i]; maxnr = i; } } // if (maxnr != -1) { printf("%d\n", maxval); for (int i = 1; i <= n; ++i) { if (miasta[i].nr == maxnr) { printf("%d ", i); } } } else { printf("NIE"); } 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 127 | #include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; int n, m, d, a, b; struct Miasto{ vector<int> drogi; int c; int k; int nr; }; Miasto miasta[200013]; int stack[200013]; int numerki[200013]; int si, sc, nr; int main() { scanf("%d %d %d", &n, &m, &d); for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); miasta[a].drogi.push_back(b); miasta[a].c++; miasta[b].drogi.push_back(a); miasta[b].c++; } // for (int i = 1; i <= n; ++i) { if (miasta[i].c < d) { stack[sc++] = i; } } // while (si < sc) { int mw = stack[si++]; for (int j = 0; j < miasta[mw].drogi.size(); ++j) { int md = miasta[mw].drogi[j]; if (miasta[md].c == d) { stack[sc++] = md; } --miasta[md].c; } } // si = 0; sc = 0; nr = 0; for (int i = 1; i <= n; ++i) { if ((miasta[i].c >= d) && (miasta[i].k ==0)) { ++nr; si = 0; sc = 0; stack[sc++] = i; while (si < sc) { int md = stack[si++]; miasta[md].k = 2; miasta[md].nr = nr; for (int j = 0; j < miasta[md].drogi.size(); ++j) { int mk = miasta[md].drogi[j]; if ((miasta[mk].k == 0) && (miasta[mk].c >= d)) { stack[sc++] = mk; miasta[mk].k = 1; } } } } miasta[i].k = 2; } // for (int i = 1; i <= n; ++i) { numerki[miasta[i].nr]++; } // int maxnr = -1; int maxval = 0; for (int i = 1; i <= n; ++i) { if (numerki[i] > maxval) { maxval = numerki[i]; maxnr = i; } } // if (maxnr != -1) { printf("%d\n", maxval); for (int i = 1; i <= n; ++i) { if (miasta[i].nr == maxnr) { printf("%d ", i); } } } else { printf("NIE"); } return 0; } |