您的位置:首页 > 其它

uva 10720 Graph Construction

2015-01-23 20:58 274 查看
题意:给你一串度序列,问该序列是不是可图的。

思路: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: