CodeForces-329C(div1):Graph Reconstruction(随机&构造)
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 ≤ n; u ≠ 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
8 7
1 2
2 3
4 5
5 6
6 8
8 7
7 4
1 4
4 6
1 6
2 7
7 5
8 5
2 8
3 2
1 2
2 3
-1
5 4
1 2
2 3
3 4
4 1
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(随机&构造)的更多相关文章
- Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化
C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- zoj3732&& hdu4797 Graph Reconstruction
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- codeforces 407 div1 A题(Functions again)
codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...
- 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction
题目链接 Graph Reconstruction 题意 给你无向图每个点的度数, 问是否存在唯一解, 存在输出唯一解, 多解输出两个, 无解输出IMPOSSIBLE 思路 这里用到了 Havel-H ...
- 利用程序随机构造N个已解答的数独棋盘
高级软件工程第二次作业:利用程序随机构造N个已解答的数独棋盘,代码如下: package SudokuGame; /** * 解决这个问题使用的是回溯+剪枝的算法 * 基本思想:不断地将每个格子可填入 ...
- 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 ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
随机推荐
- Nginx绑定多个域名的方法
nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里. 一.每个域名一个 ...
- javascript的slice(),splice(),split(),substring(),substr()
例子摘抄于http://www.w3school.com.cn/jsref/jsref_obj_array.asp 1.slice(): Array和String对象都有 在Array中 slice ...
- Pyqt4 360界面风格皮肤实现
前言 最近用Pyqt做了软件界面,始终觉得windows风格不太好看,虽然数字公司的行为有争议,但是也不影响我欣赏360卫士的界面风格. 声明 首先声明,此项工作并非原创,而是基于这位zhuyeqin ...
- Android Handler 异步消息处理机制的妙用 创建强大的图片载入类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 近期创建了一个群.方便大家交流,群号: ...
- A和B是好友,他们经常在空闲时间聊天,A的空闲时间为[a1 ,b1 ],[a2 ,b2 ]..[ap ,bp ]。B的空闲时间是[c1 +t,d1 +t]..[cq +t,dq +t],这里t为B的起床时间。这些时间包括了边界点。B的起床时间为[l,r]的一个时刻。若一个起床时间能使两人在任意时刻聊天,那么这个时间就是合适的,问有多少个合适的起床时间?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- 黑名单机制来临,你的应用还好么?Android P DP2最新兼容性报告出炉
5月9日,谷歌面向全球开发者发布了 Android P Beta,即 Android P DP2. 华为终端开放实验室第一时间对TOP1000主流应用兼容性进行测试,那么在版本兼容性方面各主流应用有何 ...
- vue-strap 修改Modal组件
在用到vue-strap的Modal组件时,会有两个默认按钮,查看官方文档配置如下: 可以看到,ok-text和cancel-text都有一个默认值,在使用时即使不给这两个选项赋值,也会显示两个默认文 ...
- OpenCV 中的三大数据类型:IplImage 类型
前言 本文将介绍 OpenCV 中的图像结构 IplImage 并提供一些很实用的技巧. 更多的矩阵处理函数还请参阅相关资料. IplImage 的类型定义 typedef struct _IplIm ...
- 【C语言】一句printf代码——{ a[0] ? 0[a] }
这是前段时间做的http://fun.coolshell.cn/中的一道题,很有意思,涉及的其实是C的基础,不过当时第一次看见这行代码确实把我弄懵了: printf(&unix["\ ...
- Unity3D研究院之Inspector视图中的get/set使用
get set 使用起来很方便,但是编辑时在Inspector视图中问题就来了,因为get/set的属性即使是public了,但是在Inspector视图中依然不显示..谷歌一下估计就是下面这样的答案 ...