Submission #62925791


Source Code Expand

Copy
/**
*
*
* 1
* 1
*/
#include <bits/stdc++.h>
using namespace std;
//@brief
struct Timer
{
chrono::system_clock::time_point start;
Timer()
{
start = chrono::system_clock::now();
}
void reset()
{
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/**
 * 以下の近傍を用いる焼き鈍し解法
 * 近傍
 *  ・1点の深さを変更(隣接頂点いずれかの子とみなせる深さ)
 *  ・1点を根にする
 */
#include <bits/stdc++.h>
using namespace std;

//@brief プログラムの実行時間を計測する
struct Timer
{
    chrono::system_clock::time_point start;

    Timer()
    {
        start = chrono::system_clock::now();
    }

    void reset()
    {
        start = chrono::system_clock::now();
    }

    //@brief プログラムの実行時間を取得する
    //@return プログラムの実行時間(秒)
    double getTime()
    {
        auto now = chrono::system_clock::now();
        return chrono::duration<double>(now - start).count();
    }
};

//@brief startからendまでの範囲でprogressに応じ、最初に急激に減少、その後緩やかに減少した値をとる
//@param start 開始値
//@param end 終了値
//@param progress 進捗度(0~1)
//@return 進捗に合わせた値
double exponential_decrease(double start, double end, double progress)
{
    return pow(start, 1.0 - progress) * pow(end, progress);
}

Timer timer;
class Xorshift
{
public:
    using result_type = uint64_t;

    explicit Xorshift(uint64_t seed) : x_(seed) { assert(seed); }

    // [0, stop)
    uint64_t randrange(uint64_t stop)
    {
        assert(stop > 0);
        next();
        return x_ % stop;
    }

    // [start, stop)
    uint64_t randrange(uint64_t start, uint64_t stop)
    {
        assert(start < stop);
        next();
        return start + x_ % (stop - start);
    }

    // [a, b]
    uint64_t randint(uint64_t a, uint64_t b)
    {
        assert(a <= b);
        return randrange(a, b + 1);
    }

    // [0.0, 1.0]
    double random()
    {
        next();
        return static_cast<double>(x_) * (1.0 / static_cast<double>(UINT64_MAX));
    }

    // [a, b] or [b, a]
    double uniform(double a, double b) { return a + (b - a) * random(); }

    uint64_t next()
    {
        x_ ^= x_ << 13;
        x_ ^= x_ >> 17;
        x_ ^= x_ << 5;
        return x_;
    }

    bool gen_bool(double p)
    {
        return random() < p;
    }

    // Add this operator to use with std::shuffle
    uint64_t operator()()
    {
        return next();
    }

    static constexpr uint64_t min() { return 0; }
    static constexpr uint64_t max() { return UINT64_MAX; }

private:
    uint64_t x_;
};
Xorshift rng(1);
constexpr int N = 1000, H = 10;
int M;
vector<vector<int>> G(N);
int A[N], X[N], Y[N];

void input()
{
    int _;
    cin >> _ >> M >> _;
    for (int i = 0; i < N; i++)
        cin >> A[i];
    for (int i = 0; i < M; i++)
    {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for (int i = 0; i < N; i++)
        cin >> X[i] >> Y[i];
    for (int i = 0; i < N; i++)
    {
        sort(G[i].begin(), G[i].end(), [&](int x, int y)
             { return A[x] < A[y]; });
    }
}

/// @brief 各頂点の高さを元に親を復元して出力
/// @param heigts 頂点iの高さ
void output(vector<int> &heigts)
{
    vector<int> p(N, -1); // 頂点iの親の頂点
    for (int i = 0; i < N; i++)
    {
        if (heigts[i] == 0)
        {
            p[i] = -1;
        }
        else
        {
            for (auto to : G[i])
            {
                if (heigts[to] + 1 == heigts[i])
                {
                    p[i] = to;
                }
            }
        }
    }
    for (int i = 0; i < N; i++)
        cout << p[i] << " \n"[i + 1 == N];
}

struct SimulatedAnnealing
{
    const float start_temp = 32;
    const float end_temp = 4.8;
    const int TEMP_CHANGE_STEP = 4096;

    // 頂点iの隣接点を高さごとに何個あるか数える。
    // e.g. next_height_cnt[i][h]=2なら、頂点iの隣接点のうち、高さhの頂点が2個ある。
    int next_height_cnt[N][H + 1];
    vector<int> heights;
    int score = 0;

    vector<int> best_heights;
    int best_score = 0;

    SimulatedAnnealing(const vector<int> &heights) : heights(heights)
    {
        // 各頂点の隣接点を高さごとに何個あるか数える。
        for (int i = 0; i < N; i++)
            for (int j = 0; j < H + 1; j++)
                next_height_cnt[i][j] = 0;
        for (int i = 0; i < N; i++)
        {
            score += A[i] * (heights[i] + 1);
            for (auto &e : G[i])
            {
                assert(e < heights.size());
                assert(heights[e] < H + 1);
                next_height_cnt[i][heights[e]]++;
            }
        }
    }

    void solve(double time_limit)
    {

        best_heights = heights;
        best_score = score;

        float current_temp = start_temp;
        while (timer.getTime() < time_limit)
        {
            current_temp = exponential_decrease(start_temp, end_temp, timer.getTime() / time_limit);
            for (int _ = 0; _ < TEMP_CHANGE_STEP; _++)
            {
                // 1点の深さを変更
                int cur = rng.randrange(N);
                int new_parent = G[cur][rng.randrange(G[cur].size())];
                int prev_height = heights[cur];
                int new_height = heights[new_parent] + 1;
                if (rng.randrange(10) == 0)
                    new_height = 0;

                if (new_height > H)
                    continue;
                if (new_height == heights[cur])
                    continue;

                int height_diff = new_height - prev_height;
                int score_diff = A[cur] * height_diff;
                if (score_diff < 0 && exp(score_diff / current_temp) < rng.random())
                    continue;

                // 変更しても木が構築可能かチェック
                // 対象頂点に子の頂点がある場合、その子が別の頂点を親にできれば、
                // 子の高さを変えずに済む。
                // よって、対象頂点の子とつながる頂点が他に対象頂点の元の高さと同じ高さの頂点を持つかどうかをチェックする。
                bool can_change = true;
                for (auto &e : G[cur])
                    if (heights[e] == prev_height + 1)
                    {
                        if (next_height_cnt[e][prev_height] == 1)
                        {
                            can_change = false;
                            break;
                        }
                    }
                if (!can_change)
                    continue;

                // 更新
                score += score_diff;
                heights[cur] = new_height;
                for (auto &e : G[cur])
                {
                    next_height_cnt[e][prev_height]--;
                    next_height_cnt[e][new_height]++;
                }

                if (best_score < score)
                {
                    best_score = score;
                    best_heights = heights;
                }
            }
        }
    }
    inline vector<int> getAnswer()
    {
        return best_heights;
    }
};

int main()
{
    ios::sync_with_stdio(false); // cin, coutの同期を切る
    cin.tie(0);                  // cinのバッファリングを切る
    cout.tie(0);                 // coutのバッファリングを切る
    input();
    vector<int> heights(N, 0);
    {
        timer.reset();

        SimulatedAnnealing annealing(heights);
        annealing.solve(1.98);
        cerr << annealing.best_score << endl;
        heights = annealing.getAnswer();
        auto elapsed_time = timer.getTime();
        cerr << "elapsed_time for annealing: " << elapsed_time << endl;
    }

    output(heights);
}

Submission Info

Submission Time
Task A - Christmas Tree Cutting
User thunder
Language C++ 23 (gcc 12.2)
Score 70420513
Code Size 7979 Byte
Status AC
Exec Time 1983 ms
Memory 4284 KB

Compile Error

In file included from /usr/include/c++/12/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/12/bits/stdc++.h:33,
                 from Main.cpp:7:
Main.cpp: In constructor ‘SimulatedAnnealing::SimulatedAnnealing(const std::vector<int>&)’:
Main.cpp:190:26: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  190 |                 assert(e < heights.size());
      |                        ~~^~~~~~~~~~~~~~~~

Judge Result

Set Name test_ALL
Score / Max Score 70420513 / 300000000000
Status
AC × 150
Set Name Test Cases
test_ALL test_0000.txt, test_0001.txt, test_0002.txt, test_0003.txt, test_0004.txt, test_0005.txt, test_0006.txt, test_0007.txt, test_0008.txt, test_0009.txt, test_0010.txt, test_0011.txt, test_0012.txt, test_0013.txt, test_0014.txt, test_0015.txt, test_0016.txt, test_0017.txt, test_0018.txt, test_0019.txt, test_0020.txt, test_0021.txt, test_0022.txt, test_0023.txt, test_0024.txt, test_0025.txt, test_0026.txt, test_0027.txt, test_0028.txt, test_0029.txt, test_0030.txt, test_0031.txt, test_0032.txt, test_0033.txt, test_0034.txt, test_0035.txt, test_0036.txt, test_0037.txt, test_0038.txt, test_0039.txt, test_0040.txt, test_0041.txt, test_0042.txt, test_0043.txt, test_0044.txt, test_0045.txt, test_0046.txt, test_0047.txt, test_0048.txt, test_0049.txt, test_0050.txt, test_0051.txt, test_0052.txt, test_0053.txt, test_0054.txt, test_0055.txt, test_0056.txt, test_0057.txt, test_0058.txt, test_0059.txt, test_0060.txt, test_0061.txt, test_0062.txt, test_0063.txt, test_0064.txt, test_0065.txt, test_0066.txt, test_0067.txt, test_0068.txt, test_0069.txt, test_0070.txt, test_0071.txt, test_0072.txt, test_0073.txt, test_0074.txt, test_0075.txt, test_0076.txt, test_0077.txt, test_0078.txt, test_0079.txt, test_0080.txt, test_0081.txt, test_0082.txt, test_0083.txt, test_0084.txt, test_0085.txt, test_0086.txt, test_0087.txt, test_0088.txt, test_0089.txt, test_0090.txt, test_0091.txt, test_0092.txt, test_0093.txt, test_0094.txt, test_0095.txt, test_0096.txt, test_0097.txt, test_0098.txt, test_0099.txt, test_0100.txt, test_0101.txt, test_0102.txt, test_0103.txt, test_0104.txt, test_0105.txt, test_0106.txt, test_0107.txt, test_0108.txt, test_0109.txt, test_0110.txt, test_0111.txt, test_0112.txt, test_0113.txt, test_0114.txt, test_0115.txt, test_0116.txt, test_0117.txt, test_0118.txt, test_0119.txt, test_0120.txt, test_0121.txt, test_0122.txt, test_0123.txt, test_0124.txt, test_0125.txt, test_0126.txt, test_0127.txt, test_0128.txt, test_0129.txt, test_0130.txt, test_0131.txt, test_0132.txt, test_0133.txt, test_0134.txt, test_0135.txt, test_0136.txt, test_0137.txt, test_0138.txt, test_0139.txt, test_0140.txt, test_0141.txt, test_0142.txt, test_0143.txt, test_0144.txt, test_0145.txt, test_0146.txt, test_0147.txt, test_0148.txt, test_0149.txt
Case Name Status Exec Time Memory
test_0000.txt AC 1982 ms 4068 KB
test_0001.txt AC 1982 ms 4064 KB
test_0002.txt AC 1982 ms 4256 KB
test_0003.txt AC 1982 ms 4264 KB
test_0004.txt AC 1982 ms 4280 KB
test_0005.txt AC 1982 ms 4056 KB
test_0006.txt AC 1982 ms 4132 KB
test_0007.txt AC 1982 ms 4204 KB
test_0008.txt AC 1982 ms 4268 KB
test_0009.txt AC 1982 ms 4056 KB
test_0010.txt AC 1982 ms 4244 KB
test_0011.txt AC 1982 ms 4124 KB
test_0012.txt AC 1982 ms 4264 KB
test_0013.txt AC 1982 ms 4064 KB
test_0014.txt AC 1982 ms 4264 KB
test_0015.txt AC 1982 ms 4236 KB
test_0016.txt AC 1982 ms 4260 KB
test_0017.txt AC 1982 ms 4132 KB
test_0018.txt AC 1982 ms 4284 KB
test_0019.txt AC 1982 ms 4048 KB
test_0020.txt AC 1982 ms 4200 KB
test_0021.txt AC 1982 ms 4160 KB
test_0022.txt AC 1982 ms 4256 KB
test_0023.txt AC 1982 ms 4264 KB
test_0024.txt AC 1982 ms 4064 KB
test_0025.txt AC 1982 ms 4124 KB
test_0026.txt AC 1982 ms 4068 KB
test_0027.txt AC 1982 ms 4120 KB
test_0028.txt AC 1982 ms 4240 KB
test_0029.txt AC 1982 ms 4204 KB
test_0030.txt AC 1982 ms 4088 KB
test_0031.txt AC 1982 ms 4132 KB
test_0032.txt AC 1982 ms 4284 KB
test_0033.txt AC 1982 ms 4244 KB
test_0034.txt AC 1982 ms 4264 KB
test_0035.txt AC 1982 ms 4268 KB
test_0036.txt AC 1982 ms 4280 KB
test_0037.txt AC 1982 ms 4244 KB
test_0038.txt AC 1982 ms 4240 KB
test_0039.txt AC 1982 ms 4200 KB
test_0040.txt AC 1982 ms 4208 KB
test_0041.txt AC 1982 ms 4208 KB
test_0042.txt AC 1982 ms 4256 KB
test_0043.txt AC 1982 ms 4280 KB
test_0044.txt AC 1982 ms 4204 KB
test_0045.txt AC 1982 ms 4056 KB
test_0046.txt AC 1982 ms 4240 KB
test_0047.txt AC 1982 ms 4284 KB
test_0048.txt AC 1982 ms 4164 KB
test_0049.txt AC 1982 ms 4264 KB
test_0050.txt AC 1982 ms 4248 KB
test_0051.txt AC 1983 ms 4044 KB
test_0052.txt AC 1982 ms 4068 KB
test_0053.txt AC 1982 ms 4240 KB
test_0054.txt AC 1982 ms 4132 KB
test_0055.txt AC 1982 ms 4136 KB
test_0056.txt AC 1982 ms 4256 KB
test_0057.txt AC 1982 ms 4268 KB
test_0058.txt AC 1982 ms 4260 KB
test_0059.txt AC 1982 ms 4136 KB
test_0060.txt AC 1982 ms 4132 KB
test_0061.txt AC 1982 ms 4280 KB
test_0062.txt AC 1982 ms 4200 KB
test_0063.txt AC 1982 ms 4284 KB
test_0064.txt AC 1982 ms 4260 KB
test_0065.txt AC 1982 ms 4132 KB
test_0066.txt AC 1982 ms 4264 KB
test_0067.txt AC 1982 ms 4132 KB
test_0068.txt AC 1982 ms 4240 KB
test_0069.txt AC 1982 ms 4128 KB
test_0070.txt AC 1982 ms 4120 KB
test_0071.txt AC 1982 ms 4256 KB
test_0072.txt AC 1982 ms 4048 KB
test_0073.txt AC 1982 ms 4216 KB
test_0074.txt AC 1982 ms 4120 KB
test_0075.txt AC 1982 ms 4056 KB
test_0076.txt AC 1982 ms 4204 KB
test_0077.txt AC 1982 ms 4116 KB
test_0078.txt AC 1982 ms 4064 KB
test_0079.txt AC 1982 ms 4164 KB
test_0080.txt AC 1982 ms 4136 KB
test_0081.txt AC 1982 ms 4248 KB
test_0082.txt AC 1982 ms 4240 KB
test_0083.txt AC 1982 ms 4164 KB
test_0084.txt AC 1982 ms 4200 KB
test_0085.txt AC 1982 ms 4060 KB
test_0086.txt AC 1982 ms 4240 KB
test_0087.txt AC 1982 ms 4124 KB
test_0088.txt AC 1982 ms 4256 KB
test_0089.txt AC 1982 ms 4204 KB
test_0090.txt AC 1982 ms 4204 KB
test_0091.txt AC 1982 ms 4260 KB
test_0092.txt AC 1982 ms 4244 KB
test_0093.txt AC 1982 ms 4284 KB
test_0094.txt AC 1982 ms 4132 KB
test_0095.txt AC 1982 ms 4240 KB
test_0096.txt AC 1982 ms 4260 KB
test_0097.txt AC 1982 ms 4136 KB
test_0098.txt AC 1982 ms 4260 KB
test_0099.txt AC 1982 ms 4256 KB
test_0100.txt AC 1982 ms 4252 KB
test_0101.txt AC 1982 ms 4244 KB
test_0102.txt AC 1982 ms 4248 KB
test_0103.txt AC 1982 ms 4204 KB
test_0104.txt AC 1982 ms 4136 KB
test_0105.txt AC 1982 ms 4132 KB
test_0106.txt AC 1982 ms 4208 KB
test_0107.txt AC 1982 ms 4260 KB
test_0108.txt AC 1982 ms 4268 KB
test_0109.txt AC 1982 ms 4252 KB
test_0110.txt AC 1982 ms 4160 KB
test_0111.txt AC 1982 ms 4208 KB
test_0112.txt AC 1982 ms 4208 KB
test_0113.txt AC 1982 ms 4064 KB
test_0114.txt AC 1982 ms 4240 KB
test_0115.txt AC 1982 ms 4244 KB
test_0116.txt AC 1982 ms 4220 KB
test_0117.txt AC 1982 ms 4244 KB
test_0118.txt AC 1982 ms 4204 KB
test_0119.txt AC 1982 ms 4124 KB
test_0120.txt AC 1982 ms 4160 KB
test_0121.txt AC 1982 ms 4264 KB
test_0122.txt AC 1982 ms 4056 KB
test_0123.txt AC 1982 ms 4240 KB
test_0124.txt AC 1982 ms 4284 KB
test_0125.txt AC 1982 ms 4056 KB
test_0126.txt AC 1982 ms 4120 KB
test_0127.txt AC 1982 ms 4128 KB
test_0128.txt AC 1982 ms 4064 KB
test_0129.txt AC 1982 ms 4060 KB
test_0130.txt AC 1982 ms 4280 KB
test_0131.txt AC 1982 ms 4256 KB
test_0132.txt AC 1982 ms 4056 KB
test_0133.txt AC 1982 ms 4260 KB
test_0134.txt AC 1982 ms 4056 KB
test_0135.txt AC 1982 ms 4044 KB
test_0136.txt AC 1982 ms 4200 KB
test_0137.txt AC 1982 ms 4136 KB
test_0138.txt AC 1982 ms 4060 KB
test_0139.txt AC 1982 ms 4164 KB
test_0140.txt AC 1982 ms 4136 KB
test_0141.txt AC 1982 ms 4136 KB
test_0142.txt AC 1982 ms 4044 KB
test_0143.txt AC 1982 ms 4120 KB
test_0144.txt AC 1982 ms 4264 KB
test_0145.txt AC 1982 ms 4240 KB
test_0146.txt AC 1982 ms 4240 KB
test_0147.txt AC 1982 ms 4248 KB
test_0148.txt AC 1982 ms 4260 KB
test_0149.txt AC 1982 ms 4244 KB


2025-04-06 (Sun)
06:33:23 +00:00