#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a); i<(b); ++i) //#define REP(i,n) FOR(i,1,(n)+1) typedef vector<int> vi; #define pb push_back #define sz size() typedef pair<int,int> pii; #define mp make_pair #define st first #define nd second typedef long long ll; #define INF 1000000001 //#define VAR(n,v) typeof(v) n=(v) #define ALL(t) t.begin(),t.end() #define SC(a) scanf("%d", &a) #define GET(a) int a; SC(a) #define ISDEBUG 1 #define dprintf(...) if(ISDEBUG) \ {printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");} template <class It> void dptab(It b, It e, const char* f="%d ") { if(ISDEBUG) { for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n"); }} struct vertex { set<int> adjacent; size_t deg() { return adjacent.size(); } }; void delete_vertex(int v, vector<vertex> &graph, int d) { set<int> adjacent_copy = move(graph[v].adjacent); graph[v].adjacent.clear(); for (auto i : adjacent_copy) { graph[i].adjacent.erase(v); if (graph[i].deg() < d) delete_vertex(i, graph, d); } } void find_subgraph(int v, vector<bool> &visited, vector<vertex> &graph, vector<int> &subgraph) { if (visited[v] || !graph[v].deg()) return; visited[v] = true; subgraph.push_back(v); for (auto i : graph[v].adjacent) { find_subgraph(i, visited, graph, subgraph); } } int main() { GET(n); GET(m); GET(d); vector<vertex> graph(n); while (m--) { GET(u); GET(v); --u, --v; graph[u].adjacent.insert(v); graph[v].adjacent.insert(u); } queue<int> to_be_deleted; FOR (i,0,n) { if (graph[i].deg() < d) delete_vertex(i, graph, d); } vector<int> max_subgraph; vector<bool> visited(n, false); FOR (i,0,n) { vector<int> subgraph; find_subgraph(i, visited, graph, subgraph); if (subgraph.size() > max_subgraph.size()) { max_subgraph = move(subgraph); } } sort(ALL(max_subgraph)); if (max_subgraph.size()) { printf("%d\n", (int) max_subgraph.size()); FOR (i,0,max_subgraph.size()) { printf("%d ", max_subgraph[i] + 1); } printf("\n"); } else { printf("NIE\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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a); i<(b); ++i) //#define REP(i,n) FOR(i,1,(n)+1) typedef vector<int> vi; #define pb push_back #define sz size() typedef pair<int,int> pii; #define mp make_pair #define st first #define nd second typedef long long ll; #define INF 1000000001 //#define VAR(n,v) typeof(v) n=(v) #define ALL(t) t.begin(),t.end() #define SC(a) scanf("%d", &a) #define GET(a) int a; SC(a) #define ISDEBUG 1 #define dprintf(...) if(ISDEBUG) \ {printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");} template <class It> void dptab(It b, It e, const char* f="%d ") { if(ISDEBUG) { for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n"); }} struct vertex { set<int> adjacent; size_t deg() { return adjacent.size(); } }; void delete_vertex(int v, vector<vertex> &graph, int d) { set<int> adjacent_copy = move(graph[v].adjacent); graph[v].adjacent.clear(); for (auto i : adjacent_copy) { graph[i].adjacent.erase(v); if (graph[i].deg() < d) delete_vertex(i, graph, d); } } void find_subgraph(int v, vector<bool> &visited, vector<vertex> &graph, vector<int> &subgraph) { if (visited[v] || !graph[v].deg()) return; visited[v] = true; subgraph.push_back(v); for (auto i : graph[v].adjacent) { find_subgraph(i, visited, graph, subgraph); } } int main() { GET(n); GET(m); GET(d); vector<vertex> graph(n); while (m--) { GET(u); GET(v); --u, --v; graph[u].adjacent.insert(v); graph[v].adjacent.insert(u); } queue<int> to_be_deleted; FOR (i,0,n) { if (graph[i].deg() < d) delete_vertex(i, graph, d); } vector<int> max_subgraph; vector<bool> visited(n, false); FOR (i,0,n) { vector<int> subgraph; find_subgraph(i, visited, graph, subgraph); if (subgraph.size() > max_subgraph.size()) { max_subgraph = move(subgraph); } } sort(ALL(max_subgraph)); if (max_subgraph.size()) { printf("%d\n", (int) max_subgraph.size()); FOR (i,0,max_subgraph.size()) { printf("%d ", max_subgraph[i] + 1); } printf("\n"); } else { printf("NIE\n"); } return 0; } |