import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { public static class UnionFind { int[] par; // public UnionFind(int n) { par = new int[n]; for (int i = 0; i < n; i++) { par[i] = -1; } } public int find(int x) { if (par[x] < 0) { return x; } else { return par[x] = find(par[x]); } } public boolean union(int x, int y) { x = find(x); y = find(y); if (x != y) { if (par[y] < par[x]) { // 多い方が根になるようにスワップする. int tmp = x; x = y; y = tmp; } par[x] += par[y]; par[y] = x; return true; } else { return false; } } public boolean same(int x, int y) { return find(x) == find(y); } public int size(int x) { return -par[find(x)]; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); UnionFind uf = new UnionFind(N); for(int i = 0; i < M; i++){ final int a = sc.nextInt() - 1; final int b = sc.nextInt() - 1; uf.union(a, b); } HashSet already = new HashSet(); TreeMap counts = new TreeMap(); for(int i = 0; i < N; i++){ if(already.contains(uf.find(i))){ continue; } already.add(uf.find(i)); if(!counts.containsKey(uf.size(i))){ counts.put(uf.size(i), 0); } counts.put(uf.size(i), counts.get(uf.size(i)) + 1); } int[] DP = new int[N + 1]; Arrays.fill(DP, -1); DP[0] = 0; for(final Entry entry : counts.entrySet()){ { final int size = entry.getKey(); //System.out.println(size); for(int pos = N - size; pos >= 0; pos--){ if(DP[pos] < 0){ continue; } if(DP[pos + size] < 0){ DP[pos + size] = DP[pos] + 1; } else{ DP[pos + size] = Math.min(DP[pos + size], DP[pos] + 1); } } DP[size] = 0; } int count = entry.getValue() - 1; for(int size_2 = 1; size_2 <= count; count -= size_2, size_2 *= 2){ final int size = entry.getKey() * size_2; //System.out.println(":" + size_2); for(int pos = N - size; pos >= 0; pos--){ if(DP[pos] < 0){ continue; } if(DP[pos + size] < 0){ DP[pos + size] = DP[pos] + 1; } else{ DP[pos + size] = Math.min(DP[pos + size], DP[pos] + 1); } } } { final int size = count * entry.getKey(); if(size == 0){ continue; } //System.out.println("::" + size); for(int pos = N - size; pos >= 0; pos--){ if(DP[pos] < 0){ continue; } if(DP[pos + size] < 0){ DP[pos + size] = DP[pos] + 1; } else{ DP[pos + size] = Math.min(DP[pos + size], DP[pos] + 1); } } } } for(int i = 1; i <= N; i++){ System.out.println(DP[i]); } } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } catch (IOException e) { /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { /* ignore */ } } } }