您的位置:首页 > 其它

UVa 10720 - Graph Construction

2014-06-10 20:21 656 查看
传送门UVa 10720 - Graph Construction



题意:给出一个图中的各个点的度数,判断能不能构成一个 完全图。



参考了shuangde800的解题报告,学习了Havel
- Hakimi定理。


以下内容转载自Coddicted,简单地举例了定理的应用。那个图就是伪代码。


The Havel Hakimi Algorithm

The Havel Hakimi algorithm gives a systematic approach to answer the question of determining whether it is possible to construct a simple graph from a given degree sequence.
 
Take as input a degree sequence S and determine if that sequence is graphical
That is, can we produce a graph with that degree sequence?
 
Assume the degree sequence is S 



 

Example 1:

S = 4, 3, 3, 3, 1

Where n = 5 (no. of vertices)

Step 1. Degree of all vertices is less than or equal to n ( no.of vertices)

Step 2. Odd number vertices  are four.

Step 3. There is no degree less than zero.

Step 4. Remove ‘4’ from the sequence and subtract 1 from the remaining new sequence and arrange again in non-increasing order to get

S = 2,2,2,0

Step 5. Again remove ‘2 ‘ from the sequence and subtracting 1 from the remaining new sequence and arrange in non-increasing order we get

S=  1,1,0

Repeating the above step

S= 0,0

Step 6. Since all the deg remaining in the sequence is zero, the given sequence is graphical.

 

Example 2:

Consider the degree sequence: S = 7, 5, 5, 4, 4, 4, 4, 3

Where n = 8 (no. of vertices)

Step 1. Degree of all vertices is less than or equal to n ( no.of vertices)

Step 2. Odd number vertices  are four.

Step 3. There is no degree less than zero.

Step 4. Remove ‘7’ from the sequence and subtract 1 from the remaining new sequence and arrange again in non-increasing order to get

S = 4, 4, 3, 3, 3, 3, 2

Step 5. Now remove the first ‘4 ‘ from the sequence and subtract 1 from the remaining new sequence to get:

S = 3, 2, 2, 2, 3, 2

rearrange in non-increasing order to get:

S = 3, 3, 2, 2, 2, 2

Repeating the above step we get following degree sequences:

S = 2, 2, 2, 1, 1

S = 1, 1, 1, 1

S = 1, 1, 0

S = 0, 0

Step 6. Since all the deg remaining in the sequence is zero, the given sequence is graphical (or in other words, it is possible to construct a simple graph from the given degree sequence).

#include <cstdio>
#include <functional>
#include <algorithm>
using namespace std;
const int MAXN = 11000;

int num[MAXN], n;

bool Havel_Hakimi()
{
for (int i = 0; i < n - 1; i++)
{
sort(num + i, num + n, greater<int>());
for (int j = i + 1; j <= i + num[i]; j++)
{
num[j]--;
if (num[j] < 0)
return false;
}
}
if (num[n - 1] != 0)
return false;
return true;
}

int main()
{
//freopen("input.txt", "r", stdin);
int i, j, t, sum;
bool flag;
while (scanf("%d", &n), n)
{
flag = true;
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &num[i]);
if (num[i] >= n || num[i] < 0)
flag = false;
sum += num[i];
}
if (sum % 2 != 0)
flag = false;
if (sum == 0 || (flag && Havel_Hakimi()))
printf("Possible\n");
else
printf("Not possible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM UVa