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 ≤ 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:

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

A possible new graph for the first example:

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

In the second example, we cannot create any new graph.

The old graph of the third example:

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

A possible new graph for the third example:

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


随机化乱搞。。。

 

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;

}


 



 

你可能感兴趣的:(shuffle)