题意:给你一串度序列,问该序列是不是可图的。
思路:Havel-Hakimi定理。每次去掉一个度数最大的节点a[i],对其后的a[i]项减一,若度数出现负数或超出范围则是不可图的。
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[10005];
bool cmp(const int a, const int b){
return a > b;
}
bool input(){
cin>>n;
if(n == 0)
return false;
for(int i= 0; i < n; i++)
cin>>a[i];
return true;
}
bool check(){
for(int i = 0; i < n; i++){
sort(a+i,a+n,cmp);
int d = a[i];
if(i+d >= n)
return false;
for(int j = i+1; j <= i+d; j++){
a[j]--;
if(a[j] < 0)
return false;
}
}
return true;
}
int main(){
while(input()){
if(check())
cout<<"Possible\n";
else
cout<<"Not possible\n";
}
return 0;
}