codeforces 330 B. Road Construction (图论基础)

题目链接 题意:n个点,m(m<n/2)条不能走的边,问最少连多少条边,使得任何两个点之间的距离最多为2. 输出最少的边数和连接的哪些边。 思路: **数据范围很关键。**数据范围很关键。数据范围很关键。

因为m<n/2,每条禁止的边最多禁止两个点,所以禁止的点数<n..那么至少有一个点是和可以和其他所有点相连的。。于是把其他所有点和该点相连即可。

/* ***********************************************
Author :111qqz
Created Time :2016年03月19日 星期六 10时39分40秒
File Name :code/cf/problem/330B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E3+7;
 7int cnt[N];
 8int n,m;
 9int main()
10{
11	#ifndef  ONLINE_JUDGE 
12	freopen("code/in.txt","r",stdin);
13  #endif
1	ms(cnt,0);
2	cin>>n>>m;
3	for ( int i = 1 ; i <= m ; i++) //因为m<n/2...一条m限制两个点不能相连。。最多限制2*m<n个。。。
4	{				//所以至少有一个点不受任何限制。。把其他的所有点连到这个点上即可。
5	    int u,v;                    //此时得到一个 star graph.
6	    cin>>u>>v;
7	    cnt[v]++;
8	    cnt[u]++;
9	}
1	int p;
2	for ( int i = 1 ; i <= n ; i++)
3	{
4	    if (cnt[i] == 0)
5	    {
6		p = i ;
7		break;
8	    }
9	}
1	cout<<n-1<<endl;
2	for ( int i = 1; i <= n ; i++)
3	{
4	    if (i==p) continue;
5	    cout<<p<<" "<<i<<endl;
6	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}