zoukankan      html  css  js  c++  java
  • CF w4d2 B. Road Construction

    A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities — roads cannot be constructed between these pairs of cities.

    Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.

    Input

    The first line consists of two integers n and m .

    Then m lines follow, each consisting of two integers a i and b i (1 ≤ a i, b i ≤ n, a i ≠ b i), which means that it is not possible to construct a road connecting cities a i and b i. Consider the cities are numbered from 1 to n.

    It is guaranteed that every pair of cities will appear at most once in the input.

    Output

    You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers a i and b i (1 ≤ a i, b i ≤ n, a i ≠ b i), which means that a road should be constructed between cities a i and b i.

    If there are several solutions, you may print any of them.

    Examples

    inputCopy
    4 1
    1 3
    outputCopy
    3
    1 2
    4 2
    2 3

    #include<bits/stdc++.h>
    using namespace std;
    int n,m,center,tmp1,tmp2;
    int f[1005];
    
    int main()
    {
    	cin>>n>>m;
    	for(int i=0;i<m;i++){
    		cin>>tmp1>>tmp2;
    		f[tmp1]++;
    		f[tmp2]++;
    	}
    	cout<<n-1<<endl;
    	for(int i=1;i<=n;i++){
    		if(!f[i]){
    			center=i;
    			break;
    		}
    	}
    	for(int i=1;i<=n;i++){
    		if(i==center)continue;
    		cout<<i<<" "<<center<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?
    李洪强iOS开发之FMDB线程安全的用法
    李洪强iOS开发之-FMDB的用法
    李洪强iOS开发之-sql数据库的使用
    iOS截屏功能
    李洪强和你一起学习前端之(6)css行高,盒模型,外边距
    iOS网络_优化请求性能
    iOS-ARC-环境下如何查看引用计数的变化
    远程桌面连接没有授权此用户进行远程登陆
    路由重发布
  • 原文地址:https://www.cnblogs.com/LiangYC1021/p/12977203.html
Copyright © 2011-2022 走看看