两个结论
- 对于一个顶点数为n的完全图,度数集合为{n-1}
证明:
n完全图中每个顶点都与另外n-1个点有连边
即,n完全图中有n个度数为n-1的顶点
度数集合为{n-1}- 对于一个顶点数为n个顶点的图,取k∈[1,n],令[1,k]中所有顶点与图中所有顶点连边,则[1,k]中所有顶点度数为n-1,[k+1,n]中所有顶点度数为k。
证明:
1.对于顶点v∈[1,k]
v与图中除自身外所有n-1个顶点都有连边,度数为n-1.
2.对于顶点v∈[k+1,n]
v与[1,k]中所有顶点连边,度数为k
度数集合为{k,n-1}其中结论1是结论2的特例(取k = n)
题目分析
我们现在d[n]+1个顶点,需要将其构造成一个度数集为{d[1],d[2]…d[n]的无向图}
考虑结论2,我们令[ 1,d[1] ]中所有顶点与图上所有顶点连边,则可得[ 1, d[1] ]中所有顶点度数为d[n],剩下所有顶点度数为d[1]。我们将图中顶点划分为3个部分
1. [ 1, d[1] ]中所有顶点度数为d[n]。
2. [ d[1]+1, d[n-1]+1 ]中所有顶点度数为d[1]。
3. [ d[n-1]+2, d[n] ]中所有顶点度数为d[1]。 此时我们的度数集合为{d[1], d[n]}, 接下来只需要在第2部分中连边,使得度数集合中出现d[2],d[3]…d[n-1]即可。
问题转化为:构造一个顶点数为d[n-1]-d[1]+1的无向图,使其度数集为{d[2]-d[1],d[3]-d[1],…d[n-1]-d[1]},这是一个与原问题类似的规模更小的问题,可以重复上述操作。
核心代码
我们每次都将1d[1]中顶点与图上所有顶点连边,然后忽略1d[1]与d[n-1]+2~d[n]+1,在区间d[2]~d[n-1]+1上重复同样的操作。
//当前的最左端区间与最右端区间
int left = 1, right = n;
//记录边数
int cot = 0;
while (left <= right)
{
for (int i = d[left - 1] + 1; i <= d[left]; i++)
for (int j = i+1; j <= d[right] + 1; j++)
{
ans.push_back(edge(i,j));
cot++;
}
left++;
right--;
}完整代码
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <list>
#include <string.h>
using namespace std;
int d[310] = {};
struct edge
{
int u;
int v;
edge(int u, int v)
{
this->u = u;
this->v = v;
}
};
list<edge> ans;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> d[i];
int l = 1, r = n;
int cot = 0;
while (l <= r)
{
for (int i = d[l - 1] + 1; i <= d[l]; i++)
for (int j = i+1; j <= d[r] + 1; j++)
{
ans.push_back(edge(i,j));
cot++;
}
l++;
r--;
}
cout<<cot;
for(edge a : ans)
{
cout<<endl<<a.u<<' '<<a.v;
}
return 0;
}