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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>

#define EACH_CITY(iter) (iter = 1; iter <= n; iter++)

using namespace std;

typedef unsigned int INT;

const INT N = 200000;
const INT M = 200000;
const INT D = 200000;

struct city;
struct connection;

struct city
{
	bool removed;
	INT id;
	INT group_id;
	INT connections_count;
	INT connectivity;
	connection *connections;

	void notify_adjacent_city_removal();
	void check();
	void remove();
	INT mark_city_group(INT gid, INT acc = 0);
	bool can_mark();
};

struct connection
{
	city * from;
	city * to;
};

INT n, m, d;
city cities[N+1];
connection *connections;

void inline city::notify_adjacent_city_removal()
{
	connectivity--;
	check();
}

void inline city::check()
{
	if (!removed && connectivity < d) remove();
}

void city::remove()
{
	INT i;
	removed = true;
	for (i = 0; i < connections_count; i++)
	{
		connections[i].to->notify_adjacent_city_removal();
	}
}

INT city::mark_city_group(INT gid, INT acc)
{
	INT i;
	group_id = gid;
	acc++;
	for (i = 0; i < connections_count; i++)
	{
		if (connections[i].to->can_mark())
		{
			acc += connections[i].to->mark_city_group(gid);
		}
	}
	return acc;
}

bool inline city::can_mark()
{
	return (!removed && group_id == 0);
}

int inline cmp_connections(const void * a, const void * b)
{
	return (((connection*) a)->from - ((connection*) b)->from);
}

void read_input()
{
	INT i, a, b;
	INT p, q;

	city *ct;

	cin >> n >> m >> d;

	connections = new connection[m * 2];

	for (i = 0; i < m; i++)
	{
		cin >> p >> q;
		connections[2 * i].from = &cities[p];
		connections[2 * i].to = &cities[q];
		connections[2 * i + 1].from = &cities[q];
		connections[2 * i + 1].to = &cities[p];
	}

	qsort(connections, 2 * m, sizeof(connection), cmp_connections);

	a = 0;
	for EACH_CITY(i)
	{
		ct = &cities[i];
		while (connections[a].from != ct && a < 2 * m) a++;
		b = a;
		while (connections[b].from == ct && b < 2 * m) b++;
		ct->id = i;
		ct->connections = &connections[a];
		ct->connections_count = ct->connectivity = b - a;
		a = b;
	}

}

void write_successful_output(INT k, INT gid)
{
	INT i;
	cout << k << endl;

	for EACH_CITY(i)
	{
		if (cities[i].group_id == gid)
		{
			cout << cities[i].id << " ";
		}
	}
}

void write_failure_output()
{
	cout << "NIE";
}

void initial_check()
{
	INT i;
	for EACH_CITY(i) cities[i].check();
}

void find_best()
{
	INT i, v;
	INT best_v = 0, best_i = 0;

	for EACH_CITY(i)
	{
		if (cities[i].can_mark())
		{
			v = cities[i].mark_city_group(i);
			if (v > best_v) {
				best_v = v;
				best_i = i;
			};
		}
	}

	if (best_v > 0)
	{
		write_successful_output(best_v, best_i);
	}
	else
	{
		write_failure_output();
	}
}

int main(int argc, char** argv)
{
	read_input();
	initial_check();
	find_best();

	return 0;
}