import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; class Graph { class Vertex implements Comparable<Vertex>{ public int idx, label, degree, component; boolean active; public Vertex(int idx, int label) { this.idx = idx; this.label = label; this.degree = 0; this.component = -1; this.active = true; } public int deg() { return degree; } public boolean isActive() { return this.active; } public void deactivate() { //System.out.println("Removing "+label); this.active = false; } @Override public int compareTo(Vertex other) { int d = this.deg(); int od = other.deg(); if(d < od) { return -1; } else if (d > od) { return 1; } else { return 0; } } } int n,m; ArrayList<List<Integer>> edges; Vertex[] vertices; int color = 0; Map<Integer, List<Vertex>> components; public Graph(int n, int m) { this.n = n; this.m = m; vertices = new Vertex[n]; components = new HashMap<Integer, List<Vertex>>(); for(int i = 0; i < n; i++) { addVertex(new Vertex(i, i+1)); } this.edges = new ArrayList<List<Integer>>(); for(int i = 0; i < n; i++) { edges.add(new ArrayList<Integer>()); } } public void addVertex(Vertex v) { this.vertices[v.idx] = v; } public void addEdge(int fromIdx, int toIdx) { edges.get(fromIdx).add(toIdx); vertices[fromIdx].degree++; } public void removeSmallVertices(int degLimit) { List<Vertex> verts = new ArrayList<Vertex>(); for(Vertex v: vertices) { verts.add(v); } Collections.sort(verts); for(Vertex v: verts) { if (v.deg() < degLimit) { // remove v v.deactivate(); for(int uIdx: edges.get(v.idx)) { vertices[uIdx].degree--; } } } //rebuildEdges(); } private void addToComponent(int componentId, Vertex v) { if(!components.containsKey(componentId)) { components.put(componentId, new ArrayList<Vertex>()); } components.get(componentId).add(v); } private void dfsVisit(Vertex u) { List<Integer> adj = edges.get(u.idx); u.component = color; addToComponent(color, u); for(int uIdx: adj){ Vertex v = vertices[uIdx]; if (v.component < 0 && v.isActive()) { dfsVisit(v); } } } public void markComponents() { for(Vertex v: vertices) { if (v.isActive()) { dfsVisit(v); color++; } } } public List<Integer> getBiggestComponent() { List<Integer> results = new ArrayList<Integer>(); int mx = 0; int mxIdx = -1; for(Map.Entry<Integer, List<Vertex>> e: components.entrySet()) { int c = e.getKey(); List<Vertex> verts = e.getValue(); if (verts.size() > mx) { mx = verts.size(); mxIdx = c; } } if(mx > 0){ List<Vertex> cV = components.get(mxIdx); for(Vertex v : cV) { results.add(v.label); } } return results; } } 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,m); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); G.addEdge(a-1, b-1); G.addEdge(b-1, a-1); } G.removeSmallVertices(d); G.markComponents(); List<Integer> r = G.getBiggestComponent(); Collections.sort(r); if (r.size() > 0) { System.out.println(r.size()); for(int i : r){ System.out.print(i + " "); } System.out.println(); } else { System.out.println("NIE"); } sc.close(); } }
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; class Graph { class Vertex implements Comparable<Vertex>{ public int idx, label, degree, component; boolean active; public Vertex(int idx, int label) { this.idx = idx; this.label = label; this.degree = 0; this.component = -1; this.active = true; } public int deg() { return degree; } public boolean isActive() { return this.active; } public void deactivate() { //System.out.println("Removing "+label); this.active = false; } @Override public int compareTo(Vertex other) { int d = this.deg(); int od = other.deg(); if(d < od) { return -1; } else if (d > od) { return 1; } else { return 0; } } } int n,m; ArrayList<List<Integer>> edges; Vertex[] vertices; int color = 0; Map<Integer, List<Vertex>> components; public Graph(int n, int m) { this.n = n; this.m = m; vertices = new Vertex[n]; components = new HashMap<Integer, List<Vertex>>(); for(int i = 0; i < n; i++) { addVertex(new Vertex(i, i+1)); } this.edges = new ArrayList<List<Integer>>(); for(int i = 0; i < n; i++) { edges.add(new ArrayList<Integer>()); } } public void addVertex(Vertex v) { this.vertices[v.idx] = v; } public void addEdge(int fromIdx, int toIdx) { edges.get(fromIdx).add(toIdx); vertices[fromIdx].degree++; } public void removeSmallVertices(int degLimit) { List<Vertex> verts = new ArrayList<Vertex>(); for(Vertex v: vertices) { verts.add(v); } Collections.sort(verts); for(Vertex v: verts) { if (v.deg() < degLimit) { // remove v v.deactivate(); for(int uIdx: edges.get(v.idx)) { vertices[uIdx].degree--; } } } //rebuildEdges(); } private void addToComponent(int componentId, Vertex v) { if(!components.containsKey(componentId)) { components.put(componentId, new ArrayList<Vertex>()); } components.get(componentId).add(v); } private void dfsVisit(Vertex u) { List<Integer> adj = edges.get(u.idx); u.component = color; addToComponent(color, u); for(int uIdx: adj){ Vertex v = vertices[uIdx]; if (v.component < 0 && v.isActive()) { dfsVisit(v); } } } public void markComponents() { for(Vertex v: vertices) { if (v.isActive()) { dfsVisit(v); color++; } } } public List<Integer> getBiggestComponent() { List<Integer> results = new ArrayList<Integer>(); int mx = 0; int mxIdx = -1; for(Map.Entry<Integer, List<Vertex>> e: components.entrySet()) { int c = e.getKey(); List<Vertex> verts = e.getValue(); if (verts.size() > mx) { mx = verts.size(); mxIdx = c; } } if(mx > 0){ List<Vertex> cV = components.get(mxIdx); for(Vertex v : cV) { results.add(v.label); } } return results; } } 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,m); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); G.addEdge(a-1, b-1); G.addEdge(b-1, a-1); } G.removeSmallVertices(d); G.markComponents(); List<Integer> r = G.getBiggestComponent(); Collections.sort(r); if (r.size() > 0) { System.out.println(r.size()); for(int i : r){ System.out.print(i + " "); } System.out.println(); } else { System.out.println("NIE"); } sc.close(); } } |