import java.util.*; import static java.util.Arrays.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.Integer.*; public class mis { ArrayList[]G; int n; int d; ArrayList<Integer> reduce(ArrayList<Integer> A) { HashMap<Integer,Integer>E = new HashMap<>(); Stack<Integer>toRemove = new Stack<>(); for(int x:A){ int v = G[x].size(); E.put(x, v); if(v<d)toRemove.add(x); } HashSet<Integer>removed = new HashSet<>(); int left = A.size(); while(!toRemove.isEmpty() && left >0) { int q = toRemove.pop(); left --; removed.add(q); for(int i=0;i<G[q].size();i++) { int p = (Integer)G[q].get(i); if(removed.contains(p)) continue; int v = E.get(p); E.put(p,v-1); if(v==d){ toRemove.push(p); } } } ArrayList ret = new ArrayList(); HashSet<Integer>U = new HashSet<>(); for(int x:A){ if(removed.contains(x) || U.contains(x)) continue; ArrayList<Integer>C = new ArrayList<>(); Stack<Integer> Q = new Stack<>(); Q.add(x); U.add(x); while(!Q.isEmpty()) { int p = Q.pop(); C.add(p); for(int i=0;i<G[p].size();i++){ int q = (Integer)G[p].get(i); if(!removed.contains(q) && !U.contains(q)){ Q.push(q); U.add(q); } } } if(C.size()>ret.size())ret=C; } return ret; } boolean validate(ArrayList<Integer> A) { if(A.size()==0)return true; HashSet<Integer>H = new HashSet<>(); for(int x:A)H.add(x); for(int x:A){ int md =0; for(int i=0;i<G[x].size();i++){ int p = (Integer)G[x].get(i); if(H.contains(p))md++; } if(md<this.d)throw new RuntimeException("error edges"); } int visited =0; HashSet<Integer>U = new HashSet<>(); Stack<Integer>Q = new Stack<Integer>(); Q.push(A.get(0)); while(!Q.isEmpty()) { int x = Q.pop(); U.add(x); for(int i=0;i<G[x].size();i++){ int p = (Integer)G[x].get(i); if(H.contains(p) && !U.contains(p)) Q.push(p); } } if(U.size() != A.size())throw new RuntimeException("Error connected"); return true; } void doit(ArrayList[]G, int d) { this.G = G; this.d = d; n = G.length; boolean[]U = new boolean[n]; ArrayList<Integer>best = new ArrayList<>(); for(int i=0;i<n;i++) { if(U[i])continue; ArrayList<Integer>con = new ArrayList(); U[i]=true; con.add(i); for(int j=0;j<con.size();j++){ int p = con.get(j); for(int k =0;k<G[p].size();k++){ int q = (Integer)G[p].get(k); if(!U[q]){ con.add(q); U[q]=true; } } } ArrayList r = reduce(con); // validate(r); if(r.size()>best.size())best = r; } if(best.size()==0) { System.out.println("NIE"); } else { Collections.sort(best); StringBuffer sb = new StringBuffer(); sb.append(best.size());sb.append('\n'); for(int x:best) { sb.append(x+1);sb.append(' '); } System.out.println(sb); } } public static void main(String[] args) throws Exception { long timeStart = System.currentTimeMillis(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //new BufferedReader(new FileReader("sample3.in")); String []s = br.readLine().split(" "); int n = parseInt(s[0]);int m = parseInt(s[1]);int d = parseInt(s[2]); ArrayList[]G = new ArrayList[n]; for(int i=0;i<n;i++){ G[i]= new ArrayList(); } for(int i=0;i<m;i++){ s = br.readLine().split(" "); int a = parseInt(s[0]) -1; int b = parseInt(s[1]) -1; G[a].add(b); G[b].add(a); } new mis().doit(G, d); System.err.println("Time: "+(System.currentTimeMillis()-timeStart)); } }
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 | import java.util.*; import static java.util.Arrays.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.Integer.*; public class mis { ArrayList[]G; int n; int d; ArrayList<Integer> reduce(ArrayList<Integer> A) { HashMap<Integer,Integer>E = new HashMap<>(); Stack<Integer>toRemove = new Stack<>(); for(int x:A){ int v = G[x].size(); E.put(x, v); if(v<d)toRemove.add(x); } HashSet<Integer>removed = new HashSet<>(); int left = A.size(); while(!toRemove.isEmpty() && left >0) { int q = toRemove.pop(); left --; removed.add(q); for(int i=0;i<G[q].size();i++) { int p = (Integer)G[q].get(i); if(removed.contains(p)) continue; int v = E.get(p); E.put(p,v-1); if(v==d){ toRemove.push(p); } } } ArrayList ret = new ArrayList(); HashSet<Integer>U = new HashSet<>(); for(int x:A){ if(removed.contains(x) || U.contains(x)) continue; ArrayList<Integer>C = new ArrayList<>(); Stack<Integer> Q = new Stack<>(); Q.add(x); U.add(x); while(!Q.isEmpty()) { int p = Q.pop(); C.add(p); for(int i=0;i<G[p].size();i++){ int q = (Integer)G[p].get(i); if(!removed.contains(q) && !U.contains(q)){ Q.push(q); U.add(q); } } } if(C.size()>ret.size())ret=C; } return ret; } boolean validate(ArrayList<Integer> A) { if(A.size()==0)return true; HashSet<Integer>H = new HashSet<>(); for(int x:A)H.add(x); for(int x:A){ int md =0; for(int i=0;i<G[x].size();i++){ int p = (Integer)G[x].get(i); if(H.contains(p))md++; } if(md<this.d)throw new RuntimeException("error edges"); } int visited =0; HashSet<Integer>U = new HashSet<>(); Stack<Integer>Q = new Stack<Integer>(); Q.push(A.get(0)); while(!Q.isEmpty()) { int x = Q.pop(); U.add(x); for(int i=0;i<G[x].size();i++){ int p = (Integer)G[x].get(i); if(H.contains(p) && !U.contains(p)) Q.push(p); } } if(U.size() != A.size())throw new RuntimeException("Error connected"); return true; } void doit(ArrayList[]G, int d) { this.G = G; this.d = d; n = G.length; boolean[]U = new boolean[n]; ArrayList<Integer>best = new ArrayList<>(); for(int i=0;i<n;i++) { if(U[i])continue; ArrayList<Integer>con = new ArrayList(); U[i]=true; con.add(i); for(int j=0;j<con.size();j++){ int p = con.get(j); for(int k =0;k<G[p].size();k++){ int q = (Integer)G[p].get(k); if(!U[q]){ con.add(q); U[q]=true; } } } ArrayList r = reduce(con); // validate(r); if(r.size()>best.size())best = r; } if(best.size()==0) { System.out.println("NIE"); } else { Collections.sort(best); StringBuffer sb = new StringBuffer(); sb.append(best.size());sb.append('\n'); for(int x:best) { sb.append(x+1);sb.append(' '); } System.out.println(sb); } } public static void main(String[] args) throws Exception { long timeStart = System.currentTimeMillis(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //new BufferedReader(new FileReader("sample3.in")); String []s = br.readLine().split(" "); int n = parseInt(s[0]);int m = parseInt(s[1]);int d = parseInt(s[2]); ArrayList[]G = new ArrayList[n]; for(int i=0;i<n;i++){ G[i]= new ArrayList(); } for(int i=0;i<m;i++){ s = br.readLine().split(" "); int a = parseInt(s[0]) -1; int b = parseInt(s[1]) -1; G[a].add(b); G[b].add(a); } new mis().doit(G, d); System.err.println("Time: "+(System.currentTimeMillis()-timeStart)); } } |