#include <stdio.h> #include <vector> #include <set> using namespace std; struct node { vector <int> neighbours; int nS; bool inGraph; }; node nodes[234567]; struct nodecmp { bool operator () (const int& lhs, const int& rhs) const { if (nodes[lhs].nS == nodes[rhs].nS) return lhs < rhs; return nodes[lhs].nS < nodes[rhs].nS; } }; set <int, nodecmp> graph; int n, m, d; void load () { scanf ("%d %d %d", &n, &m, &d); int a, b; for (int i = 0; i < m; ++i) { scanf ("%d %d", &a, &b); nodes[a].neighbours.push_back (b); nodes[b].neighbours.push_back (a); } for (int i = 1; i <= n; ++i) { nodes[i].inGraph = true; nodes[i].nS = nodes[i].neighbours.size (); graph.insert (i); } } void sieve () { int weakest, sas; while (!graph.empty () && nodes[weakest = *(graph.begin ())].nS < d) { nodes[weakest].inGraph = false; graph.erase (graph.begin ()); for (int i = 0; i < nodes[weakest].neighbours.size (); ++i) { sas = nodes[weakest].neighbours[i]; if (!nodes[sas].inGraph) continue; graph.erase (graph.find (sas)); --nodes[sas].nS; graph.insert (sas); } } } vector <int> part; vector <vector <int> > parts; void findPart (int no) { if (!nodes[no].inGraph) return; nodes[no].inGraph = false; part.push_back (no); int sas; for (int i = 0; i < nodes[no].neighbours.size(); ++i) { sas = nodes[no].neighbours[i]; findPart (sas); } } void divide () { for (int i = 1; i <= n; ++i) if (nodes[i].inGraph) { part.clear (); findPart (i); parts.push_back (part); } } void solve () { if (d > 2000) { printf ("NIE"); return; } sieve (); divide (); int maxSize = 0; int partNum = -1; for (int i = 0; i < parts.size (); ++i) { if (maxSize < parts[i].size ()) { maxSize = parts[i].size (); partNum = i; } } if (partNum < 0) { printf ("NIE\n"); } else { printf ("%d\n", maxSize); for (int i = 0; i < parts[partNum].size (); ++i) printf ("%d ", parts[partNum][i]); printf ("\n"); } } int main () { load (); solve (); }
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 | #include <stdio.h> #include <vector> #include <set> using namespace std; struct node { vector <int> neighbours; int nS; bool inGraph; }; node nodes[234567]; struct nodecmp { bool operator () (const int& lhs, const int& rhs) const { if (nodes[lhs].nS == nodes[rhs].nS) return lhs < rhs; return nodes[lhs].nS < nodes[rhs].nS; } }; set <int, nodecmp> graph; int n, m, d; void load () { scanf ("%d %d %d", &n, &m, &d); int a, b; for (int i = 0; i < m; ++i) { scanf ("%d %d", &a, &b); nodes[a].neighbours.push_back (b); nodes[b].neighbours.push_back (a); } for (int i = 1; i <= n; ++i) { nodes[i].inGraph = true; nodes[i].nS = nodes[i].neighbours.size (); graph.insert (i); } } void sieve () { int weakest, sas; while (!graph.empty () && nodes[weakest = *(graph.begin ())].nS < d) { nodes[weakest].inGraph = false; graph.erase (graph.begin ()); for (int i = 0; i < nodes[weakest].neighbours.size (); ++i) { sas = nodes[weakest].neighbours[i]; if (!nodes[sas].inGraph) continue; graph.erase (graph.find (sas)); --nodes[sas].nS; graph.insert (sas); } } } vector <int> part; vector <vector <int> > parts; void findPart (int no) { if (!nodes[no].inGraph) return; nodes[no].inGraph = false; part.push_back (no); int sas; for (int i = 0; i < nodes[no].neighbours.size(); ++i) { sas = nodes[no].neighbours[i]; findPart (sas); } } void divide () { for (int i = 1; i <= n; ++i) if (nodes[i].inGraph) { part.clear (); findPart (i); parts.push_back (part); } } void solve () { if (d > 2000) { printf ("NIE"); return; } sieve (); divide (); int maxSize = 0; int partNum = -1; for (int i = 0; i < parts.size (); ++i) { if (maxSize < parts[i].size ()) { maxSize = parts[i].size (); partNum = i; } } if (partNum < 0) { printf ("NIE\n"); } else { printf ("%d\n", maxSize); for (int i = 0; i < parts[partNum].size (); ++i) printf ("%d ", parts[partNum][i]); printf ("\n"); } } int main () { load (); solve (); } |