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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>

#define MAX_SIZE 200000

#define W 1
#define G 2
#define B 3

struct edge {
	int node;
	struct edge *n;
};

struct graph_t {
	struct edge *graph[MAX_SIZE + 1];
	int node_deg[MAX_SIZE + 1];
	int N, M;
} g;

static unsigned char color[MAX_SIZE + 1];
static int bkp_stack [MAX_SIZE + 1];
static int bkp_top = 0;

static int d;

int add_edge(struct graph_t *g, int a, int b) {
	struct edge *e = calloc(1, sizeof(struct edge));
	struct edge *ptr;

	e->node = b;

	if (!e)
		return -ENOMEM;

	if (!g->graph[a]) {
		g->graph[a] = e;
	} else {
		for (ptr = g->graph[a]; ptr->n; ptr=ptr->n);
		ptr->n = e;
	}

	return 0;
}

void clean_graph (struct graph_t *g) {
	struct edge **ptr, *p;
	int i;

	for (i = 1; i <= g->N; i++) {
		if (!g->graph[i])
			continue;

		for (ptr = &g->graph[i]; g->graph[i];) {
			p = *ptr;
			*ptr = (*ptr)->n;
			free(p);
		}
	}
}

static int stack[MAX_SIZE + 1];
static int top = 0;
void stack_push(int n) {
	stack[top++] = n;
}

int stack_pop(void) {
	return stack[top--];
}

int stack_empty(void) {
	if (top == 0)
		return 1;

	return 0;
}

int bfs(struct graph_t *g, int u) {

	struct edge *ptr;
	int v;

	color[u] = G;

	for (ptr = g->graph[u]; ptr; ptr = ptr->n) {
		v = ptr->node;
		if (color[v] == W) {
			bfs(g, v);
		}
	}

	color[u] = B;
	stack_push(u);

	return 0;
}

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

int main () {
	int m, a, b, i;
	int *tab_ptr;

	top = bkp_top = 0;

	scanf("%d %d %d\n", &g.N, &g.M, &d);

	/* Init bfs */
	for (i = 1; i <= g.N; i++)
		color[i] = B;

	for (m = 1; m <= g.M; m++) {
		scanf("%d %d\n", &a, &b);

		add_edge(&g, a, b);
		add_edge(&g, b, a);

		g.node_deg[a]++;
		g.node_deg[b]++;

		if (g.node_deg[a] >= d) {
			color[a] = W;
		}

		if (g.node_deg[b] >= d) {
			color[b] = W;
		}
	}

	//RUN DFS on graph
	for (i = 1; i < g.N; i++) {
		if (color[i] == W) {
			bfs(&g, i);
			if (top > bkp_top) {
				memcpy(bkp_stack, stack, top * sizeof(int));
				bkp_top = top;
			}
			top = 0;
		}
	}

	tab_ptr = stack;

	if (bkp_top > top) {
		top = bkp_top;
		tab_ptr = bkp_stack;
	}

	qsort(tab_ptr, top, sizeof(int), cmpfunc);


	if (top == 1 || top == 0) {
		printf("NIE\n");
	} else {
		printf("%d\n", top);
		for (i = 0; i < top; i++)
			printf("%d ", tab_ptr[i]);

		printf("\n");
	}


	//Sort nodes of S
	clean_graph(&g);
	return 0;
}