#include <cstdio> #include <iostream> #include <vector> #include <set> using namespace std; typedef set<unsigned long> TNeighbours; struct SCity { unsigned long colour; TNeighbours neighbours; SCity() : colour(0) {} }; typedef vector<SCity> TGraph; typedef set<unsigned long> TNodes; static TGraph gGraph; static TNodes gToDelete; static TNodes gResult; struct SStackNode { unsigned long num; TNeighbours::iterator egdeIdx; SStackNode(unsigned long n, TNeighbours::iterator e) : num(n), egdeIdx(e){} }; void dfs(unsigned long start, unsigned long colour) // wglab { vector<SStackNode> dfsStack; TNodes lResult; gGraph[start].colour = colour; lResult.insert(start); dfsStack.push_back(SStackNode(start, gGraph[start].neighbours.begin())); while(!dfsStack.empty()) { SStackNode &curr = dfsStack.back(); if (curr.egdeIdx == gGraph[curr.num].neighbours.end()) { dfsStack.pop_back(); continue; } while (curr.egdeIdx != gGraph[curr.num].neighbours.end()) { unsigned long next = *(curr.egdeIdx); ++ curr.egdeIdx; if(gGraph[next].colour == 0) { gGraph[next].colour = colour; lResult.insert(next); dfsStack.push_back(SStackNode(next, gGraph[next].neighbours.begin())); break; } } } if(lResult.size() > gResult.size()) { gResult = lResult; } } int main() { unsigned long N, M, D, i, A, B, colour = 0; TNeighbours::iterator nIt; cin >> N >> M >> D; gGraph.resize(N+1); for (i = 0; i < M; ++ i) { cin >> A >> B; gGraph[A].neighbours.insert(B); gGraph[B].neighbours.insert(A); } for(i = 1; i <= N; ++ i) { if (gGraph[i].neighbours.size() < D) { for (nIt = gGraph[i].neighbours.begin(); nIt != gGraph[i].neighbours.end(); ++ nIt) { if (gGraph[*nIt].neighbours.erase(i) > 0 && *nIt < i && gGraph[*nIt].neighbours.size() < D) { gToDelete.insert(*nIt); } } gGraph[i].neighbours.clear(); } } while(!gToDelete.empty()) { i = *(gToDelete.begin()); for (nIt = gGraph[i].neighbours.begin(); nIt != gGraph[i].neighbours.end(); ++ nIt) { if (gGraph[*nIt].neighbours.erase(i) > 0 && gGraph[*nIt].neighbours.size() < D) { gToDelete.insert(*nIt); } } gGraph[i].neighbours.clear(); gToDelete.erase(i); } for(i = 1; i <= N; ++i) { if(gGraph[i].colour == 0 && gGraph[i].neighbours.size() >= D) { ++ colour; dfs(i, colour); } } if (gResult.empty()) { cout << "NIE" << endl; } else { cout << gResult.size() << endl; for (TNodes::iterator rIt = gResult.begin(); rIt != gResult.end(); ++ rIt) { cout << *rIt << " "; } cout << endl; } 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 135 136 | #include <cstdio> #include <iostream> #include <vector> #include <set> using namespace std; typedef set<unsigned long> TNeighbours; struct SCity { unsigned long colour; TNeighbours neighbours; SCity() : colour(0) {} }; typedef vector<SCity> TGraph; typedef set<unsigned long> TNodes; static TGraph gGraph; static TNodes gToDelete; static TNodes gResult; struct SStackNode { unsigned long num; TNeighbours::iterator egdeIdx; SStackNode(unsigned long n, TNeighbours::iterator e) : num(n), egdeIdx(e){} }; void dfs(unsigned long start, unsigned long colour) // wglab { vector<SStackNode> dfsStack; TNodes lResult; gGraph[start].colour = colour; lResult.insert(start); dfsStack.push_back(SStackNode(start, gGraph[start].neighbours.begin())); while(!dfsStack.empty()) { SStackNode &curr = dfsStack.back(); if (curr.egdeIdx == gGraph[curr.num].neighbours.end()) { dfsStack.pop_back(); continue; } while (curr.egdeIdx != gGraph[curr.num].neighbours.end()) { unsigned long next = *(curr.egdeIdx); ++ curr.egdeIdx; if(gGraph[next].colour == 0) { gGraph[next].colour = colour; lResult.insert(next); dfsStack.push_back(SStackNode(next, gGraph[next].neighbours.begin())); break; } } } if(lResult.size() > gResult.size()) { gResult = lResult; } } int main() { unsigned long N, M, D, i, A, B, colour = 0; TNeighbours::iterator nIt; cin >> N >> M >> D; gGraph.resize(N+1); for (i = 0; i < M; ++ i) { cin >> A >> B; gGraph[A].neighbours.insert(B); gGraph[B].neighbours.insert(A); } for(i = 1; i <= N; ++ i) { if (gGraph[i].neighbours.size() < D) { for (nIt = gGraph[i].neighbours.begin(); nIt != gGraph[i].neighbours.end(); ++ nIt) { if (gGraph[*nIt].neighbours.erase(i) > 0 && *nIt < i && gGraph[*nIt].neighbours.size() < D) { gToDelete.insert(*nIt); } } gGraph[i].neighbours.clear(); } } while(!gToDelete.empty()) { i = *(gToDelete.begin()); for (nIt = gGraph[i].neighbours.begin(); nIt != gGraph[i].neighbours.end(); ++ nIt) { if (gGraph[*nIt].neighbours.erase(i) > 0 && gGraph[*nIt].neighbours.size() < D) { gToDelete.insert(*nIt); } } gGraph[i].neighbours.clear(); gToDelete.erase(i); } for(i = 1; i <= N; ++i) { if(gGraph[i].colour == 0 && gGraph[i].neighbours.size() >= D) { ++ colour; dfs(i, colour); } } if (gResult.empty()) { cout << "NIE" << endl; } else { cout << gResult.size() << endl; for (TNodes::iterator rIt = gResult.begin(); rIt != gResult.end(); ++ rIt) { cout << *rIt << " "; } cout << endl; } return 0; } |