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
#include <cstdio>
#include <cstdlib>

#define CITY_MAX 200001

int t[400000][2];
bool visited[CITY_MAX], inset[CITY_MAX], marked[CITY_MAX];
int local_set[CITY_MAX], max_set[CITY_MAX], local_stack[CITY_MAX];

int comp(const void *a, const void *b)
{
	int *x = (int *)a;
	int *y = (int *)b;

	if (x[0] == y[0]) {
		return x[1] - y[1];
	} else {
		return x[0] - y[0];
	}
}

int comp_simple(const void *a, const void *b)
{
	return (*(int *)a - *(int *)b);
}

int find_neighbors(int t[][2], int size, int city)
{
	int left = 0;
	int right = size;
	int found = -2;
	int last_middle = -1;

	do {
		int middle = (left + right) / 2;
		if (middle == last_middle) {
			found = -1;
		}
		last_middle = middle;
		if (t[middle][0] == city) {
			if (!middle) {
				found = middle;
			} else if (t[middle-1][0] < t[middle][0]) {
				found = middle;
			} else {
				right = middle;
			}
		} else if (t[middle][0] < city) {
			left = middle;
		} else {
			right = middle;
		}
	} while (found == -2) ;

	return found;
}

int main()
{
	int n, m, d;

	scanf("%d %d %d", &n, &m, &d);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		t[2*i][0] = a;
		t[2*i][1] = b;
		t[2*i+1][0] = b;
		t[2*i+1][1] = a;
	}

	qsort(t, 2 * m, 2 * sizeof(int), comp);

	for (int city = 1; city <= n; city++) {
		inset[city] = true;
	}

	int max_set_ptr = 0;

	for (int global_city = 1; global_city <= n; global_city++) {
		int local_stack_ptr = 0;
		int local_set_ptr = 0;
		int city = global_city;
		while ((inset[city] && !visited[city]) || (local_stack_ptr > 0)) {
if (!visited[city] && inset[city]) {
			visited[city] = true;
			int neighbors_ptr = find_neighbors(t, 2 * m, city);
			if (neighbors_ptr >= 0) {
				int d_counter = 0;
				int old_local_stack_ptr = local_stack_ptr;
				while ((neighbors_ptr < 2 * m) && (t[neighbors_ptr][0] == city)) {
					int neighbor = t[neighbors_ptr][1];
					if (inset[neighbor]) {
						d_counter++;
						if (!visited[neighbor]) {
							local_stack[local_stack_ptr] = neighbor;
							local_stack_ptr++;
						}
					}
					neighbors_ptr++;
				}
				if ((d_counter >= d) && !marked[city]) {
					local_set[local_set_ptr] = city;
					local_set_ptr++;
					marked[city] = true;
				} else if (d_counter < d) {
					inset[city] = false;
					local_stack_ptr = old_local_stack_ptr;
					neighbors_ptr--;
					while ((neighbors_ptr >= 0) && (t[neighbors_ptr][0] == city)) {
						int neighbor = t[neighbors_ptr][1];
						if (inset[neighbor] && visited[neighbor]) {
							visited[neighbor] = false;
							local_stack[local_stack_ptr] = neighbor;
							local_stack_ptr++;
						}
						neighbors_ptr--;
					}
				} else {
					local_stack_ptr = old_local_stack_ptr;
				}
			} else {
				inset[city] = false;
			}
}
			if (local_stack_ptr > 0) {
				local_stack_ptr--;
				city = local_stack[local_stack_ptr];
			}
		}
		int local_set_size = 0;
		for (int i = 0; i < local_set_ptr; i++) {
			int city = local_set[i];
visited[city] = false;
			marked[city] = false;
			if (inset[city]) {
				local_set_size++;
			}
		}
		if (local_set_size > max_set_ptr) {
			max_set_ptr = 0;
			for (int i = 0; i < local_set_ptr; i++) {
				int city = local_set[i];
				if (inset[city]) {
					max_set[max_set_ptr] = local_set[i];
					max_set_ptr++;
				}
inset[city] = true;
			}
		}
	}

	qsort(max_set, max_set_ptr, sizeof(int), comp_simple);

	if (max_set_ptr > 1) {
		printf("%d\n", max_set_ptr);
		for (int i = 0; i < max_set_ptr; i++) {
			printf("%d ", max_set[i]);
		}
		printf("\n");
	} else {
		printf("NIE\n");
	}
}