Havel-Hakimi定理
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 10010;
using namespace std;
int deg[maxn];
bool Cmp(int x,int y){ return x > y;}
bool Havel_Hakimi(int n){
for(int i=0; i<=n-1; ++i){
sort(deg+i,deg+n,Cmp);
if(i+deg[i] >= n) return false;
if(!deg[i]) return true;//剪枝
for(int j=i+1; j<=i+deg[i]; ++j){
--deg[j];
if(deg[j] < 0) return false;
}
}
if(deg[n-1] != 0) return false;
return true;
}
int main()
{
int n;
while(~scanf("%d",&n) && n){
bool flag = true;
int sum = 0;
for(int i= 0; i < n; ++i){
scanf("%d",°[i]);
sum += deg[i];
if(deg[i] > n-1) flag = false;
}
if(sum % 2 != 0) flag = false;
if(flag&&Havel_Hakimi(n))
printf("Possible\n");
else printf("Not possible\n");
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 10010;
using namespace std;
int deg[maxn];
bool Cmp(int x,int y){ return x > y;}
bool Havel_Hakimi(int n){
for(int i=0; i<=n-1; ++i){
sort(deg+i,deg+n,Cmp);
if(i+deg[i] >= n) return false;
if(!deg[i]) return true;//剪枝
for(int j=i+1; j<=i+deg[i]; ++j){
--deg[j];
if(deg[j] < 0) return false;
}
}
if(deg[n-1] != 0) return false;
return true;
}
int main()
{
int n;
while(~scanf("%d",&n) && n){
bool flag = true;
int sum = 0;
for(int i= 0; i < n; ++i){
scanf("%d",°[i]);
sum += deg[i];
if(deg[i] > n-1) flag = false;
}
if(sum % 2 != 0) flag = false;
if(flag&&Havel_Hakimi(n))
printf("Possible\n");
else printf("Not possible\n");
}
return 0;
}