#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <pair <int, int> > droga;
vector <int> zbior1;
vector <int> zbior2;
int czypolaczone[200001];
bool uzytemiasto[200001];
int ilszlakow;
int ilwsi;
int main()
{
int x, y;
int standardunijny;
cin >> ilwsi >> ilszlakow >> standardunijny;
for (int i=0; i<ilszlakow; i++)
{
cin >> x >> y;
czypolaczone[x]++;
czypolaczone[y]++;
pair <int, int> z;
z.first=x;
z.second=y;
droga.push_back(z);
}
bool odpowiedz=1;
while (odpowiedz==1)
{
odpowiedz=0;
for (int i=0; i<droga.size(); i=i)
{
if (czypolaczone[droga[i].first]<standardunijny || czypolaczone[droga[i].second]<standardunijny)
{
czypolaczone[droga[i].first]--;
czypolaczone[droga[i].second]--;
droga.erase(droga.begin()+i);
odpowiedz=1;
}
else {i++;}
}
}
if (droga.empty())
{
cout << "NIE"; return 0;
}
while (droga.size()>0)
{
if (zbior2.size()<=zbior1.size())
{
zbior2.clear();
zbior2.push_back(droga[0].first);
for (int f=0; f<zbior2.size(); f++)
{
for (int i=0; i<droga.size(); i++)
{
if (droga[i].first==zbior2[f])
{
if (!uzytemiasto[droga[i].second])
{
zbior2.push_back(droga[i].second);
uzytemiasto[droga[i].second]=1;
}
droga.erase(droga.begin()+i);
i--;
}
else if (droga[i].second==zbior2[f])
{
if (!uzytemiasto[droga[i].first])
{
zbior2.push_back(droga[i].first);
uzytemiasto[droga[i].first]=1;
}
droga.erase(droga.begin()+i);
i--;
}
}
}
}
else
{
zbior1.clear();
zbior1.push_back(droga[0].first);
for (int f=0; f<zbior1.size(); f++)
{
for (int i=0; i<droga.size(); i++)
{
if (droga[i].first==zbior1[f])
{
if (!uzytemiasto[droga[i].second])
{
zbior1.push_back(droga[i].second);
uzytemiasto[droga[i].second]=1;
}
droga.erase(droga.begin()+i);
i--;
}
else if (droga[i].second==zbior1[f])
{
if (!uzytemiasto[droga[i].first])
{
zbior1.push_back(droga[i].first);
uzytemiasto[droga[i].first]=1;
}
droga.erase(droga.begin()+i);
i--;
}
}
}
}
}
if (zbior1.size()>zbior2.size())
{
sort(zbior1.begin(), zbior1.end());
cout << zbior1.size() << endl;
for (int i=0; i<zbior1.size(); i++) cout << zbior1[i] << " ";
return 0;
}
sort(zbior2.begin(), zbior2.end());
cout << zbior2.size() << endl;
for (int i=0; i<zbior2.size(); i++) cout << zbior2[i] << " ";
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector <pair <int, int> > droga; vector <int> zbior1; vector <int> zbior2; int czypolaczone[200001]; bool uzytemiasto[200001]; int ilszlakow; int ilwsi; int main() { int x, y; int standardunijny; cin >> ilwsi >> ilszlakow >> standardunijny; for (int i=0; i<ilszlakow; i++) { cin >> x >> y; czypolaczone[x]++; czypolaczone[y]++; pair <int, int> z; z.first=x; z.second=y; droga.push_back(z); } bool odpowiedz=1; while (odpowiedz==1) { odpowiedz=0; for (int i=0; i<droga.size(); i=i) { if (czypolaczone[droga[i].first]<standardunijny || czypolaczone[droga[i].second]<standardunijny) { czypolaczone[droga[i].first]--; czypolaczone[droga[i].second]--; droga.erase(droga.begin()+i); odpowiedz=1; } else {i++;} } } if (droga.empty()) { cout << "NIE"; return 0; } while (droga.size()>0) { if (zbior2.size()<=zbior1.size()) { zbior2.clear(); zbior2.push_back(droga[0].first); for (int f=0; f<zbior2.size(); f++) { for (int i=0; i<droga.size(); i++) { if (droga[i].first==zbior2[f]) { if (!uzytemiasto[droga[i].second]) { zbior2.push_back(droga[i].second); uzytemiasto[droga[i].second]=1; } droga.erase(droga.begin()+i); i--; } else if (droga[i].second==zbior2[f]) { if (!uzytemiasto[droga[i].first]) { zbior2.push_back(droga[i].first); uzytemiasto[droga[i].first]=1; } droga.erase(droga.begin()+i); i--; } } } } else { zbior1.clear(); zbior1.push_back(droga[0].first); for (int f=0; f<zbior1.size(); f++) { for (int i=0; i<droga.size(); i++) { if (droga[i].first==zbior1[f]) { if (!uzytemiasto[droga[i].second]) { zbior1.push_back(droga[i].second); uzytemiasto[droga[i].second]=1; } droga.erase(droga.begin()+i); i--; } else if (droga[i].second==zbior1[f]) { if (!uzytemiasto[droga[i].first]) { zbior1.push_back(droga[i].first); uzytemiasto[droga[i].first]=1; } droga.erase(droga.begin()+i); i--; } } } } } if (zbior1.size()>zbior2.size()) { sort(zbior1.begin(), zbior1.end()); cout << zbior1.size() << endl; for (int i=0; i<zbior1.size(); i++) cout << zbior1[i] << " "; return 0; } sort(zbior2.begin(), zbior2.end()); cout << zbior2.size() << endl; for (int i=0; i<zbior2.size(); i++) cout << zbior2[i] << " "; return 0; } |
English