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
import java.util.*;

public class mis {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int nodes = scanner.nextInt();
        int edges = scanner.nextInt();
        int K = scanner.nextInt();
        Solution solution = new Solution(K, nodes);

        for (int i = 0; i < edges; i++) {
            solution.addEdge(scanner.nextInt() - 1, scanner.nextInt() - 1);
        }
        solution.commit();

        solution.reduceNodes();
        solution.findMaxSubGraph();
        if (solution.foundGraph()) {
            Collection<Node> result = solution.getFoundNodes();
            System.out.println(result.size());
            for (Node node : result) {
                System.out.print((node.id + 1) + " ");
            }
            System.out.println();
        } else {
            System.out.println("NIE");
        }
    }

    private static class Solution {
        private int K;
        private Node[] nodes;
        private LinkedList<Node> nodesLL;
        private List<Node> maxSubGraph;

        public Solution(int K, int nodesCount) {
            this.K = K;
            this.nodes = new Node[nodesCount];
            for (int i = 0; i < nodesCount; i++) {
                this.nodes[i] = new Node(i);
            }
            this.nodesLL = new LinkedList<>();
        }

        public void addEdge(int from, int to) {
            this.nodes[from].addEdge(to);
            this.nodes[to].addEdge(from);
        }

        public void commit() {
            Collections.addAll(this.nodesLL, this.nodes);
        }

        public void reduceNodes() {
            boolean done = false;

            while (!done) {
                done = true;
                ListIterator<Node> i = nodesLL.listIterator();
                while (i.hasNext()) {
                    Node node = i.next();
                    if (node.getEdgesCount() < K) {
                        done = false;
                        removeEdges(node);
                        i.remove();
                    }
                }
            }
        }

        private void removeEdges(Node node) {
            for (int neighbor : node.neighbors) {
                nodes[neighbor].removeEdge(node.id);
            }
        }

        public void findMaxSubGraph() {
            Node node;

            while ((node = findUncheckedNode()) != null) {
                List<Node> subGraph = calculateGraphNodes(node);
                if (maxSubGraph == null || maxSubGraph.size() < subGraph.size()) {
                    maxSubGraph = subGraph;
                }
            }

            if (maxSubGraph != null) {
                maxSubGraph.sort(new Comparator<Node>() {
                    @Override
                    public int compare(Node o1, Node o2) {
                        return o1.id - o2.id;
                    }
                });
            }
        }

        private List<Node> calculateGraphNodes(Node node) {
            node.check();
            LinkedList<Node> queue = new LinkedList<>();
            queue.add(node);
            LinkedList<Node> result = new LinkedList<>();

            while (!queue.isEmpty()) {
                node = queue.poll();
                result.push(node);
                for (int neighborId : node.neighbors) {
                    Node neighbor = nodes[neighborId];
                    if (!neighbor.isChecked()) {
                        neighbor.check();
                        queue.add(neighbor);
                    }
                }
            }
            return result;
        }

        private Node findUncheckedNode() {
            for (Node node : nodesLL) {
                if (!node.isChecked()) {
                    return node;
                }
            }
            return null;
        }

        public List<Node> getFoundNodes() {
            return maxSubGraph;
        }

        public boolean foundGraph() {
            return maxSubGraph != null;
        }
    }

    private static class Node {

        private int id;
        private LinkedList<Integer> neighbors;
        private boolean checked;

        public Node(int id) {
            this.id = id;
            this.neighbors = new LinkedList<>();
            this.checked = false;
        }

        public void addEdge(int target) {
            neighbors.add(target);
        }

        public int getEdgesCount() {
            return neighbors.size();
        }

        public void removeEdge(int id) {
            neighbors.remove(new Integer(id));
        }

        public boolean isChecked() {
            return checked;
        }

        public void check() {
            checked = true;
        }
    }
}