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:

CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))_第1张图片

A possible new graph for the first example:

CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))_第2张图片

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))_第3张图片

A possible new graph for the third example:


随机化乱搞。。。

random_shuffle(a+1,a+1+n) 入门。。。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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=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,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+(12) 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<




你可能感兴趣的:(随机化,C++)