POJ 1659 Frogs' Neighborhood Havel-Hakimi定理判断可图

1,Havel-Hakimi定理主要用来判定一个给定的序列是否是可图的。
2,首先介绍一下度序列:若把图 G 所有顶点的度数排成一个序列 S,则称 S 为图 G 的度序列。
3,一个非负整数组成的有限序列如果是某个无向图的序列,则称该序列是可图的。

4,判定过程:(1)按降序排序,进入步骤(2)。(2)将第[2,2+s[1]-1]全部减1,若出现负数则不可图,判定结束。若[2,2+s[1]-1]全部变为0,则可图,判定结束。将s[1]删除,跳至步骤(1)。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime>
#include <iomanip>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define _LL long long
#define ULL unsigned long long
#define LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007

/** I/O Accelerator Interface .. **/
#define g (c=getchar())
#define d isdigit(g)
#define p x=x*10+c-'0'
#define n x=x*10+'0'-c
#define pp l/=10,p
#define nn l/=10,n
template<class T> inline T& RD(T &x)
{
    char c;
    while(!d);
    x=c-'0';
    while(d)p;
    return x;
}
template<class T> inline T& RDD(T &x)
{
    char c;
    while(g,c!='-'&&!isdigit(c));
    if (c=='-')
    {
        x='0'-g;
        while(d)n;
    }
    else
    {
        x=c-'0';
        while(d)p;
    }
    return x;
}
inline double& RF(double &x)      //scanf("%lf", &x);
{
    char c;
    while(g,c!='-'&&c!='.'&&!isdigit(c));
    if(c=='-')if(g=='.')
        {
            x=0;
            double l=1;
            while(d)nn;
            x*=l;
        }
        else
        {
            x='0'-c;
            while(d)n;
            if(c=='.')
            {
                double l=1;
                while(d)nn;
                x*=l;
            }
        }
    else if(c=='.')
    {
        x=0;
        double l=1;
        while(d)pp;
        x*=l;
    }
    else
    {
        x=c-'0';
        while(d)p;
        if(c=='.')
        {
            double l=1;
            while(d)pp;
            x*=l;
        }
    }
    return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
using namespace std;

struct N
{
    int ans,id;
}st[20];

int mark[20][20];

bool cmp(N n1,N n2)
{
    return n1.ans > n2.ans;
}

int Solve()
{
    int n,i,j;

    scanf("%d",&n);

    memset(mark,0,sizeof(mark));

    for(i = 1;i <= n; ++i)
        scanf("%d",&st[i].ans),st[i].id = i;
    for(i = 1;i <= n; ++i)
    {
        sort(st+i,st+n+1,cmp);
        for(j = i+1;j <= n && st[i].ans; ++j)
        {
            st[j].ans--;
            st[i].ans--;
            mark[st[i].id][st[j].id] = 1;
            mark[st[j].id][st[i].id] = 1;
            if(st[j].ans == -1)
                return puts("NO"),0;
        }
    }

    puts("YES");

    for(i = 1;i <= n; ++i)
        for(j = 1;j <= n; ++j)
            printf("%d%c",mark[i][j],(j == n ? '\n' : ' '));
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Solve();
        if(T)
            puts("");
    }
    return 0;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值