C. Graph Reconstruction
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ mn ≤ 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, vnuv), 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 exactlym lines. Each line should contain a description of edge in the same way as used in the input format.

Sample test(s)
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
Note

The old graph of the first example:

A possible new graph for the first example:

In the second example, we cannot create any new graph.

The old graph of the third example:

A possible new graph for the third example:

随机化乱搞。。。

random_shuffle(a+1,a+1+n) 入门。。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<map>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,m;
map<pair<int,int>,bool> h;
int degree[MAXN]={0},ans[MAXN];
bool check()
{
For(i,m) if (h[make_pair(ans[i],ans[i+1])]) return 1;
For(i,n) degree[i]=0;
For(i,m+1) degree[ans[i]]+=1+(1<i&&i<m+1);
For(i,m+1) if (degree[ans[i]]>2) return 1;
if (m+1==3&&ans[1]==ans[3]) return 1;
return 0;
}
int main()
{
// freopen("Graph Reconstruction.in","r",stdin);
scanf("%d%d",&n,&m);
For(i,m) {int u,v;scanf("%d%d",&u,&v); h[make_pair(u,v)]=h[make_pair(v,u)]=1; }
For(i,n) h[make_pair(i,i)]=1;
//cout<<h[make_pair(8,7)]<<endl; For(i,n) ans[i]=i; int T=3000000/m;
while (T--)
{
random_shuffle(ans+1,ans+1+n);ans[0]=ans[n];//0..n 每个数出现一次,1个出现2次 的 rand_seq
int cnt=0;
Rep(i,n) if (!h[make_pair(ans[i],ans[i+1])]) cnt++;
if (cnt<m) continue; {
for(int i=0;m;i++) if (!h[make_pair(ans[i],ans[i+1])]) printf("%d %d\n",ans[i],ans[i+1]),m--;
return 0;
}
}
//puts("-1"); puts("-1");
return 0;
}

CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))的更多相关文章

  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. 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction

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

  5. CodeForces-329C(div1):Graph Reconstruction(随机&构造)

    I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two in ...

  6. CF 990D Graph And Its Complement 第十八 构造、思维

    Graph And Its Complement time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. ZOJ3732 Graph Reconstruction Havel-Hakimi定理

    分析: 给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化. 进一步,若图为简单图,则称此序列可简单图化 (来自百度百科) 可简单图化的判定可以用Have ...

  8. 2018省赛赛第一次训练题解和ac代码

    第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces ...

  9. 论文解读(SEP)《Structural Entropy Guided Graph Hierarchical Pooling》

    论文信息 论文标题:Structural Entropy Guided Graph Hierarchical Pooling论文作者:Junran Wu, Xueyuan Chen, Ke Xu, S ...

随机推荐

  1. 跟厂长学PHP7内核(八):深入理解字符串的实现

    在前面大致预览了常用变量的结构之后,我们今天来仔细的剖析一下字符串的具体实现. 一.字符串的结构 struct _zend_string { zend_refcounted_h gc; /* 字符串类 ...

  2. Linux-c系统编程

    进程相关的概念 程序和进程 程序:二进制文件.占用磁盘空间 进程:运行着的程序,数据在内存中,占用系统资源,CPU,物理内存() PCB描述进程(进程控制块) 把描述进程的所有信息的那条记录叫做 PC ...

  3. 5,EasyNetQ-Send Receive

    而发布/订阅和请求/响应模式是位置透明的,因为您不需要指定消息的消费者所在的位置,发送/接收模式专门用于通过命名队列进行通信. 它也不会对可以通过队列发送的消息的类型做任何假设. 这意味着您可以通过同 ...

  4. GRYZ 模 拟 赛 系 列 之 迷 宫(不就是个洪水)

    - 迷 宫 (maze.cpp/c/pas) Description Karles 和朋友到迷宫玩耍,没想到遇上了 10000000 年一次的大洪水,好在 Karles 是一个喜 欢思考的人,他发现迷 ...

  5. 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  6. centos7 打造基于python语言Selenium2自动化开发环境

    1. 准备 安装模块 # yum groupinstall "Development tools" # yum install zlib-devel bzip2-devel ope ...

  7. vuessr nuxt入门指南

    nuxt.js 官网地址:https://zh.nuxtjs.org/guide/installation 1.安装 vue init nuxt-community/starter-template ...

  8. css卷叶效果

    <!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8&qu ...

  9. Redis使用小结

    Redis官方没有windows版本的,对于Windows环境的redis,有如下两个方案 微软的移植版本,但只支持到3.2 下载地址 win10及以上的版本直接通过win10的liunx子系统执行 ...

  10. BoundingBoxUV与BoundingBoxXYZ

    start UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; ); ...