Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <cstdio> #include <algorithm> #include <set> #include <vector> #include <queue> //#define DBG using namespace std; int n, m, d; struct Miasto { bool ok; // czy jeszcze brane pod uwag� vector<int> drogi; int ile; // liczba dr�g do miast branych pod uwag� }; vector<Miasto> miasta; /////////////////////////////////////////////////////////////////////////////// void licz() { int i, j; set<pair<int, int> > pq; // (liczba dr�g, numer miasta) for(i = 0; i < n; ++i) { // wrzuci� wszystkie miasta do pq miasta[i].ok = true; miasta[i].ile = miasta[i].drogi.size(); pq.insert(pair<int, int>(miasta[i].ile, i)); } // wybra� miasta o najmniejszej liczbie dr�g do dobrych miast i je�li to mniej ni� d to wyrzuci� while(pq.size()) { set<pair<int, int> >::iterator it = pq.begin(); if (it->first >= d) break; // pozosta�y same miasta o liczbie dr�g >= d int nr = it->second; miasta[nr].ok = false; pq.erase(it); // usu� drogi for (i = 0; i < miasta[nr].drogi.size(); ++i) { int cel = miasta[nr].drogi[i]; if (miasta[cel].ok) { // poprawi� liczb� dr�g w pq dla miasta docelowego pq.erase(pair<int, int>(miasta[cel].ile, cel)); miasta[cel].ile--; pq.insert(pair<int, int>(miasta[cel].ile, cel)); } } } #ifdef DBG for (i = 0; i < n; ++i) printf("%d:(ok=%d, ile=%d)\n", i, (int)miasta[i].ok, miasta[i].ile); #endif // DBG vector<int> best; // najwi�ksza grupa miast for (i = 0; i < n; ++i) if (miasta[i].ok) { vector<int> kand; // kandydat na popraw� wyniku queue<int> q; // miasta do odwiedzenia (BSF) q.push(i); miasta[i].ok = false; while (q.size()) { int nr = q.front(); kand.push_back(nr); // kolejne miasto w aktualnej grupie q.pop(); for (j = 0; j < miasta[nr].drogi.size(); ++j) { int cel = miasta[nr].drogi[j]; if (miasta[cel].ok) { miasta[cel].ok = false; q.push(cel); } } } if (kand.size() > best.size()) best = kand; } if (best.size()) { sort(best.begin(), best.end()); printf("%d\n", best.size()); for (i = 0; i < best.size(); ++i) printf("%d ", best[i] + 1); printf("\n"); } else printf("NIE\n"); } /////////////////////////////////////////////////////////////////////////////// void czytaj() { int i, a, b; scanf("%d%d%d", &n, &m, &d); for (i = 0; i < n; ++i) { Miasto m; miasta.push_back(m); } for (i = 0; i < m; ++i) { scanf("%d%d", &a, &b); miasta[a - 1].drogi.push_back(b - 1); miasta[b - 1].drogi.push_back(a - 1); } } int main() { czytaj(); licz(); 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 | #include <cstdio> #include <algorithm> #include <set> #include <vector> #include <queue> //#define DBG using namespace std; int n, m, d; struct Miasto { bool ok; // czy jeszcze brane pod uwag� vector<int> drogi; int ile; // liczba dr�g do miast branych pod uwag� }; vector<Miasto> miasta; /////////////////////////////////////////////////////////////////////////////// void licz() { int i, j; set<pair<int, int> > pq; // (liczba dr�g, numer miasta) for(i = 0; i < n; ++i) { // wrzuci� wszystkie miasta do pq miasta[i].ok = true; miasta[i].ile = miasta[i].drogi.size(); pq.insert(pair<int, int>(miasta[i].ile, i)); } // wybra� miasta o najmniejszej liczbie dr�g do dobrych miast i je�li to mniej ni� d to wyrzuci� while(pq.size()) { set<pair<int, int> >::iterator it = pq.begin(); if (it->first >= d) break; // pozosta�y same miasta o liczbie dr�g >= d int nr = it->second; miasta[nr].ok = false; pq.erase(it); // usu� drogi for (i = 0; i < miasta[nr].drogi.size(); ++i) { int cel = miasta[nr].drogi[i]; if (miasta[cel].ok) { // poprawi� liczb� dr�g w pq dla miasta docelowego pq.erase(pair<int, int>(miasta[cel].ile, cel)); miasta[cel].ile--; pq.insert(pair<int, int>(miasta[cel].ile, cel)); } } } #ifdef DBG for (i = 0; i < n; ++i) printf("%d:(ok=%d, ile=%d)\n", i, (int)miasta[i].ok, miasta[i].ile); #endif // DBG vector<int> best; // najwi�ksza grupa miast for (i = 0; i < n; ++i) if (miasta[i].ok) { vector<int> kand; // kandydat na popraw� wyniku queue<int> q; // miasta do odwiedzenia (BSF) q.push(i); miasta[i].ok = false; while (q.size()) { int nr = q.front(); kand.push_back(nr); // kolejne miasto w aktualnej grupie q.pop(); for (j = 0; j < miasta[nr].drogi.size(); ++j) { int cel = miasta[nr].drogi[j]; if (miasta[cel].ok) { miasta[cel].ok = false; q.push(cel); } } } if (kand.size() > best.size()) best = kand; } if (best.size()) { sort(best.begin(), best.end()); printf("%d\n", best.size()); for (i = 0; i < best.size(); ++i) printf("%d ", best[i] + 1); printf("\n"); } else printf("NIE\n"); } /////////////////////////////////////////////////////////////////////////////// void czytaj() { int i, a, b; scanf("%d%d%d", &n, &m, &d); for (i = 0; i < n; ++i) { Miasto m; miasta.push_back(m); } for (i = 0; i < m; ++i) { scanf("%d%d", &a, &b); miasta[a - 1].drogi.push_back(b - 1); miasta[b - 1].drogi.push_back(a - 1); } } int main() { czytaj(); licz(); return 0; } |