UVA 10720 (图论,度序列的可图性)

题意:
给出一个图的顶点数和各个顶点的度,问是否有一个简单图满足这个度序列。

思路:
度序列的可图性。把度数从大到小排列,从头开始取点,设这个点的度数为x,那么其后的x个点度数全部减一。当有点的度数为负数或者后面不足x个点时,不可图。
因为是简单图,所以一个顶点的度数最多为n-1,用一个桶记录度数出现的次数在做能降低度数重复情况时的复杂度。
这题因为数据问题好像O(n^2logn)都可以过。还有一种O(nlogn)的方法是,先排序,然后后面全部减一的动作通过树状数组来做,在相同数字时,二分找到这个数字下标最大的位置,从这个下标大的位置开始删除,这样可以保证序列依然有序。

代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll mod=998244353;
const double eps=1e-8;
const int maxn=20005;
const int maxm=10005;

int T;
int n,m;
int a[maxn];
int cnta[maxn];
void solve() {
    while(~sd(n)) {
        if(n==0)break;
        mst(cnta,0);
        int mx=0;
        for(int i=1; i<=n; i++) {
            sd(a[i]);
            cnta[a[i]]++;
            mx=max(a[i],mx);
        }

        bool ok=1;
        for(int i=mx;i>=1;i--){
            if(i==1&&cnta[i]%2){
                ok=0;
                continue;
            }
            if(cnta[i]){
                int now=i;
                cnta[i]--;
                for(int j=i;j>=1;j--){
                    if(now-cnta[j]>0){
                        now-=cnta[j];
                    }else{
                        cnta[j-1]+=now;
                        cnta[j]-=now;
                        now=0;
                        for(int k=j+1;k<=i;k++){
                            cnta[k-1]+=cnta[k];
                            cnta[k]=0;
                        }
                        break;
                    }
                }
                if(now){
                    ok=0;
                    break;
                }
            }
        }

        if(ok) {
            printf("Possible\n");
        } else {
            printf("Not possible\n");
        }
    }
    return ;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#else
    //    freopen("","r",stdin);
    //    freopen("","w",stdout);
#endif
    solve();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值