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 现在,请构造一个这样的无向图, ...
随机推荐
- TimeSpan时间间隔
一个TimeSpan对象都表示一个时间间隔 (持续时间的时间或时间),单位为正数或负数的天数. 小时. 分钟. 秒和小数部分的第二个数字.TimeSpan结构还可以用于表示一天时间,但仅,如果与某一特 ...
- EMC机理------串扰
转:电子工程师不得不知道的EMC机理------串扰(韬略科技EMC) 串扰是信号完整性中最基本的现象之一,在板上走线密度很高时串扰的影响尤其严重.我们知道,线性无缘系统满足叠加定理,如果受害线上有信 ...
- OS开发之旅之App的生命周期【转载】
原文链接 http://www.360doc.com/content/15/0918/14/27799428_499912639.shtml 在iOS App中,入口函数并不在根目录下,而是在“Sup ...
- STM32 I2C
STM32 I2C 搞了几天了,比较郁闷,写点东西给那些正在郁闷的同志 // 好使的,也是范例的代码 cnt = TIME_OUT; while (cnt-- && !I2C_Chec ...
- 身份证归属地查询免费api接口代码
描写叙述 :依据身份证编号 查询归属地信息. 身份证实体类: package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @versio ...
- OpenSUSE 13.1上安装StrongSwan
结果: 1)iOS 7.1设备能够拨IPSec VPN到StrongSwan电脑上面来 - Connect to VPN 2)iOS 设备浏览器能够訪问StrongSwan VPN所在的内网地址服务器 ...
- java和erlang之间的DES加解密
app登录,登录的密码要用DES加密,服务器是用erlang,客户端要同时支持多平台(Android.iOS).首先,Java端的DES加密的实现方式, 少说废话了,直接上代码,如下: public ...
- 漫反射和Lambert模型
粗糙的物体表面向各个方向等强度地反射光,这种等同地向各个方向散射的现象称为光的漫反射(diffuse reflection).产生光的漫反射现象的物体表面称为理想漫反射体,也称为朗伯(Lambert) ...
- 开发及应用中 Linux与Window 取舍
Linux是开源的,而Windows不是.这个也是Linux与Windows之间最大的差异.一般来说,开源似乎收到了更多系统管理员的亲睐,而开源的软件似乎更受个人电脑用户的欢迎.两种类型之间有很多不同 ...
- Entity Framework 4.1:多对多的关系
这篇文章讨论多对多的关系. 让我们从最简单的例子开始.我们让 EF4.1 来推断表的映射.我在订单和雇员之间建模多对多的关系. )] publicstring CustomerName { get; ...