#include <iostream> #include <stdio.h> #include <queue> #include <stack> #include <algorithm> using namespace std; vector<int> g[200000]; bool usunTo[200000]; bool odw[200000]; int gS[200000]; int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < n; i++) gS[i] = 0; for (int i = 0; i < n; i++) usunTo[i] = true; for (int i = 0; i < n; i++) odw[i] = true; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; g[a].push_back(b); gS[a]++; g[b].push_back(a); gS[b]++; } priority_queue< pair<int, int> > pq; for (int i = 0; i < n; i++) { pq.push(make_pair(gS[i], i)); } while (!pq.empty()) { pair<int, int> cur = pq.top(); pq.pop(); cur.first = gS[cur.second]; //cout << "USUNAC TO??? " << cur.first << " " << cur.second + 1 << endl; if (cur.first < d && usunTo[cur.second]) { //cout << "USUN TO!!!!!!!!: " << cur.second + 1 << endl; usunTo[cur.second] = false; for (int i = 0; i < g[cur.second].size(); i++) { if (usunTo[g[cur.second][i]]) { gS[g[cur.second][i]]--; if (gS[g[cur.second][i]] < d) { pq.push(make_pair(gS[g[cur.second][i]], g[cur.second][i])); } } } } } vector<int> res; for (int i = 0; i < n; i++) { if (odw[i] && usunTo[i]) { vector<int> curRes; stack<int> dfs; odw[i] = false; dfs.push(i); while (!dfs.empty()) { int aktW = dfs.top(); dfs.pop(); curRes.push_back(aktW); for (int j = 0; j < g[aktW].size(); j++) { if (odw[g[aktW][j]] && usunTo[g[aktW][j]]) { odw[g[aktW][j]] = false; dfs.push(g[aktW][j]); } } } if (curRes.size() > res.size()) res = curRes; } } if (res.empty()) puts("NIE"); else { printf("%d\n", res.size()); sort(res.begin(), res.end()); for (int i = 0; i < res.size(); i++) printf("%d ", res[i] + 1); 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 | #include <iostream> #include <stdio.h> #include <queue> #include <stack> #include <algorithm> using namespace std; vector<int> g[200000]; bool usunTo[200000]; bool odw[200000]; int gS[200000]; int main() { int n, m, d; scanf("%d%d%d", &n, &m, &d); for (int i = 0; i < n; i++) gS[i] = 0; for (int i = 0; i < n; i++) usunTo[i] = true; for (int i = 0; i < n; i++) odw[i] = true; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; g[a].push_back(b); gS[a]++; g[b].push_back(a); gS[b]++; } priority_queue< pair<int, int> > pq; for (int i = 0; i < n; i++) { pq.push(make_pair(gS[i], i)); } while (!pq.empty()) { pair<int, int> cur = pq.top(); pq.pop(); cur.first = gS[cur.second]; //cout << "USUNAC TO??? " << cur.first << " " << cur.second + 1 << endl; if (cur.first < d && usunTo[cur.second]) { //cout << "USUN TO!!!!!!!!: " << cur.second + 1 << endl; usunTo[cur.second] = false; for (int i = 0; i < g[cur.second].size(); i++) { if (usunTo[g[cur.second][i]]) { gS[g[cur.second][i]]--; if (gS[g[cur.second][i]] < d) { pq.push(make_pair(gS[g[cur.second][i]], g[cur.second][i])); } } } } } vector<int> res; for (int i = 0; i < n; i++) { if (odw[i] && usunTo[i]) { vector<int> curRes; stack<int> dfs; odw[i] = false; dfs.push(i); while (!dfs.empty()) { int aktW = dfs.top(); dfs.pop(); curRes.push_back(aktW); for (int j = 0; j < g[aktW].size(); j++) { if (odw[g[aktW][j]] && usunTo[g[aktW][j]]) { odw[g[aktW][j]] = false; dfs.push(g[aktW][j]); } } } if (curRes.size() > res.size()) res = curRes; } } if (res.empty()) puts("NIE"); else { printf("%d\n", res.size()); sort(res.begin(), res.end()); for (int i = 0; i < res.size(); i++) printf("%d ", res[i] + 1); printf("\n"); } return 0; } |