uva-10720-贪心

  题意:对于一个简单图(不存在平行边和自旋边),输入所有的点的度,问,能不能变成一个简单图.

解题思路:

可图化定理.https://blog.csdn.net/shuangde800/article/details/7857246

对所有点的度从大到小排序,每次都选取一个度最大的点(度为n),往后的n个点度数每个都减一,如果,发现出现负数,或者不够减,那就不能变成简单图.

WA了很多次,主要错在以下俩个原因,刚开始从小的开始删除.考虑下面这组输入,

4 1 1 2 2,如果从小的开始删,第一次删除后变成 0 0 2 2 ,这个一个错误的图.

第二次只排了一次.

第一次排序后 d1 >= d2 >= d3 >= ......dn,但是在删除一次后,不保证序列还是递减.

#include "pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>

namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::bitset;


    constexpr int N = 10010;

    int a[N];
    int n;
    bool check()
    {
        for (int i = 0;i < n - 1;i++)
        {
            sort(a + i, a + n, greater<int>());
            if (i + a[i] >= n) return false;
            for (int j = i + 1;j <= i + a[i];++j)
            {
                --a[j];
                if (a[j] < 0)return false;
            }
        }
        if (a[n - 1] != 0)
            return false;
        return true;
    }
    
    void solve()
    {
        while (cin >> n && n)
        {
            memset(a, 0, sizeof(a));
            int ok = 1;
            for (int i = 0;i < n;i++)
            {
                cin >> a[i];
                if (a[i] >= n)
                {
                    ok = 0;
                }
            }

            if (ok == 1 && check())
            {
                cout << "Possible" << endl;
            }
            else
            {
                cout << "Not possible" << endl;
            }
        }


    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shuiyonglewodezzzzz/p/10162556.html
今日推荐