#include <stdio.h> #include <vector> #include <queue> #include <algorithm> void launchBfs(int start, int limit, std::vector<std::vector<int> > & graph, std::vector<int> & neighbours, std::vector<int> & removed){ std::queue<int> queue; queue.push(start); int next; while (!queue.empty()){ next = queue.front(); queue.pop(); if (removed[next] == 1) continue;//wierzcholki usuniete byly juz odwiedzone if (neighbours[next] < limit) removed[next] = 1;//jesli wlasnie usuwamy jakis wierzcholek to przetwarzomy jego sasiadow dalej wpp. ignorujemy go else continue; for (int i = 0; i < (int)graph[next].size(); i++) {//wszystkim sadsiadom obnizamy liczbe kontaktow queue.push(graph[next][i]); neighbours[graph[next][i]]--; } } } void launchBfs(int start, std::vector<int> & acc, std::vector<int> & removed, std::vector<int> & visited, std::vector<std::vector<int> > & graph){ std::queue<int> queue; queue.push(start); int next; while (!queue.empty()){ next = queue.front(); queue.pop(); if (visited[next] == 1 || removed[next] == 1) continue; visited[next] = 1; acc.push_back(next); for (int i = 0; i < (int)graph[next].size(); i++) {//wszystkim sadsiadom obnizamy liczbe kontaktow queue.push(graph[next][i]); } } } int main(){ int n, m, d; int p, k; scanf("%d %d %d", &n,&m,&d); std::vector<std::vector<int> > graph(n + 1, std::vector<int>()); std::vector<int> neighbours(n + 1,0); std::vector<int> removed(n + 1, 0); for (int i = 0; i < m; i++){ scanf("%d %d", &p,&k); neighbours[p]++; neighbours[k]++; graph[p].push_back(k); graph[k].push_back(p); } std::vector<int> toCheck; for (int i = 1; i <= n; i++) if (neighbours[i] < d) { toCheck.push_back(i); //printf("--- %d\n",i); //removed[i] = 1; } for (int i = 0; i < (int)toCheck.size(); i++){ launchBfs(toCheck[i], d, graph, neighbours, removed); } toCheck.clear(); for (int i = 1; i <= n; i++) if (removed[i] == 0) { //printf("++++ %d\n",i); toCheck.push_back(i); } std::vector<std::vector<int> > results; std::vector<int> visited(n+1, 0); for (int i = 0; i < (int)toCheck.size(); i++){ results.push_back(std::vector<int>()); launchBfs(toCheck[i], results.back(), removed, visited, graph); } int max = -1; int maxVal = 0; for (int i = 0; i < (int)results.size(); i++){ if ((int)results[i].size() > maxVal){ max = i; maxVal = results[i].size(); } /* printf("%d ", results[i].size()); for (int j = 0; j < (int)results[i].size(); j++){ printf("%d ", results[i][j]); } printf("\n");*/ } if (max == -1){ printf("NIE\n"); } else { printf("%d\n", (int)results[max].size()); std::sort(results[max].begin(), results[max].end()); for (int j = 0; j < (int)results[max].size(); j++){ printf("%d ", results[max][j]); } 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 | #include <stdio.h> #include <vector> #include <queue> #include <algorithm> void launchBfs(int start, int limit, std::vector<std::vector<int> > & graph, std::vector<int> & neighbours, std::vector<int> & removed){ std::queue<int> queue; queue.push(start); int next; while (!queue.empty()){ next = queue.front(); queue.pop(); if (removed[next] == 1) continue;//wierzcholki usuniete byly juz odwiedzone if (neighbours[next] < limit) removed[next] = 1;//jesli wlasnie usuwamy jakis wierzcholek to przetwarzomy jego sasiadow dalej wpp. ignorujemy go else continue; for (int i = 0; i < (int)graph[next].size(); i++) {//wszystkim sadsiadom obnizamy liczbe kontaktow queue.push(graph[next][i]); neighbours[graph[next][i]]--; } } } void launchBfs(int start, std::vector<int> & acc, std::vector<int> & removed, std::vector<int> & visited, std::vector<std::vector<int> > & graph){ std::queue<int> queue; queue.push(start); int next; while (!queue.empty()){ next = queue.front(); queue.pop(); if (visited[next] == 1 || removed[next] == 1) continue; visited[next] = 1; acc.push_back(next); for (int i = 0; i < (int)graph[next].size(); i++) {//wszystkim sadsiadom obnizamy liczbe kontaktow queue.push(graph[next][i]); } } } int main(){ int n, m, d; int p, k; scanf("%d %d %d", &n,&m,&d); std::vector<std::vector<int> > graph(n + 1, std::vector<int>()); std::vector<int> neighbours(n + 1,0); std::vector<int> removed(n + 1, 0); for (int i = 0; i < m; i++){ scanf("%d %d", &p,&k); neighbours[p]++; neighbours[k]++; graph[p].push_back(k); graph[k].push_back(p); } std::vector<int> toCheck; for (int i = 1; i <= n; i++) if (neighbours[i] < d) { toCheck.push_back(i); //printf("--- %d\n",i); //removed[i] = 1; } for (int i = 0; i < (int)toCheck.size(); i++){ launchBfs(toCheck[i], d, graph, neighbours, removed); } toCheck.clear(); for (int i = 1; i <= n; i++) if (removed[i] == 0) { //printf("++++ %d\n",i); toCheck.push_back(i); } std::vector<std::vector<int> > results; std::vector<int> visited(n+1, 0); for (int i = 0; i < (int)toCheck.size(); i++){ results.push_back(std::vector<int>()); launchBfs(toCheck[i], results.back(), removed, visited, graph); } int max = -1; int maxVal = 0; for (int i = 0; i < (int)results.size(); i++){ if ((int)results[i].size() > maxVal){ max = i; maxVal = results[i].size(); } /* printf("%d ", results[i].size()); for (int j = 0; j < (int)results[i].size(); j++){ printf("%d ", results[i][j]); } printf("\n");*/ } if (max == -1){ printf("NIE\n"); } else { printf("%d\n", (int)results[max].size()); std::sort(results[max].begin(), results[max].end()); for (int j = 0; j < (int)results[max].size(); j++){ printf("%d ", results[max][j]); } printf("\n"); } return 0; } |