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 7 1 2 2 3 4 5 5 6 6 8 8 7 7 4
1 4 4 6 1 6 2 7 7 5 8 5 2 8
3 2 1 2 2 3
-1
5 4 1 2 2 3 3 4 4 1
1 3 3 5 5 2 2 4
给一幅图,图中任意结点的度数至多为2。
让你重构一幅图,要求:图中节点和原来一样,边数数量也一样,任意节点度数至多为2.
解题思路:
#include <cstdio>
#include <algorithm>
#include <vector>
#define maxn 100005
using 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;
}