POJ - 1659 Frogs' Neighborhood Havel-Hakimi定理

题目链接

题意:给出每个点的度数,问是否有连边方案使得所有点的度数都满足条件。

思路:Havel-Hakimi定理:令 S = ( d 1 , . . . , d n ) {S=(d_{1},...,d_{n})} S=(d1,...,dn) 为有限多个非负整数组成的非递增的点的序列。
S S S 可简单图化当且仅当 S ′ = ( d 2 − 1 , d 3 − 1 , . . . , d d 1 + 1 − 1 , d d 1 + 2 , . . . , d n ) {S'=(d_{2}-1,d_{3}-1,...,d_{d_{1}+1}-1,d_{d_{1}+2},...,d_{n})} S=(d21,d31,...,dd1+11,dd1+2,...,dn) 只含有非负整数且是可简单图化的。

具体求解:
例如度数序列:
4 4 3 3 3 3 2 2 1 1 \begin{array}{|c|c|c|c|c|c|c|c|c|c|} \hline 4&4&3&3&3&3&2&2&1&1\\\\ \hline \end{array} 4433332211

  • 第一步:删除4,对于第2~5的值均减1
    3 2 2 2 3 2 2 1 1 \begin{array}{|c|c|c|c|c|c|c|c|c|} \hline 3&2&2&2&3&2&2&1&1\\\\ \hline \end{array} 322232211
  • 再排序
    3 3 2 2 2 2 2 1 1 \begin{array}{|c|c|c|c|c|c|c|c|c|} \hline 3&3&2&2&2&2&2&1&1\\\\ \hline \end{array} 332222211
  • 第二步:删除3,对于第2到4的值均减1
    2 1 1 2 2 2 1 1 \begin{array}{|c|c|c|c|c|c|c|c|} \hline 2&1&1&2&2&2&1&1\\\\ \hline \end{array} 21122211
  • 再排序
    2 2 2 2 1 1 1 1 \begin{array}{|c|c|c|c|c|c|c|c|} \hline 2&2&2&2&1&1&1&1\\\\ \hline \end{array} 22221111
    以此类推,若中途出现负数的值则说明这个序列不可简单图化,若最后结果全为0,则可以简单图化。
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int T;
int n;
struct node
{
    int id, x;
    bool operator<(const node &b)const
    {
        return x > b.x;
    }
} a[12];
int ma[12][12];
bool Havel_Hakimi()
{
    for (int i = 1; i < n; ++i)
    {
        sort(a + i, a + n + 1);
        if (a[i].x + i > n)
            return false;
        for (int j = i + 1; j <= i + a[i].x; ++j)
        {
            --a[j].x;
            if (a[j].x < 0)
                return false;
            ma[a[i].id][a[j].id] = ma[a[j].id][a[i].id] = 1;
        }
    }
    return true;
}
int main()
{
    cin >> T;
    while(T--)
    {
        cin >> n;
        memset(ma, 0, sizeof(ma));
        for (int i = 1; i <= n; ++i)
        {
            a[i].id = i;
            cin >> a[i].x;
        }
        if(Havel_Hakimi())
        {
            puts("YES");
            for (int i = 1; i <= n; ++i)
            {
                for (int j = 1; j <= n; ++j)
                    printf("%d ", ma[i][j]);
                putchar('\n');
            }
        }
        else
            puts("NO");
        putchar('\n');
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值