#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct City
{
City()
: inS(true)
, degree(0)
, inRes(0)
{
}
bool inS;
int degree;
int inRes;
vector<City*> neighbours;
};
int main()
{
ios_base::sync_with_stdio(false);
int N, M, D;
cin >> N >> M >> D;
vector<City> cities(N);
for (int i = 0; i < M; i++)
{
int A, B;
cin >> A >> B;
cities[A-1].neighbours.push_back(&cities[B-1]);
cities[B-1].neighbours.push_back(&cities[A-1]);
cities[A-1].degree++;
cities[B-1].degree++;
}
queue<City*> outOfS;
for (int i = 0; i < N; i++)
{
if (cities[i].degree < D)
{
cities[i].inS = false;
outOfS.push(&cities[i]);
}
}
while (!outOfS.empty())
{
City *city = outOfS.front();
outOfS.pop();
for (auto &n: city->neighbours)
{
if (n->inS)
{
n->degree--;
if (n->degree < D)
{
n->inS = false;
outOfS.push(n);
}
}
}
}
int whichOne = 1;
int maxcnt = 0;
int whichMax = 0;
for (auto &c: cities)
{
if (c.inS)
{
queue<City*> bfs;
bfs.push(&c);
c.inS = false;
c.inRes = whichOne;
int maxlocal = 1;
// go and mark inS false and result to whichOne
while (!bfs.empty())
{
City *city = bfs.front();
bfs.pop();
for (auto &n: city->neighbours)
{
if (n->inS)
{
maxlocal++;
n->inS = false;
n->inRes = whichOne;
bfs.push(n);
}
}
}
if (maxlocal>maxcnt)
{
maxcnt = maxlocal;
whichMax = whichOne;
}
whichOne++;
}
}
if (0 == maxcnt) cout << "NIE" << endl;
else
{
cout << maxcnt << endl;
for (int i = 0; i < N; i++)
{
if (cities[i].inRes == whichMax) cout << i+1 << " ";
}
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 | #include <iostream> #include <vector> #include <queue> using namespace std; struct City { City() : inS(true) , degree(0) , inRes(0) { } bool inS; int degree; int inRes; vector<City*> neighbours; }; int main() { ios_base::sync_with_stdio(false); int N, M, D; cin >> N >> M >> D; vector<City> cities(N); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; cities[A-1].neighbours.push_back(&cities[B-1]); cities[B-1].neighbours.push_back(&cities[A-1]); cities[A-1].degree++; cities[B-1].degree++; } queue<City*> outOfS; for (int i = 0; i < N; i++) { if (cities[i].degree < D) { cities[i].inS = false; outOfS.push(&cities[i]); } } while (!outOfS.empty()) { City *city = outOfS.front(); outOfS.pop(); for (auto &n: city->neighbours) { if (n->inS) { n->degree--; if (n->degree < D) { n->inS = false; outOfS.push(n); } } } } int whichOne = 1; int maxcnt = 0; int whichMax = 0; for (auto &c: cities) { if (c.inS) { queue<City*> bfs; bfs.push(&c); c.inS = false; c.inRes = whichOne; int maxlocal = 1; // go and mark inS false and result to whichOne while (!bfs.empty()) { City *city = bfs.front(); bfs.pop(); for (auto &n: city->neighbours) { if (n->inS) { maxlocal++; n->inS = false; n->inRes = whichOne; bfs.push(n); } } } if (maxlocal>maxcnt) { maxcnt = maxlocal; whichMax = whichOne; } whichOne++; } } if (0 == maxcnt) cout << "NIE" << endl; else { cout << maxcnt << endl; for (int i = 0; i < N; i++) { if (cities[i].inRes == whichMax) cout << i+1 << " "; } cout << endl; } return 0; } |
English