305813: CF1092E. Minimal Diameter Forest
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Minimal Diameter Forest
题意翻译
给定一张图(不一定联通),图中每一个联通块都是一棵树,我们称这样的图为“森林” 这张图有$n(\leq 1000)$个点,$m(\leq n-1)$条边,现在给这张图再添加$n-m-1$条边,使得这张图变成一棵树,且树的直径最小 要求输出最小的直径长度和连边方案 如果你不清楚树的直径是什么,下面给出“直径”的定义:树上所有简单路径中最长的那条叫“直径”题目描述
You are given a forest — an undirected graph with $ n $ vertices such that each its connected component is a tree. The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices. You task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible. If there are multiple correct answers, print any of them.输入输出格式
输入格式
The first line contains two integers $ n $ and $ m $ ( $ 1 \le n \le 1000 $ , $ 0 \le m \le n - 1 $ ) — the number of vertices of the graph and the number of edges, respectively. Each of the next $ m $ lines contains two integers $ v $ and $ u $ ( $ 1 \le v, u \le n $ , $ v \ne u $ ) — the descriptions of the edges. It is guaranteed that the given graph is a forest.
输出格式
In the first line print the diameter of the resulting tree. Each of the next $ (n - 1) - m $ lines should contain two integers $ v $ and $ u $ ( $ 1 \le v, u \le n $ , $ v \ne u $ ) — the descriptions of the added edges. The resulting graph should be a tree and its diameter should be minimal possible. For $ m = n - 1 $ no edges are added, thus the output consists of a single integer — diameter of the given tree. If there are multiple correct answers, print any of them.
输入输出样例
输入样例 #1
4 2
1 2
2 3
输出样例 #1
2
4 2
输入样例 #2
2 0
输出样例 #2
1
1 2
输入样例 #3
3 2
1 3
2 3
输出样例 #3
2