import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; class Graph { class Node { private TreeSet<Integer> neighbors = new TreeSet<Integer>(); private boolean visited; public TreeSet<Integer> getNeighbors() { return neighbors; } public void addNeighbor(Integer n) { neighbors.add(n); } public void removeNeighbor(Integer n) { neighbors.remove(n); } public int getDegree() { return neighbors.size(); } public boolean isVisited() { return visited; } public void setVisited(boolean visited) { this.visited = visited; } public void visit(TreeSet<Integer> component) { setVisited(true); for (Integer neighbor : getNeighbors()) { if (nodes[neighbor] != null && !nodes[neighbor].isVisited()) { component.add(Integer.valueOf(neighbor)); nodes[neighbor].visit(component); } } } } private Node[] nodes; private ArrayList<TreeSet<Integer>> components; public Graph(int n) { nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(); } } public void addEdge(int b, int e) { nodes[b].addNeighbor(Integer.valueOf(e)); nodes[e].addNeighbor(Integer.valueOf(b)); } public void clean(int d) { TreeSet<Integer> forRemove = new TreeSet<Integer>(); for (int i = 0; i < nodes.length; i++) { if (nodes[i].getDegree() < d) forRemove.add(Integer.valueOf(i)); } while (!forRemove.isEmpty()) { Integer removed = forRemove.pollFirst(); for (Integer neighbor : nodes[removed.intValue()].getNeighbors()) { nodes[neighbor].removeNeighbor(removed); if (nodes[neighbor].getDegree() < d) forRemove.add(neighbor); } nodes[removed.intValue()] = null; } } public void prepareComponents() { components = new ArrayList<TreeSet<Integer>>(); for (int i = 0; i < nodes.length; i++) { if (nodes[i] != null && !nodes[i].isVisited()) { TreeSet<Integer> component = new TreeSet<Integer>(); component.add(Integer.valueOf(i)); nodes[i].visit(component); components.add(component); } } } public TreeSet<Integer> getMaxComponent() { if (components.isEmpty()) return null; TreeSet<Integer> max = components.get(0); for (TreeSet<Integer> c : components) if (c.size() > max.size()) max = c; return max; } } public class mis { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt(); Graph graph = new Graph(n); for (int i = 0; i < m; i++) { int a = in.nextInt(); int b = in.nextInt(); graph.addEdge(a - 1, b - 1); } in.close(); graph.clean(d); graph.prepareComponents(); TreeSet<Integer> result = graph.getMaxComponent(); if (result == null) System.out.println("NIE"); else { System.out.println(result.size()); for (Integer i : result) System.out.print((i.intValue() + 1) + " "); } } }
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 | import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; class Graph { class Node { private TreeSet<Integer> neighbors = new TreeSet<Integer>(); private boolean visited; public TreeSet<Integer> getNeighbors() { return neighbors; } public void addNeighbor(Integer n) { neighbors.add(n); } public void removeNeighbor(Integer n) { neighbors.remove(n); } public int getDegree() { return neighbors.size(); } public boolean isVisited() { return visited; } public void setVisited(boolean visited) { this.visited = visited; } public void visit(TreeSet<Integer> component) { setVisited(true); for (Integer neighbor : getNeighbors()) { if (nodes[neighbor] != null && !nodes[neighbor].isVisited()) { component.add(Integer.valueOf(neighbor)); nodes[neighbor].visit(component); } } } } private Node[] nodes; private ArrayList<TreeSet<Integer>> components; public Graph(int n) { nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(); } } public void addEdge(int b, int e) { nodes[b].addNeighbor(Integer.valueOf(e)); nodes[e].addNeighbor(Integer.valueOf(b)); } public void clean(int d) { TreeSet<Integer> forRemove = new TreeSet<Integer>(); for (int i = 0; i < nodes.length; i++) { if (nodes[i].getDegree() < d) forRemove.add(Integer.valueOf(i)); } while (!forRemove.isEmpty()) { Integer removed = forRemove.pollFirst(); for (Integer neighbor : nodes[removed.intValue()].getNeighbors()) { nodes[neighbor].removeNeighbor(removed); if (nodes[neighbor].getDegree() < d) forRemove.add(neighbor); } nodes[removed.intValue()] = null; } } public void prepareComponents() { components = new ArrayList<TreeSet<Integer>>(); for (int i = 0; i < nodes.length; i++) { if (nodes[i] != null && !nodes[i].isVisited()) { TreeSet<Integer> component = new TreeSet<Integer>(); component.add(Integer.valueOf(i)); nodes[i].visit(component); components.add(component); } } } public TreeSet<Integer> getMaxComponent() { if (components.isEmpty()) return null; TreeSet<Integer> max = components.get(0); for (TreeSet<Integer> c : components) if (c.size() > max.size()) max = c; return max; } } public class mis { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt(); Graph graph = new Graph(n); for (int i = 0; i < m; i++) { int a = in.nextInt(); int b = in.nextInt(); graph.addEdge(a - 1, b - 1); } in.close(); graph.clean(d); graph.prepareComponents(); TreeSet<Integer> result = graph.getMaxComponent(); if (result == null) System.out.println("NIE"); else { System.out.println(result.size()); for (Integer i : result) System.out.print((i.intValue() + 1) + " "); } } } |