include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
std::vector[HTML_REMOVED]pre(1000);
std::vector[HTML_REMOVED]H(10000);
int n, m;
int find_root ( int x ) {
return ( x == pre[x] ) ? x : find_root ( pre[x]);
}
void Merget ( int x , int y ) {
int x_root = find_root ( x ) , y_root = find_root(y );
// 属于 同一个 连通集
if ( x_root == y_root ) {
return;
}
if ( H[x_root] > H[y_root] ) {
pre[y_root] = x_root;
}
else if ( H[x_root] < H[y_root]) {
pre[x_root] = y_root;
}
else {
pre[y_root] = x_root;
H[x_root] ++;
}
}
int main ()
{
std::cin >> n >> m;
// 初始化 并查集
for ( int i = 0 ; i <= n ; i ){
pre[i] = i;
H[i] = 1;
}
for ( int i = 0 ; i < m ; i ) {
int a , b;
std::cin >> a >> b;
Merget ( a , b );
}
int cnt = 0 ; // 统计 连通分支的数目
for ( int i = 1 ; i <= n ; i ++ ) {
if ( pre[i] == i ) {
cnt ++;
std::cout << i << " " << pre[i] << std::endl;
}
}
std::cout << cnt -1 ;
system("pause");
return 0;
}