codeforces 330 B. Road Construction (图论基础)
题目链接 题意:n个点,m(m<n/2)条不能走的边,问最少连多少条边,使得任何两个点之间的距离最多为2. 输出最少的边数和连接的哪些边。 思路: **数据范围很关键。**数据范围很关键。数据范围很关键。
因为m<n/2,每条禁止的边最多禁止两个点,所以禁止的点数<n..那么至少有一个点是和可以和其他所有点相连的。。于是把其他所有点和该点相连即可。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年03月19日 星期六 10时39分40秒
4File Name :code/cf/problem/330B.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E3+7;
34int cnt[N];
35int n,m;
36int main()
37{
38 #ifndef ONLINE_JUDGE
39 freopen("code/in.txt","r",stdin);
40 #endif
41
42 ms(cnt,0);
43 cin>>n>>m;
44 for ( int i = 1 ; i <= m ; i++) //因为m<n/2...一条m限制两个点不能相连。。最多限制2*m<n个。。。
45 { //所以至少有一个点不受任何限制。。把其他的所有点连到这个点上即可。
46 int u,v; //此时得到一个 star graph.
47 cin>>u>>v;
48 cnt[v]++;
49 cnt[u]++;
50 }
51
52 int p;
53 for ( int i = 1 ; i <= n ; i++)
54 {
55 if (cnt[i] == 0)
56 {
57 p = i ;
58 break;
59 }
60 }
61
62 cout<<n-1<<endl;
63 for ( int i = 1; i <= n ; i++)
64 {
65 if (i==p) continue;
66 cout<<p<<" "<<i<<endl;
67 }
68
69
70
71
72 #ifndef ONLINE_JUDGE
73 fclose(stdin);
74 #endif
75 return 0;
76}