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. 高能天气——团队Scrum冲刺阶段-Day 2

    高能天气--团队Scrum冲刺阶段-Day 2 今日完成任务 于欣月:实现滑动界面视图,天气预报UI组件的初步优化 滑动界面 实现代码 //Activity中 //请求新选择城市的天气信息 navBu ...

  2. webpack+vue-cli中代理配置(proxyTable)

    在做vue的项目时,用到webpack打包工具,我们会发现本地开发开启的node服务地址请求接口的地址存在跨域问题.本地开启的服务地址是 http://localhost:8080 而服务器的地址是 ...

  3. IO读 BufferedReader+FileReader

    private static final String FILENAME = "c:\\temp\\in.txt"; public static void main(String[ ...

  4. Servlet接口、GenericServlet类、HttpServlet类

    Servlet是最顶层的接口,其提供的方法有: init(ServletConfig config):void // 初始化 getServletConfig():ServletConfig // 取 ...

  5. Bootstrap css-表格

    前言:整理的东西比较基础,有不足的地方欢迎大家批评指正! 1,Bootstrap基本的表格结构 源代码: <table class="table">   <cap ...

  6. code vs 1094 FBI树 2004年NOIP全国联赛普及组

    题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...

  7. 格式化p6spy的输出日志

    众所周知, p6spy打印出来的日志是一行很长很长的内容, 很不容易查看, 牛B的p6spy为什么就不能想hibernate那样有format_sql的功能? 竟然没有, 我只好自己动手写一个日志输出 ...

  8. [BZOJ1115][POI2009]石子游戏Kam解题报告|阶梯博弈

    有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必胜. 首先 ...

  9. spy++使用指南

    很多朋友都对窗口句柄比较迷糊,这篇短文就以spy++这个软件为主,介绍下窗体句柄和使用按键插件时,如果对这个句柄发送消息,即所谓的后台挂机.spy++这个软件来自VC++,装好VC后,就可以在工具中看 ...

  10. .Net Discovery 系列之六--深入浅出.Net实时编译机制(下)

    接上文 在初始化时,HashTable中各个方法指向的并不是对应的内存入口地址,而是一个JIT预编译代理,这个函数负责将方法编译为本地代码.注意,这里JIT还没有进行编译,只是建立了方法表! 下表(表 ...