CodeForces - 330B Road Construction(水题)
2018-01-11 14:00
721 查看
题目链接:http://codeforces.com/problemset/problem/330/B点击打开链接
B. Road Construction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi),
which means that it is not possible to construct a road connecting cities ai and bi.
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 ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi),
which means that a road should be constructed between cities ai and bi.
If there are several solutions, you may print any of them.
Examples
input
output
Note
This is one possible solution of the example:
These are examples of wrong solutions:
The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3).
In addition, it also tries to construct a road between cities 1 and 3,
while the input specifies that it is not allowed to construct a road between the pair.
The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to
city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads.
Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3,
and 4 to 3.
给的m对点不能连在一起
然后让任意两点路最少
其实就是找一个点能连所有点
然而这个点不能是刚才出现的那些有不能连一起的点
#include <bits/stdc++.h>
using namespace std;
int vis[1111];
int main()
{
int n,m;
cin >> n >> m;
for(int i=0;i<m;i++)
{
int mid1,mid2;
cin >> mid1 >> mid2;
vis[mid1]=1;
vis[mid2]=1;
}
int tot;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
tot=i;
break;
}
}
cout << n-1 << endl;
for(int i=1;i<=n;i++)
{
if(i!=tot)
cout << tot << " " << i << endl;
}
}
B. Road Construction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi),
which means that it is not possible to construct a road connecting cities ai and bi.
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 ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi),
which means that a road should be constructed between cities ai and bi.
If there are several solutions, you may print any of them.
Examples
input
4 1 1 3
output
3 1 2 4 2 2 3
Note
This is one possible solution of the example:
These are examples of wrong solutions:
The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3).
In addition, it also tries to construct a road between cities 1 and 3,
while the input specifies that it is not allowed to construct a road between the pair.
The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to
city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads.
Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3,
and 4 to 3.
给的m对点不能连在一起
然后让任意两点路最少
其实就是找一个点能连所有点
然而这个点不能是刚才出现的那些有不能连一起的点
#include <bits/stdc++.h>
using namespace std;
int vis[1111];
int main()
{
int n,m;
cin >> n >> m;
for(int i=0;i<m;i++)
{
int mid1,mid2;
cin >> mid1 >> mid2;
vis[mid1]=1;
vis[mid2]=1;
}
int tot;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
tot=i;
break;
}
}
cout << n-1 << endl;
for(int i=1;i<=n;i++)
{
if(i!=tot)
cout << tot << " " << i << endl;
}
}
相关文章推荐
- Codeforces-----614A---Link/Cut Tree---数学水题
- CodeForces 919A Supermarket(水题)
- CodeForces 165A Supercentral Point(水题)
- codeforces 266A Stones on the Table(模拟水题)
- Codeforces 368B:Sereja and Suffixes(水题)
- CodeForces 706A Beru-taxi 【水题】
- 平常水题 - Codeforces 659D Bicycle Race(几何公式)
- CodeForces 602A Two Bases(水题)
- CodeForces 560A Currency System in Geraldion 【水题】
- codeforces 675A A. Infinite Sequence(水题)
- codeforces 677A A. Vanya and Fence(水题)
- codeforces水题100道 第一题 Codeforces Beta Round #1 A. Theatre Square (math)
- CodeForces - 808B Average Sleep Time(水题+规律)
- CodeForces 342A Xenia and Divisors (水题)
- CodeForces 474A Keyboard (水题)
- Codeforces 868 B Race Against Time(水题)
- CodeForces 352B 水题
- Bits - CodeForces 484 A 水题
- Codeforces 635A Orchestra 【水题】
- CodeForces 706A Beru-taxi (数学计算,水题)