I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.

I would like to create a new graph in such a way that:

  • The new graph consists of the same number of nodes and edges as the old graph.
  • The properties in the first paragraph still hold.
  • For each two nodes u and v, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.

Help me construct the new graph, or tell me if it is impossible.

Input

The first line consists of two space-separated integers: n and m (1 ≤ m ≤ n ≤ 105), denoting the number of nodes and edges, respectively. Then m lines follow. Each of the m lines consists of two space-separated integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting an edge between nodes u and v.

Output

If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly m lines. Each line should contain a description of edge in the same way as used in the input format.

Examples

Input
8 7
1 2
2 3
4 5
5 6
6 8
8 7
7 4
Output
1 4
4 6
1 6
2 7
7 5
8 5
2 8
Input
3 2
1 2
2 3
Output
-1
Input
5 4
1 2
2 3
3 4
4 1
Output
1 3
3 5
5 2
2 4

题意:给定N点M边的无向简单图,满足每个点最多度数为2。现在叫你重新构图,使得新图满足原来的性质,而且新图的边和原图的边没有相同的。

思路:因为度数最多为2,我们去随机构造一个链(最坏的情况下是个环),如果新图和就图没有交集,则ok。

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second
using namespace std;
const int maxn=;
map<pii,int>ex;
pii p[maxn];
int N,M,ans[maxn];
bool solve()
{
random_shuffle(ans+,ans+N+); ans[]=ans[N]; int cnt=;
for(int i=;i<=N;i++)
if(!ex[mp(ans[i-],ans[i])]) {
cnt++; p[cnt]=mp(ans[i-],ans[i]);
}
if(cnt<M) return false;
return true;
}
int main()
{
int T,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d",&u,&v);
ex[mp(u,v)]=; ex[mp(v,u)]=;
}
for(i=;i<=N;i++) ans[i]=i;
T=/M;
while(T--){
if(solve()) {
for(i=;i<=M;i++) printf("%d %d\n",p[i].f,p[i].s);
return ;
}
}
puts("-1");
return ;
}

CodeForces-329C(div1):Graph Reconstruction(随机&构造)的更多相关文章

  1. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  2. 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)

    Graph Reconstruction Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Let there ...

  3. zoj3732&& hdu4797 Graph Reconstruction

    Graph Reconstruction Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Let there ...

  4. codeforces 407 div1 B题(Weird journey)

    codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...

  5. codeforces 407 div1 A题(Functions again)

    codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...

  6. 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction

    题目链接 Graph Reconstruction 题意 给你无向图每个点的度数, 问是否存在唯一解, 存在输出唯一解, 多解输出两个, 无解输出IMPOSSIBLE 思路 这里用到了 Havel-H ...

  7. 利用程序随机构造N个已解答的数独棋盘

    高级软件工程第二次作业:利用程序随机构造N个已解答的数独棋盘,代码如下: package SudokuGame; /** * 解决这个问题使用的是回溯+剪枝的算法 * 基本思想:不断地将每个格子可填入 ...

  8. Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题

    E. Interesting Graph and Apples 题目连接: http://www.codeforces.com/contest/9/problem/E Description Hexa ...

  9. Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)

    题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...

随机推荐

  1. Android防止过快点击造成多次事件

    问题 onClick事件是Android开发中最常见的事件. 比方,一个submitButton.功能是点击之后会提交一个订单, 则一般代码例如以下,当中submitOrder()函数会跳转到下一页进 ...

  2. javascript onclick中post提交

    对post提交进行封装: function post(URL, PARAMS) { var temp = document.createElement("form"); temp. ...

  3. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  4. PHP基础函数手记

    PHP常用函数总结(180多个):http://www.jb51.net/article/101179.htm PHP常用函数归类总结[大全]:http://blog.csdn.net/ty_hf/a ...

  5. 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包

    [BZOJ2427][HAOI2010]软件安装 Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为 ...

  6. 【BZOJ1124】[POI2008]枪战Maf 贪心+思路题

    [BZOJ1124][POI2008]枪战Maf Description 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开 ...

  7. ADAS

    1 什么是ADAS advanced driver assistance system,即高级驾驶员辅助系统.是基于车上各种传感器的应用,如摄像头.雷达.激光器等. 2 ADAS的构成部分 2.1 a ...

  8. 经常遇到js的面试题

    大家都知道在面试的时候,很多前端的必须要问的就是js的问题,最近我们公司也有很多这样的面试,我提了一些个问题,还有我面试的时候面试官面试我的问题汇总,也有百度的别人的,希望对那些刚进入这个行业的有一些 ...

  9. Python爬虫-- PyQuery库

    PyQuery库 PyQuery库也是一个非常强大又灵活的网页解析库,PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪 ...

  10. ProgressBar+WebView实现自定义浏览器

    当我们使用浏览器浏览网页时,总会看到下图页面的样子,上面是一个地址栏,地址栏下面显示加载进度,加载完成后进入页面内容,带颜色的进度条总是少不了的,那样子看起来也舒服,如何实现自定义手机浏览器功能呢? ...