import java.util.*; /** * Created by kamil on 29.09.15. */ public class mis { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int cities_no = scanner.nextInt(); int routes_no = scanner.nextInt(); int d = scanner.nextInt(); Set<Integer>[] neighbors = new Set[cities_no]; for(int i = 0;i<routes_no;i++){ int from = scanner.nextInt()-1; int to = scanner.nextInt()-1; Set<Integer> n1 = neighbors[from]; if(n1==null){ n1 = new HashSet<>(); neighbors[from]=n1; } n1.add(to); Set<Integer> n2 = neighbors[to]; if(n2==null){ n2 = new HashSet<>(); neighbors[to] = n2; } n2.add(from); } Set<Integer> survived = new HashSet<>(); Set<Integer> rejected = new HashSet<>(); for(int i = 0 ; i<cities_no; i++){ survived.add(i); } int[] deg = new int[cities_no]; for(int i = 0 ; i <cities_no;i++){ if(neighbors[i]!=null) deg[i] = neighbors[i].size(); } while(true){ for(Integer city:survived){ if(deg[city]<d){ rejected.add(city); Set<Integer> nbs = neighbors[city]; if(nbs!=null){ for(Integer nb:nbs){ deg[nb]--; } } } } survived.removeAll(rejected); if(rejected.isEmpty()){ break; } else { rejected.clear(); } } if(survived.size()<=1){ System.out.println("NIE"); } else { Set<Integer> biggest_cluster = null; while(!survived.isEmpty()){ Set<Integer> cluster = new HashSet<>(); Set<Integer> to_visit = new HashSet<>(); to_visit.add(survived.iterator().next()); while(!to_visit.isEmpty()){ Set<Integer> visited = new HashSet<>(); Set<Integer> to_add = new HashSet<>(); for(Integer visiting:to_visit){ visited.add(visiting); for(Integer nb:neighbors[visiting]){ if(!cluster.contains(nb) && deg[nb]>=d){ to_add.add(nb); } } } to_visit.addAll(to_add); cluster.addAll(visited); to_visit.removeAll(visited); survived.removeAll(visited); } if(biggest_cluster==null){ biggest_cluster = cluster; } else if(biggest_cluster.size()<cluster.size()){ biggest_cluster = cluster; } if(biggest_cluster.size()>=survived.size()){ break; } } survived = biggest_cluster; Integer[] output = new Integer[survived.size()]; int j =0; for(Integer survivor:survived){ output[j] = survivor+1; j++; } Arrays.sort(output); System.out.println(output.length); //System.exit(0); for(int i = 0;i<output.length-1;i++){ System.out.print(output[i]); System.out.print(" "); } System.out.println(output[output.length - 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 129 130 131 132 | import java.util.*; /** * Created by kamil on 29.09.15. */ public class mis { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int cities_no = scanner.nextInt(); int routes_no = scanner.nextInt(); int d = scanner.nextInt(); Set<Integer>[] neighbors = new Set[cities_no]; for(int i = 0;i<routes_no;i++){ int from = scanner.nextInt()-1; int to = scanner.nextInt()-1; Set<Integer> n1 = neighbors[from]; if(n1==null){ n1 = new HashSet<>(); neighbors[from]=n1; } n1.add(to); Set<Integer> n2 = neighbors[to]; if(n2==null){ n2 = new HashSet<>(); neighbors[to] = n2; } n2.add(from); } Set<Integer> survived = new HashSet<>(); Set<Integer> rejected = new HashSet<>(); for(int i = 0 ; i<cities_no; i++){ survived.add(i); } int[] deg = new int[cities_no]; for(int i = 0 ; i <cities_no;i++){ if(neighbors[i]!=null) deg[i] = neighbors[i].size(); } while(true){ for(Integer city:survived){ if(deg[city]<d){ rejected.add(city); Set<Integer> nbs = neighbors[city]; if(nbs!=null){ for(Integer nb:nbs){ deg[nb]--; } } } } survived.removeAll(rejected); if(rejected.isEmpty()){ break; } else { rejected.clear(); } } if(survived.size()<=1){ System.out.println("NIE"); } else { Set<Integer> biggest_cluster = null; while(!survived.isEmpty()){ Set<Integer> cluster = new HashSet<>(); Set<Integer> to_visit = new HashSet<>(); to_visit.add(survived.iterator().next()); while(!to_visit.isEmpty()){ Set<Integer> visited = new HashSet<>(); Set<Integer> to_add = new HashSet<>(); for(Integer visiting:to_visit){ visited.add(visiting); for(Integer nb:neighbors[visiting]){ if(!cluster.contains(nb) && deg[nb]>=d){ to_add.add(nb); } } } to_visit.addAll(to_add); cluster.addAll(visited); to_visit.removeAll(visited); survived.removeAll(visited); } if(biggest_cluster==null){ biggest_cluster = cluster; } else if(biggest_cluster.size()<cluster.size()){ biggest_cluster = cluster; } if(biggest_cluster.size()>=survived.size()){ break; } } survived = biggest_cluster; Integer[] output = new Integer[survived.size()]; int j =0; for(Integer survivor:survived){ output[j] = survivor+1; j++; } Arrays.sort(output); System.out.println(output.length); //System.exit(0); for(int i = 0;i<output.length-1;i++){ System.out.print(output[i]); System.out.print(" "); } System.out.println(output[output.length - 1]); } } } |