// Grzegorz Bukowiec // PA 2015 - Zadanie 2B - Mistrzostwa #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct City { vector<int> roads; int howMany; bool unused; City() { howMany = 0; unused = true; } }; void bfs(int a, vector<int> *result, City *cities) { cities[a].unused = false; result->push_back(a); for (int j = 0; j < result->size(); ++j) { a = (*result)[j]; for (int i = 0; i < cities[a].roads.size(); ++i) { int b = cities[a].roads[i]; if (cities[b].unused) { cities[b].unused = false; result->push_back(b); } } } } int main() { ios_base::sync_with_stdio(0); int n, m, d, a, b; cin >> n; cin >> m; cin >> d; City *cities = new City[n]; for (int i = 0; i < m; ++i) { cin >> a; cin >> b; --a; --b; cities[a].roads.push_back(b); cities[b].roads.push_back(a); ++cities[a].howMany; ++cities[b].howMany; } queue<int> weaklyConnected; for (int i = 0; i < n; ++i) { if (cities[i].howMany < d) { weaklyConnected.push(i); cities[i].unused = false; } } while (!weaklyConnected.empty()) { a = weaklyConnected.front(); weaklyConnected.pop(); for (int i = 0; i < cities[a].roads.size(); ++i) { b = cities[a].roads[i]; if (cities[b].unused) { if (--cities[b].howMany < d) { weaklyConnected.push(b); cities[b].unused = false; } } } } vector<int> firstSet; vector<int> secondSet; bool resultInFirst = true; for (int i = 0; i < n; ++i) { if (cities[i].unused) { bfs(i, resultInFirst ? &secondSet : &firstSet, cities); if (!resultInFirst && firstSet.size() > secondSet.size() || resultInFirst && firstSet.size() < secondSet.size()) { if (resultInFirst) { firstSet.clear(); } else { secondSet.clear(); } resultInFirst = !resultInFirst; } else { if (resultInFirst) { secondSet.clear(); } else { firstSet.clear(); } } } } if (!resultInFirst) { sort(secondSet.begin(), secondSet.end()); if (secondSet.size() > 0) { cout << secondSet.size() << endl; for (int i = 0; i < secondSet.size(); ++i) { cout << secondSet[i] + 1 << " "; } } else { cout << "NIE"; } } else { sort(firstSet.begin(), firstSet.end()); if (firstSet.size() > 0) { cout << firstSet.size() << endl; for (int i = 0; i < firstSet.size(); ++i) { cout << firstSet[i] + 1 << " "; } } else { cout << "NIE"; } } delete [] cities; 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 | // Grzegorz Bukowiec // PA 2015 - Zadanie 2B - Mistrzostwa #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct City { vector<int> roads; int howMany; bool unused; City() { howMany = 0; unused = true; } }; void bfs(int a, vector<int> *result, City *cities) { cities[a].unused = false; result->push_back(a); for (int j = 0; j < result->size(); ++j) { a = (*result)[j]; for (int i = 0; i < cities[a].roads.size(); ++i) { int b = cities[a].roads[i]; if (cities[b].unused) { cities[b].unused = false; result->push_back(b); } } } } int main() { ios_base::sync_with_stdio(0); int n, m, d, a, b; cin >> n; cin >> m; cin >> d; City *cities = new City[n]; for (int i = 0; i < m; ++i) { cin >> a; cin >> b; --a; --b; cities[a].roads.push_back(b); cities[b].roads.push_back(a); ++cities[a].howMany; ++cities[b].howMany; } queue<int> weaklyConnected; for (int i = 0; i < n; ++i) { if (cities[i].howMany < d) { weaklyConnected.push(i); cities[i].unused = false; } } while (!weaklyConnected.empty()) { a = weaklyConnected.front(); weaklyConnected.pop(); for (int i = 0; i < cities[a].roads.size(); ++i) { b = cities[a].roads[i]; if (cities[b].unused) { if (--cities[b].howMany < d) { weaklyConnected.push(b); cities[b].unused = false; } } } } vector<int> firstSet; vector<int> secondSet; bool resultInFirst = true; for (int i = 0; i < n; ++i) { if (cities[i].unused) { bfs(i, resultInFirst ? &secondSet : &firstSet, cities); if (!resultInFirst && firstSet.size() > secondSet.size() || resultInFirst && firstSet.size() < secondSet.size()) { if (resultInFirst) { firstSet.clear(); } else { secondSet.clear(); } resultInFirst = !resultInFirst; } else { if (resultInFirst) { secondSet.clear(); } else { firstSet.clear(); } } } } if (!resultInFirst) { sort(secondSet.begin(), secondSet.end()); if (secondSet.size() > 0) { cout << secondSet.size() << endl; for (int i = 0; i < secondSet.size(); ++i) { cout << secondSet[i] + 1 << " "; } } else { cout << "NIE"; } } else { sort(firstSet.begin(), firstSet.end()); if (firstSet.size() > 0) { cout << firstSet.size() << endl; for (int i = 0; i < firstSet.size(); ++i) { cout << firstSet[i] + 1 << " "; } } else { cout << "NIE"; } } delete [] cities; return 0; } |