import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; public class mis { public static class City { public int neighnum = 0; public ArrayList<City> neighbours = new ArrayList<>(); boolean alive = true; // int citynumber; // public City(int num) { // citynumber = num; // } } public static class CityComp implements Comparator<City> { @Override public int compare(City str1, City str2) { return str1.neighnum > str2.neighnum ? 1 : str1.neighnum == str2.neighnum ? 0 : -1; } } public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int d = Integer.parseInt(tokenizer.nextToken()); ArrayList<City> cities = new ArrayList<>(); for(int i=0;i<n;++i) { City lol = new City(); cities.add(lol); } int ac, bc; for(int i =0;i<m;++i) { tokenizer = new StringTokenizer(in.readLine()); ac = Integer.parseInt(tokenizer.nextToken()); bc = Integer.parseInt(tokenizer.nextToken()); --ac; --bc; cities.get(ac).neighbours.add(cities.get(bc)); cities.get(bc).neighbours.add(cities.get(ac)); cities.get(ac).neighnum++; cities.get(bc).neighnum++; } TreeSet<City> cityset = new TreeSet<>(new CityComp()); cityset.addAll(cities); while(!cityset.isEmpty()) { City testcity = cityset.pollFirst(); if(! testcity.alive) continue; if(testcity.neighnum < d) { testcity.alive =false; for(City ziomek : testcity.neighbours) { if(!ziomek.alive) continue; cityset.remove(ziomek); ziomek.neighnum--; cityset.add(ziomek); } } } boolean fail = true; int citynum = 0; for(int i =0; i<n;++i) { City elo = cities.get(i); if(elo.alive) { citynum++; } } if(citynum==0) { out.print("NIE"); } else { out.print(citynum+"\n"); for(int i =0; i<n;++i) { City elo = cities.get(i); if(elo.alive) { out.print(i+1 + " "); } } } in.close(); out.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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; public class mis { public static class City { public int neighnum = 0; public ArrayList<City> neighbours = new ArrayList<>(); boolean alive = true; // int citynumber; // public City(int num) { // citynumber = num; // } } public static class CityComp implements Comparator<City> { @Override public int compare(City str1, City str2) { return str1.neighnum > str2.neighnum ? 1 : str1.neighnum == str2.neighnum ? 0 : -1; } } public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int d = Integer.parseInt(tokenizer.nextToken()); ArrayList<City> cities = new ArrayList<>(); for(int i=0;i<n;++i) { City lol = new City(); cities.add(lol); } int ac, bc; for(int i =0;i<m;++i) { tokenizer = new StringTokenizer(in.readLine()); ac = Integer.parseInt(tokenizer.nextToken()); bc = Integer.parseInt(tokenizer.nextToken()); --ac; --bc; cities.get(ac).neighbours.add(cities.get(bc)); cities.get(bc).neighbours.add(cities.get(ac)); cities.get(ac).neighnum++; cities.get(bc).neighnum++; } TreeSet<City> cityset = new TreeSet<>(new CityComp()); cityset.addAll(cities); while(!cityset.isEmpty()) { City testcity = cityset.pollFirst(); if(! testcity.alive) continue; if(testcity.neighnum < d) { testcity.alive =false; for(City ziomek : testcity.neighbours) { if(!ziomek.alive) continue; cityset.remove(ziomek); ziomek.neighnum--; cityset.add(ziomek); } } } boolean fail = true; int citynum = 0; for(int i =0; i<n;++i) { City elo = cities.get(i); if(elo.alive) { citynum++; } } if(citynum==0) { out.print("NIE"); } else { out.print(citynum+"\n"); for(int i =0; i<n;++i) { City elo = cities.get(i); if(elo.alive) { out.print(i+1 + " "); } } } in.close(); out.close(); } } |