CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))

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 ≤ 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 ≤ nu ≠ 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 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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值