HDU 2454 Degree Sequence of Graph G(Havel-Hakimi定理,图论基础)

题意:判断是否可以构成图,简单的Havel-Hakimi定理的运用)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1005;
struct node
{
    int degree;
    int index;
}v[maxm];
int cmp(node p,node q)
{
    return p.degree>q.degree;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&v[i].degree);
            v[i].index=i;
        }
        int ok=1;
        for(int k1=0;k1<n;k1++)
        {
            sort(v,v+n,cmp);
            int id=v[k1].degree;
            int i=v[k1].index;
            if(id>n-k1-1)
            {
                ok=0;
                break;
            }
            for(int k2=1;k2<=id;k2++)
            {
                int j=v[k2+k1].index;
                if(v[k2+k1].degree<=0)
                {
                    ok=0;
                    break;
                }
                v[k1+k2].degree--;
            }
            if(!ok)
            {
                break;
            }
        }
        if(ok)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

你可能感兴趣的:(HDU 2454 Degree Sequence of Graph G(Havel-Hakimi定理,图论基础))