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.Scanner; import java.util.Set; import java.util.StringTokenizer; 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(); ArrayList connected = new ArrayList(); for(int i = 0; i < N; i++){ if(already.contains(uf.find(i))){ continue; } already.add(uf.find(i)); connected.add(uf.size(i)); } Collections.sort(connected); int[] DP = new int[N + 1]; Arrays.fill(DP, -1); DP[0] = 0; for(final int size : connected){ 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; } 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 */ } } } }