Codeforces Round #192 (Div. 2) E. Graph Reconstruction (随机算法)
来源:互联网 发布:程序员的统计学 编辑:程序博客网 时间:2024/05/29 08:40
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.
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 ≤ 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 exactlym lines. Each line should contain a description of edge in the same way as used in the input format.
8 71 22 34 55 66 88 77 4
1 44 61 62 77 58 52 8
3 21 22 3
-1
5 41 22 33 44 1
1 33 55 22 4
给一幅图,图中任意结点的度数至多为2。
让你重构一幅图,要求:图中节点和原来一样,边数数量也一样,任意节点度数至多为2.
解题思路:
#include <cstdio>#include <algorithm>#include <vector>#define maxn 100005using namespace std;int n,m,ans;int a[maxn];vector<int>v[maxn];void solve(){ int i,j,k,flag=0,ok,sz,tmp; for(i=1; i<=500; i++) // 随机500次 { ok=1; random_shuffle(a+1,a+n+1); for(j=1;j<=m;j++) // 检验排列是否满足条件 { sz=v[a[j]].size(); for(k=0;k<sz;k++) { if(j==n) tmp=1; // 要考虑m==n的情况 else tmp=j+1; if(v[a[j]][k]==a[tmp]) { ok=0; break ; } } } if(ok) { flag=1; for(j=1;j<=m;j++) { if(j==n) tmp=1; else tmp=j+1; printf("%d %d\n",a[j],a[tmp]); } break ; } } if(!flag) printf("-1\n");}int main(){ int i,j,l,r; while(~scanf("%d%d",&n,&m)) { for(i=1; i<=n; i++) { v[i].clear(); a[i]=i; } for(i=1; i<=m; i++) { scanf("%d%d",&l,&r); v[l].push_back(r); v[r].push_back(l); } solve(); } return 0;}
- Codeforces Round #192 (Div. 2) E. Graph Reconstruction (随机算法)
- Codeforces Round #192 (Div. 1) C. Graph Reconstruction (随机化算法)
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph【DP】
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
- Codeforces 330E Graph Reconstruction【随机化算法】Get it!
- Codeforces Round #240 (Div. 2) E分治算法探究1
- Codeforces Round #340 (Div. 2) E题 莫队算法
- Codeforces Round #340 (Div. 2). E 莫队算法
- Codeforces Round #340 (Div. 2) E 莫队算法
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph (sorting + dp)
- Codeforces Round #266 (Div. 2)-E. Information Graph(dfs+并查集)
- Codeforces Round #237 (Div. 2)__Restore Graph
- Codeforces Round #423 (Div. 2) C. String Reconstruction(字符串)
- Codeforces Round #423 (Div. 2) C. String Reconstruction
- Codeforces Round #192 (Div. 2) 解题报告 //缺E
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- An ffmpeg and SDL Tutorial
- 云计算之路
- 自定义类加载器
- NALU(NAL单元)的顺序要求
- C# SortedDictionary<TKey,TValue>排序 用法 Sort()用法
- Codeforces Round #192 (Div. 2) E. Graph Reconstruction (随机算法)
- hdu4602(快速幂)
- java第四节课堂笔记(类与对象,static和this)
- KVM虚拟机代码揭秘——QEMU代码结构分析
- Code Fragment-提供可选选项的接口胜于单一接口
- Unity 异步加载场景
- python的文件操作file:(内置函数,如seek、truncate函数)
- N皇后问题
- VB中的类模块