#include <stdio.h> #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; const int MAX_N = 200000; vector<int> edges[MAX_N + 1]; int currentDeg[MAX_N + 1]; bool deletedVer[MAX_N + 1]; bool inToDelQueue[MAX_N + 1]; vector<vector<int> > communicatedSets; queue<int> toDelVer; void dfs(int v) { //zaznaczam jako usunięte zamiast odwiedzone deletedVer[v] = true; communicatedSets[communicatedSets.size() - 1].push_back(v); for (int i = 0; i < (int)edges[v].size(); i++) { if (!deletedVer[edges[v][i]]) dfs(edges[v][i]); } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); currentDeg[a]++; currentDeg[b]++; } for (int v = 1; v <= n; v++) if (currentDeg[v] < d) { toDelVer.push(v); inToDelQueue[v] = true; } while (!toDelVer.empty()) { int v = toDelVer.front(); toDelVer.pop(); deletedVer[v] = true; for (int i = 0; i < (int)edges[v].size(); i++) { int w = edges[v][i]; if (!deletedVer[w]) { currentDeg[w]--; if (currentDeg[w] < d && !inToDelQueue[w]) { toDelVer.push(w); inToDelQueue[w] = true; } } } } //aktualizacja grafu for (int v = 1; v <= n; v++) { if (!deletedVer[v]) { //do usunietych wierzcholkow i tak nie zajrzymy int i = 0; while (i < (int)edges[v].size()) { //usuwanie krawedzi if (deletedVer[edges[v][i]]) { edges[v][i] = edges[v][edges[v].size() - 1]; edges[v].pop_back(); } else { i++; } } } } for (int v = 1; v <= n; v++) { if (!deletedVer[v]) { vector<int> vec; communicatedSets.push_back(vec); dfs(v); } } int id = -1; int size = 0; for (int i = 0; i < (int)communicatedSets.size(); i++) { if ((int)communicatedSets[i].size() > size) { id = i; size = communicatedSets[i].size(); } } if (id == -1) { printf("NIE\n"); return 0; } sort(communicatedSets[id].begin(), communicatedSets[id].end()); printf("%d\n", size); for (int i = 0; i < (int)communicatedSets[id].size(); i++) { printf("%d ", communicatedSets[id][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 | #include <stdio.h> #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; const int MAX_N = 200000; vector<int> edges[MAX_N + 1]; int currentDeg[MAX_N + 1]; bool deletedVer[MAX_N + 1]; bool inToDelQueue[MAX_N + 1]; vector<vector<int> > communicatedSets; queue<int> toDelVer; void dfs(int v) { //zaznaczam jako usunięte zamiast odwiedzone deletedVer[v] = true; communicatedSets[communicatedSets.size() - 1].push_back(v); for (int i = 0; i < (int)edges[v].size(); i++) { if (!deletedVer[edges[v][i]]) dfs(edges[v][i]); } } int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); currentDeg[a]++; currentDeg[b]++; } for (int v = 1; v <= n; v++) if (currentDeg[v] < d) { toDelVer.push(v); inToDelQueue[v] = true; } while (!toDelVer.empty()) { int v = toDelVer.front(); toDelVer.pop(); deletedVer[v] = true; for (int i = 0; i < (int)edges[v].size(); i++) { int w = edges[v][i]; if (!deletedVer[w]) { currentDeg[w]--; if (currentDeg[w] < d && !inToDelQueue[w]) { toDelVer.push(w); inToDelQueue[w] = true; } } } } //aktualizacja grafu for (int v = 1; v <= n; v++) { if (!deletedVer[v]) { //do usunietych wierzcholkow i tak nie zajrzymy int i = 0; while (i < (int)edges[v].size()) { //usuwanie krawedzi if (deletedVer[edges[v][i]]) { edges[v][i] = edges[v][edges[v].size() - 1]; edges[v].pop_back(); } else { i++; } } } } for (int v = 1; v <= n; v++) { if (!deletedVer[v]) { vector<int> vec; communicatedSets.push_back(vec); dfs(v); } } int id = -1; int size = 0; for (int i = 0; i < (int)communicatedSets.size(); i++) { if ((int)communicatedSets[i].size() > size) { id = i; size = communicatedSets[i].size(); } } if (id == -1) { printf("NIE\n"); return 0; } sort(communicatedSets[id].begin(), communicatedSets[id].end()); printf("%d\n", size); for (int i = 0; i < (int)communicatedSets[id].size(); i++) { printf("%d ", communicatedSets[id][i]); } printf("\n"); return 0; } |