import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; class Graph { private Map<Integer, Set<Integer>> adj; public Graph(int n) { adj = new LinkedHashMap<>(); for (int i = 1; i <= n; i++) { adj.put(i, new HashSet<>()); } } public void insertEdge(int a, int b) { adj.get(a).add(b); adj.get(b).add(a); } public int getDegree(int a) { return adj.get(a).size(); } public void removeNodes(Queue<Integer> nodesToRemove, int d) { while (!nodesToRemove.isEmpty()) { int node = nodesToRemove.poll(); if (adj.containsKey(node)) { for (int neighbour : adj.get(node)) { adj.get(neighbour).remove(node); if (getDegree(neighbour) < d) { nodesToRemove.add(neighbour); } } adj.remove(node); } } } public Set<Integer> getLargestComponent() { Map<Integer, Boolean> visited = new HashMap<>(); for (int node : adj.keySet()) { visited.put(node, false); } Set<Integer> largestComponent = new TreeSet<>(); for (int node : adj.keySet()) { Set<Integer> component = new TreeSet<>(); if (!visited.get(node)) { visit(node, visited, component); } if (component.size() > largestComponent.size()) { largestComponent = component; } } return largestComponent; } private void visit(int node, Map<Integer, Boolean> visited, Set<Integer> component) { visited.put(node, true); component.add(node); for (int neighbour : adj.get(node)) { if (!visited.get(neighbour)) { visit(neighbour, visited, component); } } } } public class mis { public static void main(String[] args) { Scanner SC = new Scanner(System.in); int n = SC.nextInt(); int m = SC.nextInt(); int d = SC.nextInt(); Graph G = new Graph(n); for (int i = 0; i < m; i++) { int a = SC.nextInt(); int b = SC.nextInt(); G.insertEdge(a, b); } Queue<Integer> nodesToRemove = getInitialNodesToDelete(G, n, d); G.removeNodes(nodesToRemove, d); Set<Integer> largestComponent = G.getLargestComponent(); if (largestComponent.isEmpty()) { System.out.println("NIE"); } else { System.out.println(largestComponent.size()); for (Integer node : largestComponent) { System.out.print(node + " "); } System.out.println(); } } private static Queue<Integer> getInitialNodesToDelete(Graph G, int n, int d) { Queue<Integer> initialNodesToDelete = new LinkedList<>(); for (int i = 1; i <= n; i++) { if (G.getDegree(i) < d) { initialNodesToDelete.add(i); } } return initialNodesToDelete; } }
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 | import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; class Graph { private Map<Integer, Set<Integer>> adj; public Graph(int n) { adj = new LinkedHashMap<>(); for (int i = 1; i <= n; i++) { adj.put(i, new HashSet<>()); } } public void insertEdge(int a, int b) { adj.get(a).add(b); adj.get(b).add(a); } public int getDegree(int a) { return adj.get(a).size(); } public void removeNodes(Queue<Integer> nodesToRemove, int d) { while (!nodesToRemove.isEmpty()) { int node = nodesToRemove.poll(); if (adj.containsKey(node)) { for (int neighbour : adj.get(node)) { adj.get(neighbour).remove(node); if (getDegree(neighbour) < d) { nodesToRemove.add(neighbour); } } adj.remove(node); } } } public Set<Integer> getLargestComponent() { Map<Integer, Boolean> visited = new HashMap<>(); for (int node : adj.keySet()) { visited.put(node, false); } Set<Integer> largestComponent = new TreeSet<>(); for (int node : adj.keySet()) { Set<Integer> component = new TreeSet<>(); if (!visited.get(node)) { visit(node, visited, component); } if (component.size() > largestComponent.size()) { largestComponent = component; } } return largestComponent; } private void visit(int node, Map<Integer, Boolean> visited, Set<Integer> component) { visited.put(node, true); component.add(node); for (int neighbour : adj.get(node)) { if (!visited.get(neighbour)) { visit(neighbour, visited, component); } } } } public class mis { public static void main(String[] args) { Scanner SC = new Scanner(System.in); int n = SC.nextInt(); int m = SC.nextInt(); int d = SC.nextInt(); Graph G = new Graph(n); for (int i = 0; i < m; i++) { int a = SC.nextInt(); int b = SC.nextInt(); G.insertEdge(a, b); } Queue<Integer> nodesToRemove = getInitialNodesToDelete(G, n, d); G.removeNodes(nodesToRemove, d); Set<Integer> largestComponent = G.getLargestComponent(); if (largestComponent.isEmpty()) { System.out.println("NIE"); } else { System.out.println(largestComponent.size()); for (Integer node : largestComponent) { System.out.print(node + " "); } System.out.println(); } } private static Queue<Integer> getInitialNodesToDelete(Graph G, int n, int d) { Queue<Integer> initialNodesToDelete = new LinkedList<>(); for (int i = 1; i <= n; i++) { if (G.getDegree(i) < d) { initialNodesToDelete.add(i); } } return initialNodesToDelete; } } |