//O(n+m) #include <cstdio> #include <vector> using namespace std; const int MAXN=200005, MAXM=200005; int N, M, D; vector<int> edges[MAXN]; int edgesAlive[MAXN]={}; vector<int> deleteQueue; bool scheduledToDelete[MAXN]={}; bool deleted[MAXN]={}; void checkIfShouldDelete(int node) { if (scheduledToDelete[node]) return; if (edgesAlive[node] < D) { deleteQueue.push_back(node); scheduledToDelete[node] = true; } } void annihilate() { for (int i=0; i<(int)deleteQueue.size(); ++i) { int node = deleteQueue[i]; for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { --edgesAlive[*vit]; checkIfShouldDelete(*vit); } deleted[node] = true; } } int componentId[MAXN]={}; // int dfs(int node, int cid) { // if (deleted[node] || componentId[node]!=0) // return 0; // // int res = 1; // componentId[node] = cid; // // for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { // res += dfs(*vit, cid); // } // // return res; // } vector<int> bfsQueue; int bfs(int node, int cid) { if (deleted[node] || componentId[node]!=0) return 0; bfsQueue.clear(); componentId[node] = cid; bfsQueue.push_back(node); for (int i=0; i<(int)bfsQueue.size(); ++i) { int curNode = bfsQueue[i]; for (vector<int>::iterator vit=edges[curNode].begin(); vit!=edges[curNode].end(); ++vit) { if (deleted[*vit] || componentId[*vit]!=0) continue; componentId[*vit] = cid; bfsQueue.push_back(*vit); } } return bfsQueue.size(); } int main() { scanf("%d %d %d", &N, &M, &D); int a, b; for (int i=0; i<M; ++i) { scanf("%d %d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); } for (int i=1; i<=N; ++i) { edgesAlive[i] = edges[i].size(); checkIfShouldDelete(i); } annihilate(); //Finding the biggest component: int bestId=0, bestSize=0, tmpSize; for (int i=1; i<=N; ++i) { tmpSize = bfs(i, i); if (bestSize < tmpSize) { bestSize = tmpSize; bestId = i; } } //Printing answer: if (bestId==0) printf("NIE\n"); else { printf("%d\n", bestSize); for (int i=1; i<=N; ++i) { if (componentId[i] == bestId) printf("%d ", i); } printf("\n"); } 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 | //O(n+m) #include <cstdio> #include <vector> using namespace std; const int MAXN=200005, MAXM=200005; int N, M, D; vector<int> edges[MAXN]; int edgesAlive[MAXN]={}; vector<int> deleteQueue; bool scheduledToDelete[MAXN]={}; bool deleted[MAXN]={}; void checkIfShouldDelete(int node) { if (scheduledToDelete[node]) return; if (edgesAlive[node] < D) { deleteQueue.push_back(node); scheduledToDelete[node] = true; } } void annihilate() { for (int i=0; i<(int)deleteQueue.size(); ++i) { int node = deleteQueue[i]; for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { --edgesAlive[*vit]; checkIfShouldDelete(*vit); } deleted[node] = true; } } int componentId[MAXN]={}; // int dfs(int node, int cid) { // if (deleted[node] || componentId[node]!=0) // return 0; // // int res = 1; // componentId[node] = cid; // // for (vector<int>::iterator vit=edges[node].begin(); vit!=edges[node].end(); ++vit) { // res += dfs(*vit, cid); // } // // return res; // } vector<int> bfsQueue; int bfs(int node, int cid) { if (deleted[node] || componentId[node]!=0) return 0; bfsQueue.clear(); componentId[node] = cid; bfsQueue.push_back(node); for (int i=0; i<(int)bfsQueue.size(); ++i) { int curNode = bfsQueue[i]; for (vector<int>::iterator vit=edges[curNode].begin(); vit!=edges[curNode].end(); ++vit) { if (deleted[*vit] || componentId[*vit]!=0) continue; componentId[*vit] = cid; bfsQueue.push_back(*vit); } } return bfsQueue.size(); } int main() { scanf("%d %d %d", &N, &M, &D); int a, b; for (int i=0; i<M; ++i) { scanf("%d %d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); } for (int i=1; i<=N; ++i) { edgesAlive[i] = edges[i].size(); checkIfShouldDelete(i); } annihilate(); //Finding the biggest component: int bestId=0, bestSize=0, tmpSize; for (int i=1; i<=N; ++i) { tmpSize = bfs(i, i); if (bestSize < tmpSize) { bestSize = tmpSize; bestId = i; } } //Printing answer: if (bestId==0) printf("NIE\n"); else { printf("%d\n", bestSize); for (int i=1; i<=N; ++i) { if (componentId[i] == bestId) printf("%d ", i); } printf("\n"); } return 0; } |