题目链接:

D. Bear and Two Paths

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.

Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally:

  • There is no road between a and b.
  • There exists a sequence (path) of n distinct cities v1, v2, ..., vn that v1 = avn = b and there is a road between vi and vi + 1 for .

On the other day, the similar thing happened. Limak wanted to travel between a city c and a city d. There is no road between them but there exists a sequence of n distinct cities u1, u2, ..., un that u1 = cun = d and there is a road between ui and ui + 1 for .

Also, Limak thinks that there are at most k roads in Bearland. He wonders whether he remembers everything correctly.

Given nk and four distinct cities abcd, can you find possible paths (v1, ..., vn) and (u1, ..., un) to satisfy all the given conditions? Find any solution or print -1 if it's impossible.

Input
 

The first line of the input contains two integers n and k (4 ≤ n ≤ 1000, n - 1 ≤ k ≤ 2n - 2) — the number of cities and the maximum allowed number of roads, respectively.

The second line contains four distinct integers abc and d (1 ≤ a, b, c, d ≤ n).

Output
 

Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain n distinct integers v1, v2, ..., vn where v1 = a and vn = b. The second line should contain n distinct integers u1, u2, ..., unwhere u1 = c and un = d.

Two paths generate at most 2n - 2 roads: (v1, v2), (v2, v3), ..., (vn - 1, vn), (u1, u2), (u2, u3), ..., (un - 1, un). Your answer will be considered wrong if contains more than k distinct roads or any other condition breaks. Note that (x, y) and (y, x) are the same road.

Examples
 
input
7 11
2 4 7 3
output
2 7 1 3 6 5 4
7 1 5 4 6 2 3
input
1000 999
10 20 30 40
output
-1

题意:

给出n个节点,然后给出两条路线的起点和终点,要求你构造一个无向图,使无向图中a,b之间和c,d之间均无直接相连的边,且要求这个图的边的条数不超过k;

思路:

发现n==4时怎么都不可能满足;
可以构造这样的无向图
第一条路线ac...db; 第二条路线ca...bd;
这样的边是n+1条,是最少的了; AC代码
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,k,a,b,c,d;
int vis[];
int main()
{
scanf("%d%d",&n,&k);
scanf("%d%d%d%d",&a,&b,&c,&d);
if(k<n+||n==)cout<<"-1"<<"\n";
else
{
vis[a]=;
vis[b]=;
vis[c]=;
vis[d]=;
printf("%d %d ",a,c);
for(int i=;i<=n;i++)
{
if(!vis[i])
{
printf("%d ",i);
}
}
printf("%d %d\n",d,b);
printf("%d %d ",c,a);
for(int i=;i<=n;i++)
{
if(!vis[i])
{
printf("%d ",i);
}
}
printf("%d %d \n",b,d); } return ;
}

codeforces 673D D. Bear and Two Paths(构造)的更多相关文章

  1. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造

    D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...

  2. D. Bear and Two Paths(贪心构造)

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. VK Cup 2016 D. Bear and Two Paths 模拟

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  5. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

    题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...

  6. Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)

    <题目链接> 题目大意:给定一个无向图,该无向图不含自环,且无重边.现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于2.然后根输入边的顺序,输出构造的有向图.如果构造的边与输入 ...

  7. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  8. Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

    Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  9. Codeforces Round #339 (Div. 1) C. Necklace 构造题

    C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...

随机推荐

  1. Hello SpringMVC

    1. MVC框架能做哪些事情? 讲url映射到java类或者方法 封装用户提交的数据 处理请求-调用相关业务处理-封装相应数据 将相应数据进行渲染 jsp/html/freemaker等 ... 2. ...

  2. T3185 队列练习1 codevs

    http://codevs.cn/problem/3185/ 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素. 操作解释:1表示 ...

  3. rsync故障排查整理

    1.客户端的错误现象:No route to host rsync服务端开启了iptables防火墙 rsync -avz /etc/hosts rsync_backup@172.16.1.41::b ...

  4. 6.JAVA语言基础部分--数据库操作

    操作数据数据流程:得到Connecnt->获取Statement对象->执行sql语句返回ResultSet 1.通过DriverManager.getConnection("j ...

  5. Java重写父类使用@Override时出现The method destroy() of type xxx must override a superclass method的问题解决

    解决方法: 1.把JDK版本改成1.6以上的. 2.把Compiler改成1.6以上的. 关于这两者的区别,参考:http://www.cnblogs.com/EasonJim/p/6741682.h ...

  6. Java生成读取条形码和二维码图片

    原文:http://www.open-open.com/code/view/1453520722495 package zxing; import com.google.zxing.BarcodeFo ...

  7. pinpoint 应用性能管理工具安装部署

    原文:http://www.cnblogs.com/yyhh/p/6106472.html pinpoint 安装部署   阅读目录 1. 环境配置 1.1 获取需要的依赖包 1.2 配置jdk1.7 ...

  8. PHP网站渗透中的奇技淫巧:检查相等时的漏洞

    PHP是现在网站中最为常用的后端语言之一,是一种类型系统 动态.弱类型的面向对象式编程语言.可以嵌入HTML文本中,是目前最流行的web后端语言之一,并且可以和Web Server 如apache和n ...

  9. 如何快速的开发一个完整的iOS直播app(美颜篇)

    前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,美颜功能是很重要的,如果没有美颜功能,可能分分钟钟掉粉千万,本篇主要讲 ...

  10. BUPT复试专题—Python List(2014)

    题目描述 在Python中,List (列表)是一种非常重要的数据结构.它与C/C++/Java中的 数组有些类似,但支持添加新元素时的动态扩展.在这个问题中,你需要处理如下 的几种对List的操作. ...