CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))
3 seconds
256 megabytes
standard input
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.
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.
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.
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
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))的更多相关文章
- 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 ...
- 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction
题目链接 Graph Reconstruction 题意 给你无向图每个点的度数, 问是否存在唯一解, 存在输出唯一解, 多解输出两个, 无解输出IMPOSSIBLE 思路 这里用到了 Havel-H ...
- 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 ...
- CF 990D Graph And Its Complement 第十八 构造、思维
Graph And Its Complement time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- ZOJ3732 Graph Reconstruction Havel-Hakimi定理
分析: 给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化. 进一步,若图为简单图,则称此序列可简单图化 (来自百度百科) 可简单图化的判定可以用Have ...
- 2018省赛赛第一次训练题解和ac代码
第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title A CodeForces 607A Chain Reaction B CodeForces ...
- 论文解读(SEP)《Structural Entropy Guided Graph Hierarchical Pooling》
论文信息 论文标题:Structural Entropy Guided Graph Hierarchical Pooling论文作者:Junran Wu, Xueyuan Chen, Ke Xu, S ...
随机推荐
- Angular 个人深究(一)【Angular中的Typescript 装饰器】
Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...
- 使用ajax与jqplot的小体会
在使用ajax与jqplot时遇到了传值的问题!一开始都不知值是怎么传过去的,只找到了例子是以<div id="data">原始数据</div>这样子来接收 ...
- ftp服务及其实现之vsftpd
本文最早发布于我的51CTO博客,目前已迁移至博客园. 简介 FTP,File Transfer Protocol,文件传输协议 文件服务器,具备存储和共享文件(权限设置合理的情况下)的功能 命令端口 ...
- 深入理解指针—>结构体里的成员数组和指针
单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Laruence同学出了一个关于C语言的题,微博链接.微博截图如 ...
- 解决Mac java.net Local host name unknown error的方法
解决这个问题的方法: scutil --set HostName "localhost" 解决Mac java.net Local host name unknown error ...
- 用Visio画泳道图
在一次会议中看到有个同事在讲解业务流程时画了一个与PD中很类似的泳道图,但是在图的左侧确有一个阶段的列,事后与他沟通,才知道他这个图是”拼”出来的,也就是说所有的图都是他一点点的在画图工具中做出来的. ...
- 给Eclipse安装eUML2插件以及可能出现的依赖错误解决方案(转)
eUML2是一款强大的,基于Eclipse应用程序的UML建模工具.开发者可以在UML开发过程中将模型转化为Java代码:确保软件质量和减少开发时间. 必备条件 Java runtime 1.5 or ...
- .net core在Linux ARM板上运行
最近接了个临时任务,给别的项目组的机器人平台上开发个小程序,那机器人上跑的是ARM平台,ubuntu的系统. 本来打算用C++写的,由于最近用.net core较多,鉴于其在linux平台良好的兼容性 ...
- [Node.js]操作mysql
摘要 上篇文章介绍了node.js操作redis的简单实例,这里介绍如何操作mysql. 安装 安装mysql模块 cnpm install mysql 一个例子 新建一个mysql.js的文件,代码 ...
- 在树莓派2上安装 Windows 10
微软在2015年4月29日发布了树莓派玩家期待已久的 Windows 10 物联网核心预览版(Windows 10 IoT Core Insider Preview Image for Raspber ...