CF 329C (Graph Reconstruction-Randomization solution-random_shuffle (a + 1, a + 1 + n)

Source: Internet
Author: User

C. Graph Reconstruction
Time limit per test3 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard 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 wowould 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 ≤ memory m ≤ memory n ≤ memory less than or equal to memory 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 ≤ 0000u, 0000v ≤ 0000n; u ≤ 0000v ), 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 shoshould 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:

 

 

 

# 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 --) # define RepD (I, n) for (int I = n; I> = 0; I --) # define Forp (x) for (int P = pre [x]; 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 ()); # define MEMi (a) memset (a, 128, sizeof (a); # define INF (2139062143) # define F (100000007) # define MAXN (100000 + 10) # define MAXM (100000 + 10) long mul (long a, long B) {return (a * B) % F;} long add (long, long B) {return (a + B) % F;} long sub (lon G long a, long B) {return (a-B + (a-B)/F * F + F) % F;} typedef 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", & n, & m); For (I, m) {int u, v; scanf ("% d", & u, & v); h [make_pair (u, v)] = h [make_pair (v, u)] = 1 ;} for (I, n) h [make_pair (I, I)] = 1; // cout 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.