#include <iostream> #include <set> #include <vector> #include <algorithm> using namespace std; struct Node { Node() : Traversed(false),Active(true), NeighbourCount(0) {} vector <unsigned> Neighbours; bool Traversed; bool Active; unsigned NeighbourCount; } wierzcholek; //vector<Node> Q; //a moze vector<*NOde> ? vector<unsigned> Q; //a moze vector<*NOde> ? vector<unsigned> wynik_wierzcholki; unsigned ile_miast,ile_drog,d = 0; unsigned a,b=0; unsigned rozmiar_dfs; unsigned max_rozmiar_dfs = 0; unsigned nr_max_rozmiar_dfs = 0; void dfs(Node *graf,unsigned v){//DFS graf[v].Traversed = true; //cout << "badam wierzch : " << v <<endl; rozmiar_dfs++; wynik_wierzcholki.push_back(v); while(!graf[v].NeighbourCount == 0){ unsigned z = graf[v].Neighbours.back(); graf[v].Neighbours.pop_back(); graf[v].NeighbourCount--; if(graf[z].Active && graf[z].Traversed==false){ graf[z].Traversed=true; //rozmiar_dfs++; dfs(graf,z); } } } int main() { ios_base::sync_with_stdio(0); unsigned ile_miast,ile_drog,d = 0; unsigned a,b=0; cin >>ile_miast>>ile_drog>>d; Node * graf = new Node[ile_miast]; vector<unsigned> * wyniki = new vector<unsigned>[ile_miast]; //cout << "ile_miast:" <<ile_miast<<" ile_drog:"<<ile_drog<<" d:"<<d<<endl; while (cin >> a >> b) { //V.push_back(nominal); //cout << "a:"<<a<<" b:"<<b<<endl<<flush; a--;b--; graf[a].Neighbours.push_back(b); graf[b].Neighbours.push_back(a); graf[a].NeighbourCount++; graf[b].NeighbourCount++; } /*cout << "przed k-core "; for (unsigned i =0;i<ile_miast;i++) { cout << endl << "wierzcholek : " << i << " aktywny:" << graf[i].Active << " ile_sas :" << graf[i].NeighbourCount <<" sasiedzi:" ; for(std::vector<unsigned>::iterator it = graf[i].Neighbours.begin(); it != graf[i].Neighbours.end(); ++it) { std::cout << *it << ","; } } */ //cout << endl << "licze k-core " << endl; for (unsigned i =0;i<ile_miast;i++) { //cout << "robie graf : " << i <<endl; if (graf[i].Active==true && graf[i].NeighbourCount < d) { graf[i].Active = false; // cout << "wylaczam : " << i <<endl; Q.push_back(i); // cout << "do Q : " << i <<endl; while (!Q.empty()) { unsigned w = Q.back(); Q.pop_back(); // cout << "z Q biore : " << w <<endl; for(std::vector<unsigned>::iterator it = graf[w].Neighbours.begin(); it != graf[w].Neighbours.end(); ++it) { // cout << "zmniejszam NeighbourCount dla : " << *it <<endl; graf[*it].NeighbourCount--; if (graf[*it].Active==true && graf[*it].NeighbourCount<d) { graf[*it].Active = false; // cout << "wylaczam2 : " << *it <<endl; Q.push_back(*it); // cout << "wkladam do Q: " << *it <<endl <<flush; } } } } } /* //cout << endl<<"po k-core "; for (unsigned i =0;i<ile_miast;i++) { //cout << endl << "wierzcholek : " << i << " aktywny:" << graf[i].Active << " ile_sas :" << graf[i].NeighbourCount <<" sasiedzi:" ; for(std::vector<unsigned>::iterator it = graf[i].Neighbours.begin(); it != graf[i].Neighbours.end(); ++it) { //std::cout << *it << ","; } } */ //DFS : for (unsigned i =0;i<ile_miast;i++) { rozmiar_dfs = 0; if (graf[i].Active && graf[i].Traversed == false) { dfs(graf,i); wyniki[i] = wynik_wierzcholki; } //cout << endl<< "dla wierzch : " << i << "rozmiar_dfs : " << rozmiar_dfs <<endl; if (rozmiar_dfs > max_rozmiar_dfs) { max_rozmiar_dfs = rozmiar_dfs; nr_max_rozmiar_dfs = i; } } if (max_rozmiar_dfs != 0) { for (unsigned i =0;i<ile_miast;i++) {graf[i].Traversed = false;} wynik_wierzcholki.clear(); dfs(graf,nr_max_rozmiar_dfs); wynik_wierzcholki = wyniki[nr_max_rozmiar_dfs]; sort( wynik_wierzcholki.begin(), wynik_wierzcholki.end() ); cout << max_rozmiar_dfs <<endl; for( unsigned i = 0; i < wynik_wierzcholki.size(); ++i ) { cout << wynik_wierzcholki[i] + 1 << " "; } } else { cout << "NIE"; } //cout <<"koniec"; delete [] graf; 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 137 138 139 140 141 142 143 | #include <iostream> #include <set> #include <vector> #include <algorithm> using namespace std; struct Node { Node() : Traversed(false),Active(true), NeighbourCount(0) {} vector <unsigned> Neighbours; bool Traversed; bool Active; unsigned NeighbourCount; } wierzcholek; //vector<Node> Q; //a moze vector<*NOde> ? vector<unsigned> Q; //a moze vector<*NOde> ? vector<unsigned> wynik_wierzcholki; unsigned ile_miast,ile_drog,d = 0; unsigned a,b=0; unsigned rozmiar_dfs; unsigned max_rozmiar_dfs = 0; unsigned nr_max_rozmiar_dfs = 0; void dfs(Node *graf,unsigned v){//DFS graf[v].Traversed = true; //cout << "badam wierzch : " << v <<endl; rozmiar_dfs++; wynik_wierzcholki.push_back(v); while(!graf[v].NeighbourCount == 0){ unsigned z = graf[v].Neighbours.back(); graf[v].Neighbours.pop_back(); graf[v].NeighbourCount--; if(graf[z].Active && graf[z].Traversed==false){ graf[z].Traversed=true; //rozmiar_dfs++; dfs(graf,z); } } } int main() { ios_base::sync_with_stdio(0); unsigned ile_miast,ile_drog,d = 0; unsigned a,b=0; cin >>ile_miast>>ile_drog>>d; Node * graf = new Node[ile_miast]; vector<unsigned> * wyniki = new vector<unsigned>[ile_miast]; //cout << "ile_miast:" <<ile_miast<<" ile_drog:"<<ile_drog<<" d:"<<d<<endl; while (cin >> a >> b) { //V.push_back(nominal); //cout << "a:"<<a<<" b:"<<b<<endl<<flush; a--;b--; graf[a].Neighbours.push_back(b); graf[b].Neighbours.push_back(a); graf[a].NeighbourCount++; graf[b].NeighbourCount++; } /*cout << "przed k-core "; for (unsigned i =0;i<ile_miast;i++) { cout << endl << "wierzcholek : " << i << " aktywny:" << graf[i].Active << " ile_sas :" << graf[i].NeighbourCount <<" sasiedzi:" ; for(std::vector<unsigned>::iterator it = graf[i].Neighbours.begin(); it != graf[i].Neighbours.end(); ++it) { std::cout << *it << ","; } } */ //cout << endl << "licze k-core " << endl; for (unsigned i =0;i<ile_miast;i++) { //cout << "robie graf : " << i <<endl; if (graf[i].Active==true && graf[i].NeighbourCount < d) { graf[i].Active = false; // cout << "wylaczam : " << i <<endl; Q.push_back(i); // cout << "do Q : " << i <<endl; while (!Q.empty()) { unsigned w = Q.back(); Q.pop_back(); // cout << "z Q biore : " << w <<endl; for(std::vector<unsigned>::iterator it = graf[w].Neighbours.begin(); it != graf[w].Neighbours.end(); ++it) { // cout << "zmniejszam NeighbourCount dla : " << *it <<endl; graf[*it].NeighbourCount--; if (graf[*it].Active==true && graf[*it].NeighbourCount<d) { graf[*it].Active = false; // cout << "wylaczam2 : " << *it <<endl; Q.push_back(*it); // cout << "wkladam do Q: " << *it <<endl <<flush; } } } } } /* //cout << endl<<"po k-core "; for (unsigned i =0;i<ile_miast;i++) { //cout << endl << "wierzcholek : " << i << " aktywny:" << graf[i].Active << " ile_sas :" << graf[i].NeighbourCount <<" sasiedzi:" ; for(std::vector<unsigned>::iterator it = graf[i].Neighbours.begin(); it != graf[i].Neighbours.end(); ++it) { //std::cout << *it << ","; } } */ //DFS : for (unsigned i =0;i<ile_miast;i++) { rozmiar_dfs = 0; if (graf[i].Active && graf[i].Traversed == false) { dfs(graf,i); wyniki[i] = wynik_wierzcholki; } //cout << endl<< "dla wierzch : " << i << "rozmiar_dfs : " << rozmiar_dfs <<endl; if (rozmiar_dfs > max_rozmiar_dfs) { max_rozmiar_dfs = rozmiar_dfs; nr_max_rozmiar_dfs = i; } } if (max_rozmiar_dfs != 0) { for (unsigned i =0;i<ile_miast;i++) {graf[i].Traversed = false;} wynik_wierzcholki.clear(); dfs(graf,nr_max_rozmiar_dfs); wynik_wierzcholki = wyniki[nr_max_rozmiar_dfs]; sort( wynik_wierzcholki.begin(), wynik_wierzcholki.end() ); cout << max_rozmiar_dfs <<endl; for( unsigned i = 0; i < wynik_wierzcholki.size(); ++i ) { cout << wynik_wierzcholki[i] + 1 << " "; } } else { cout << "NIE"; } //cout <<"koniec"; delete [] graf; return 0; } |