Problem:
Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).

Solution in C (GCC 9.2.1) (judge verdict: Accepted)

#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

/* prototype */

static inline int get(void);
static inline void put(int c);
int32_t nextint(void);
int64_t nextlong(void);
uint32_t nextstr(char *s);
void printint(int32_t x);
void printlong(int64_t x);
void rsort_d32(void *base, const int count, const int size, const int index);
void rsort_r32(void *base, const int count, const int size, const int index);

typedef struct union_find{
	int *p_prev, *p_rank, *p_size;
	int (*root)(struct union_find *, int);
	void (*unite)(struct union_find *, int, int);
	int (*size)(struct union_find *, int);
} union_find;
void union_find_init(struct union_find *p, int n);

/* main */

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

union_find d;

int main(void){
	int n=nextint();
	int m=nextint();
	union_find_init(&d, n+1);
	for(int i=0; i<m; i++){
		int a=nextint();
		int b=nextint();
		d.unite(&d, a, b);
	}
	int ans=-1;
	for(int i=1; i<=n; i++){
		if(d.root(&d, i) == i) ans++;
	}
	printf("%d\n", ans);
}

/* definition */

static inline int get(void){ return getchar_unlocked(); }
static inline void put(int c){ putchar_unlocked(c); }

int32_t nextint(void){ char c=get(); while(c!='-'&&(c<'0'||'9'<c)) c=get(); bool s=false; if(c=='-'){s=true;c=get();} uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=get(); } return s?-x:x; }
int64_t nextlong(void){ char c=get(); while(c!='-'&&(c<'0'||'9'<c)) c=get(); int s=0; if(c=='-'){s=1;c=get();} uint64_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=get(); } return s?-x:x; }
uint32_t nextstr(char *s){ char c=get(); while(c==' '||c=='\n') c=get(); uint32_t len=0; while(c!=' '&&c!='\n'){ *s++=c; len++; c=get(); } *s='\0'; return len; }
void printint(int32_t x){ if(x<0){x=-x; putchar('-');}else if(x==0){putchar('0');} int d=0; char s[10]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar(s[i]); putchar('\n'); }
void printlong(int64_t x){ if(x<0){x=-x; putchar('-');}else if(x==0){putchar('0');} int d=0; char s[20]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar(s[i]); putchar('\n'); }

void rsort_d32(void *base, const int count, const int size, const int index){
	typedef int32_t rsort_t;
	rsort_t (*a)[size] = (rsort_t(*)[size]) base;
	rsort_t (*b)[size] = (rsort_t(*)[size]) malloc(count*size*sizeof(rsort_t));
	int c[0x100];
	for(int k=0; k<4; k++){
		memset(c, 0, sizeof(c)); for(int i=0; i<count; i++){ c[(a[i][index]>>(k<<3))&0xFF]++; } for(int i=1; i<0x100; i++) c[i]+=c[i-1]; for(int i=count-1; i>=0; i--){ int to=--c[(a[i][index]>>(k<<3))&0xFF]; for(int j=0; j<size; j++){ b[to][j]=a[i][j]; } }{rsort_t(*t)[size]=a; a=b; b=t;}
	}
	free(b);
}
void rsortr_d32(void *base, const int count, const int size, const int index){
	typedef int32_t rsort_t;
	rsort_t (*a)[size] = (rsort_t(*)[size]) base;
	rsort_t (*b)[size] = (rsort_t(*)[size]) malloc(count*size*sizeof(rsort_t));
	int c[0x100];
	for(int k=0; k<4; k++){
		memset(c, 0, sizeof(c)); for(int i=0; i<count; i++){ c[(a[i][index]>>(k<<3))&0xFF]++; } for(int i=0xFE; i>=0; i--) c[i]+=c[i+1]; for(int i=count-1; i>=0; i--){ int to=--c[(a[i][index]>>(k<<3))&0xFF]; for(int j=0; j<size; j++){ b[to][j]=a[i][j]; } }{rsort_t(*t)[size]=a; a=b; b=t;}
	}
	free(b);
}

int union_find_root(struct union_find *p, int x){ return p->p_prev[x]==x?x:(p->p_prev[x]=union_find_root(p, p->p_prev[x])); }
int union_find_size(struct union_find *p, int x){ return p->p_size[union_find_root(p, x)]; }
void union_find_unite(struct union_find *p, int x, int y){
	x=union_find_root(p, x), y=union_find_root(p, y); if(x==y) return;
	if(p->p_rank[x]<p->p_rank[y]){
		p->p_prev[x]=y; p->p_size[y]+=p->p_size[x];
	}else if(p->p_rank[x]>p->p_rank[y]){
		p->p_prev[y]=x; p->p_size[x]+=p->p_size[y];
	}else{
		p->p_prev[x]=y; p->p_rank[y]++; p->p_size[y]+=p->p_size[x];
	}
}
void union_find_init(struct union_find *p, int n){
	p->p_prev = malloc(n*sizeof(int));
	p->p_rank = malloc(n*sizeof(int));
	p->p_size = malloc(n*sizeof(int));
	p->root = union_find_root;
	p->unite = union_find_unite;
	p->size = union_find_size;
	for(int i=0; i<n; i++){ p->p_prev[i]=i; p->p_rank[i]=1; p->p_size[i]=1; }
}