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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class mis {

	static Node[] nodes;
	static Queue<Node> queue = new LinkedList<Node>();
	static int nodesRemoved;

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), m = sc.nextInt(), d = sc.nextInt();

		nodes = new Node[n + 1];
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt(), b = sc.nextInt();
			Node nodeA = getOrCreate(a);
			Node nodeB = getOrCreate(b);
			nodeA.addChild(b);
			nodeB.addChild(a);
		}

		int prepared = 1;
		while (true) {
			nodesRemoved = 0;
			for (int i = 1; i <= n; i++) {
				Node node = nodes[i];
				if (node != null && !node.removed && node.prepared < prepared && node.childsCount < d) {
					node.prepared = prepared;
					node.removed = true;
					queue.offer(node);
					prepareNodes(d, prepared);
				}
			}
			prepared += 1;
			if (nodesRemoved == 0) {
				break;
			}
		}

		// Visit cities
		int color = 1;
		for (int i = 1; i <= n; i++) {
			Node node = nodes[i];
			if (node != null && !node.removed && node.color == 0 && node.visits == 0) {
				node.color = color;
				queue.offer(node);
				visitNodes(color);
				color += 1;
			}
		}

		// Find solution
		int[] solutions = new int[color + 1];
		for (int i = 1; i <= n; i++) {
			Node node = nodes[i];
			if (node != null && node.visits >= d) {
				solutions[node.color] += 1;
			}
		}
		int maxColor = -1;
		int maxLength = 0;
		for (int col = 1; col < solutions.length; ++col) {
			int count = solutions[col];
			if (count > maxLength) {
				maxColor = col;
				maxLength = count;
			}
		}

		if (maxColor == -1) {
			System.out.println("NIE");
		} else {
			int[] result = new int[maxLength];
			int idx = 0;
			for (Node node : nodes) {
				if (node != null && node.visits >= d && node.color == maxColor) {
					result[idx++] = node.n;
				}
			}
			Arrays.sort(result);
			System.out.println(maxLength);
			System.out.println(arrayToString(result));
		}
	}

	static void prepareNodes(int d, int prepared) {
		while (!queue.isEmpty()) {
			Node node = queue.poll();
			for (Integer idx : node.childs) {
				Node child = nodes[idx];
				if (!child.removed) {
					child.childsCount -= 1;
					if (child.prepared < prepared && child.childsCount < d) {
						child.prepared = prepared;
						child.removed = true;
						nodesRemoved += 1;
						queue.offer(child);
					}
				}
			}
		}
	}

	static void visitNodes(int color) {
		while (!queue.isEmpty()) {
			Node node = queue.poll();
			for (Integer idx : node.childs) {
				Node child = nodes[idx];
				if (!child.removed) {
					child.visits += 1;
					if (child.color == 0) {
						child.color = color;
						queue.offer(child);
					}
				}
			}
		}
	}

	static Node getOrCreate(int n) {
		if (nodes[n] == null) {
			nodes[n] = new Node(n);
		}
		return nodes[n];
	}

	static void printNodes() {
		for (Node node : nodes) {
			if (node != null && !node.removed) System.out.println(node);
		}
	}

	static String arrayToString(int[] arr) {
		StringBuilder sb = new StringBuilder();
		for (int nodeIdx : arr) {
			sb.append(nodeIdx).append(' ');
		}
		return sb.toString();
	}

	static class Node {
		int n;
		boolean removed;
		int prepared;
		int color;
		int visits;
		int childsCount;
		ArrayList<Integer> childs = new ArrayList<Integer>();

		Node(int n) {
			this.n = n;
		}

		void addChild(int n) {
			childs.add(n);
			childsCount += 1;
		}

		public String toString() {
			return "N: " + n + " Color: " + color + " Visits: " + visits + " Removed: " + removed + " Childs: " + childs + " Count: " + childsCount;
		}
	}
}