System Administrator(构造,图论)
2 seconds
256 megabytes
standard input
standard output
Bob got a job as a system administrator in X corporation. His first task was to connect n servers with the help of mtwo-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair of servers should have at most one direct connection. Y corporation, a business rival of X corporation, made Bob an offer that he couldn't refuse: Bob was asked to connect the servers in such a way, that when server with index v fails, the transmission of data between some other two servers becomes impossible, i.e. the system stops being connected. Help Bob connect the servers.
The first input line contains 3 space-separated integer numbers n, m, v (3 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ v ≤ n), n — amount of servers, m — amount of direct connections, v — index of the server that fails and leads to the failure of the whole system.
If it is impossible to connect the servers in the required way, output -1. Otherwise output m lines with 2 numbers each — description of all the direct connections in the system. Each direct connection is described by two numbers — indexes of two servers, linked by this direct connection. The servers are numbered from 1. If the answer is not unique, output any.
5 6 3
1 2
2 3
3 4
4 5
1 3
3 5
6 100 1
-1
思路:由题意,图为连通图,要使得去除v后,图中至少有两个点不能通信(即此时至少有两个连通分支),则v为割点。
要构造出v为割点的图很容易,将图的顶点集合V分为V1和V2两部分,其中V1包含v,而V2不包含v,且V2中的顶点不和{V1-v}中的任何顶点相邻,而V1和{V2+v}中的顶点又各自两两互通。例子如下(红色圈是v):

程序如何实现?将n个顶点划分为V1和V2两部分,从V1(|V1|>=2)中任选一个点作为v,然后从v出发向其他所有顶点各引一条边,如果不满m条边,就在V1和V2内部连边(V1和V2之间不能连边)直至图中已有m条边。
如何确定什么时候无解?这需要求出在顶点数为n的情况下边数m的最大值和最小值。由于图连通,易知m>=n-1。重点是求m的最大值。假设V1和V2各有k和(n-k)个点,V1内部最多k*(k-1) / 2条边,V2内部最多有 (n-k)*(n-k-1) / 2条边,而V2和v之间最多有n-k条边,则m<=k*(k-1) / 2+ (n-k)*(n-k-1) / 2 + n-k ,由一元二次方程知识知当k与(n+1)/2差的绝对值越大,m的最大值越大,于是k为1或n-1。所以m最大时,V1有n-1个点而V2有一个点。故m<=(n-1)*(n-2)/2+1.于是得n-1 <= m <= (n-1)*(n-2)/2+1.
最终思路:取相异两点v和u,v的意义如前,u是V2的唯一一个顶点,将v与所有其他顶点连边(自然包括与u连边),如果此时边数不足m,在V1内部连边直至边数为m
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std; const int SZ = ;
int n, m, v;
bool vis[SZ]; int main()
{
while(scanf("%d %d %d", &n, &m, &v) != EOF)
{
if(m < n - || m > ((n - ) * (n - )) / + n - )
puts("-1");
else if(n < )
{
puts("1 2");
}
else
{
int u = v - ;
if(v == ) u = ;
for(int i = ; i <= n; i++)
{
if(i != v)
printf("%d %d\n", i, v);
}
m -= (n - );
for(int i = ; i <= n && m; i++)
{
if(i == v || i == u) continue;
for(int j = i + ; j <= n && m; j++)
{
if(j == v || j == u) continue;
printf("%d %d\n", i , j);
m--;
}
}
}
}
return ;
}
System Administrator(构造,图论)的更多相关文章
- codeforces 22C System Administrator(构造水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud System Administrator Bob got a job as a s ...
- 解决方法:An error occurred on the server when processing the URL. Please contact the system administrator
在WINDOWS7或SERVER2008上安装了IIS7.5,调试ASP程序时出现以下错误: An error occurred on the server when processing the U ...
- Requirements of an SAP system administrator
Requirements of an SAP system administrator Have a "proper" attitude Protect and safeguard ...
- the account is currently locked out. The system administrator can unlock it.
今天遇到的问题比较有意思.首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.Data.SqlClient.SqlExcept ...
- 解决IIS7运行ASP提示错误:An error occurred on the server when processing the URL. Please contact the system administrator
原文:解决IIS7运行ASP提示错误:An error occurred on the server when processing the URL. Please contact the syste ...
- SQL点滴8—the account is currently locked out. The system administrator can unlock it.
原文:SQL点滴8-the account is currently locked out. The system administrator can unlock it. 今天遇到的问题比较有意思. ...
- The Windows account sa does not exist and cannot be provisioned as a SQL Server system administrator
今天遇到一个案例,在使用命令修改一个测试服务器(SQL Server 2014标准版)的服务器排序规则时,遇到了下面错误信息 (具体账号信息脱敏处理,随机生成一个账号密码) The Windows a ...
- cvsnt报错:Administrator: Switch to user failed due to configuration error. Contact your System Administrator
在安装CVSNT一开始用Administrator登录时总是报[login aborted]Switch to user failed due to configuration error. Cont ...
- Sql server 账号被锁住:"the account is currently locked out. The system administrator can unlock it."的解决办法(转载)
今天遇到的问题比较有意思.首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.Data.SqlClient.SqlExcept ...
随机推荐
- 搜索引擎Elasticsearch REST API学习
Elasticsearch为开发者提供了一套基于Http协议的Restful接口,只需要构造rest请求并解析请求返回的json即可实现访问Elasticsearch服务器.Elasticsearch ...
- 1104 文法产生这段C程序的推导过程
- 数组去重复及记录重复个数(以及遍历map的四种方法)
private static void check(String[] array) { // 字符串数组中,含有不重复的字符串有哪些?每一个重复的个数 Map<String,Integer> ...
- 双主双写、只备份某些表且要在建表ID自增
先展示下最终实现的配置 主1的配置(重要的,其他略) log-bin = mysql-bin #必须要有binlog auto_increment_offset = 1 #自增ID的初始值 auto_ ...
- Java多线程编程(学习笔记)
一.说明 周末抽空重新学习了下多线程,为了方便以后查阅,写下学习笔记. 有效利用多线程的关键是理解程序是并发执行而不是串行执行的.例如:程序中有两个子系统需要并发执行,这时候需要利用多线程编程. 通过 ...
- [计算机网络] DNS劫持和DNS污染
DNS劫持,指用户访问一个被标记的地址时,DNS服务器故意将此地址指向一个错误的IP地址的行为.范例就是收到各种推送广告等网站. DNS污染,指的是用户访问一个地址,国内的服务器(非DNS)监控到用户 ...
- 九度-题目1026:又一版 A+B
http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m ...
- Fiddler绕过前端直接和后台进行交互
测试需求:有一个功能,允许用钻石兑换金币,假设1钻石=1金币,前端控制一次至少兑换10个,最多100个,后台不做验证. 测试方案:输入10,也就是告诉前端我要兑换10个金币,等前端验证通过之后,截取要 ...
- 卸载iptables 小心了!!怎么关闭和卸载iptables
千万千万不要使用下面的命令卸载iptables yum remove iptables 这样操作会卸载掉很多系统必要的组件,那就开不了机了,链接不上了.切记切记. 如果想永远停用,使用以下命令即可: ...
- wpf下使用NotifyIcon
以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘 定义一个NotifyIcon成员 : Notify ...