A9140 | Graph Reconstruction
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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<=10^{5}$ ), 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 exactly $m$ lines. Each line should contain a description of edge in the same way as used in the input format.
输入输出样例
输入 #1
8 7 1 2 2 3 4 5 5 6 6 8 8 7 7 4
输出 #1
1 4 4 6 1 6 2 7 7 5 8 5 2 8
输入 #2
3 2 1 2 2 3
输出 #2
-1
输入 #3
5 4 1 2 2 3 3 4 4 1
输出 #3
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:


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:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted