#include <iostream> #include <vector> #include <stack> #include <set> #include <queue> #define N_MAX 200002 using namespace std; struct vertex : public set<int> { bool visited; }; class graph_t { public: graph_t() : V(N_MAX) {} unsigned int numVertices, numEdges, minDegree; vector<vertex> V; stack<int> tooSmall; void addEdge(int a, int b) { V[a].insert(b); V[b].insert(a); } void removeEdge(int a, int b) { V[a].erase(b); V[b].erase(a); --numEdges; } void removeVertex(int a) { for (vertex::iterator it = V[a].begin(); it != V[a].end(); ++it) { removeEdge(a, *it); if(V[*it].size() < minDegree) tooSmall.push(*it); } } void finalizeAdding() { for (unsigned int i = 0; i < numVertices; ++i) { if ( V[i].size() < minDegree) tooSmall.push(i); } } void removeTooSmall() { int v; while (!tooSmall.empty()) { v = tooSmall.top(); tooSmall.pop(); removeVertex(v); } } void findBiggestIsland() { for (unsigned int i = 0; i < numVertices; ++i) { if (! V[i].visited) { BFS(i); if (numElements > best_numElements) { best_numElements = numElements; best_elements = elements; numElements = 0; elements = myPQ (); } } } } void print() { cout << best_numElements << '\n'; while (!best_elements.empty()) { cout << best_elements.top() + 1 << ' '; best_elements.pop(); } } private: typedef priority_queue<int, vector<int>, greater<int> > myPQ; void BFS(int v) { int a; queue<int> q; q.push(v); while (!q.empty()) { a = q.front(); q.pop(); if (V[a].visited) continue; // cout << a << endl; V[a].visited = true; ++numElements; elements.push(a); for (vertex::iterator it = V[a].begin(); it != V[a].end(); ++it) { if (! V[*it].visited) q.push(*it); } } } myPQ elements, best_elements; int numElements, best_numElements; }; int m, d, a, b; graph_t graph; int main() { ios_base::sync_with_stdio(false); cin >> graph.numVertices >> graph.numEdges >> graph.minDegree; for (unsigned int i = 0; i < graph.numEdges; ++i) { cin >> a >> b; graph.addEdge(a-1,b-1); } graph.finalizeAdding(); graph.removeTooSmall(); if (graph.numEdges == 0) { cout << "NIE\n"; return 0; } graph.findBiggestIsland(); // cout << "====================\n"; graph.print(); 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include <iostream> #include <vector> #include <stack> #include <set> #include <queue> #define N_MAX 200002 using namespace std; struct vertex : public set<int> { bool visited; }; class graph_t { public: graph_t() : V(N_MAX) {} unsigned int numVertices, numEdges, minDegree; vector<vertex> V; stack<int> tooSmall; void addEdge(int a, int b) { V[a].insert(b); V[b].insert(a); } void removeEdge(int a, int b) { V[a].erase(b); V[b].erase(a); --numEdges; } void removeVertex(int a) { for (vertex::iterator it = V[a].begin(); it != V[a].end(); ++it) { removeEdge(a, *it); if(V[*it].size() < minDegree) tooSmall.push(*it); } } void finalizeAdding() { for (unsigned int i = 0; i < numVertices; ++i) { if ( V[i].size() < minDegree) tooSmall.push(i); } } void removeTooSmall() { int v; while (!tooSmall.empty()) { v = tooSmall.top(); tooSmall.pop(); removeVertex(v); } } void findBiggestIsland() { for (unsigned int i = 0; i < numVertices; ++i) { if (! V[i].visited) { BFS(i); if (numElements > best_numElements) { best_numElements = numElements; best_elements = elements; numElements = 0; elements = myPQ (); } } } } void print() { cout << best_numElements << '\n'; while (!best_elements.empty()) { cout << best_elements.top() + 1 << ' '; best_elements.pop(); } } private: typedef priority_queue<int, vector<int>, greater<int> > myPQ; void BFS(int v) { int a; queue<int> q; q.push(v); while (!q.empty()) { a = q.front(); q.pop(); if (V[a].visited) continue; // cout << a << endl; V[a].visited = true; ++numElements; elements.push(a); for (vertex::iterator it = V[a].begin(); it != V[a].end(); ++it) { if (! V[*it].visited) q.push(*it); } } } myPQ elements, best_elements; int numElements, best_numElements; }; int m, d, a, b; graph_t graph; int main() { ios_base::sync_with_stdio(false); cin >> graph.numVertices >> graph.numEdges >> graph.minDegree; for (unsigned int i = 0; i < graph.numEdges; ++i) { cin >> a >> b; graph.addEdge(a-1,b-1); } graph.finalizeAdding(); graph.removeTooSmall(); if (graph.numEdges == 0) { cout << "NIE\n"; return 0; } graph.findBiggestIsland(); // cout << "====================\n"; graph.print(); return 0; } |