Submission #1112568

# Submission time Handle Problem Language Result Execution time Memory
1112568 2024-11-14T10:54:20 Z tibinyte Longest Trip (IOI23_longesttrip) C++17
0 / 100
1000 ms 584 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const string asafaciunstringsus = R""""(
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
)"""";

typedef long long ll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int random(int st, int dr)
{
    uniform_int_distribution<int> dist(st, dr);
    return dist(rng);
}

template <typename t>
using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;

const int mod = 1e9 + 7;
struct Mint
{
    int val;
    Mint(int x = 0)
    {
        val = x % mod;
    }
    Mint(long long x)
    {
        val = x % mod;
    }
    Mint operator+(Mint oth)
    {
        return val + oth.val;
    }
    Mint operator-(Mint oth)
    {
        return val - oth.val + mod;
    }
    Mint operator*(Mint oth)
    {
        return 1ll * val * oth.val;
    }
    void operator+=(Mint oth)
    {
        val = (*this + oth).val;
    }
    void operator-=(Mint oth)
    {
        val = (*this - oth).val;
    }
    void operator*=(Mint oth)
    {
        val = (*this * oth).val;
    }
};

Mint powmod(int a, int b)
{
    if (b == 0)
    {
        return 1;
    }
    if (b % 2 == 1)
    {
        return powmod(a, b - 1) * a;
    }
    Mint p = powmod(a, b / 2);
    return p * p;
}

/*
                 .___                 __                 __           .__
  ____  ____   __| _/____     _______/  |______ ________/  |_  ______ |  |__   ___________   ____
_/ ___\/  _ \ / __ |/ __ \   /  ___/\   __\__  \\_  __ \   __\/  ___/ |  |  \_/ __ \_  __ \_/ __ \
\  \__(  <_> ) /_/ \  ___/   \___ \  |  |  / __ \|  | \/|  |  \___ \  |   Y  \  ___/|  | \/\  ___/
 \___  >____/\____ |\___  > /____  > |__| (____  /__|   |__| /____  > |___|  /\___  >__|    \___  >
     \/           \/    \/       \/            \/                 \/       \/     \/            \/
*/

std::vector<int> longest_trip(int N, int D)
{
    int n = N;
    vector<vector<bool>> mat(n, vector<bool>(n));
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            if (are_connected({i}, {j}))
            {
                mat[i][j] = mat[j][i] = 1;
            }
        }
    }
    vector<bool> vis(n);
    vector<int> ord;
    function<void(int)> dfs = [&](int node)
    {
        ord.push_back(node);
        vis[node] = true;
        for (int i = 0; i < n; ++i)
        {
            if (!vis[i] && mat[node][i])
            {
                dfs(i);
            }
        }
    };
    function<vector<int>(vector<int>)> build = [&](vector<int> ord)
    {
        if (ord.size() <= 2)
        {
            return ord;
        }
        vector<int> ans;
        ans.push_back(ord[0]);
        ans.push_back(ord[1]);
        for (int lol = 2; lol < ord.size(); ++lol)
        {
            int it = ord[lol];
            if (mat[ans.back()][it])
            {
                ans.push_back(it);
                continue;
            }
            if (mat[ans.front()][it])
            {
                ans.insert(ans.begin(), it);
                continue;
            }
            assert(mat[ans.front()][ans.back()]); // we have a cycle
            for (int i = 0; i < ans.size(); ++i)
            {
                int node = ans[i];
                if (mat[node][it])
                {
                    vector<int> new_ans;
                    new_ans.push_back(it);
                    for (int j = i; j >= 0; --j)
                    {
                        new_ans.push_back(ans[j]);
                    }
                    for (int j = ans.size() - 1; j > i; --j)
                    {
                        new_ans.push_back(ans[j]);
                    }
                    ans = new_ans;
                    break;
                }
            }
        }
        return ans;
    };
    vector<int> ans;
    for (int i = 1; i <= n; ++i)
    {
        if (!vis[i])
        {
            dfs(i);
            vector<int> lesgo = build(ord);
            if (lesgo.size() > ans.size())
            {
                ans = lesgo;
            }
            ord.clear();
        }
    }
    return ans;
}

/*
I cannot take this anymore
Saying everything I've said before
All these words, they make no sense
I find bliss in ignorance
Less I hear, the less you say
You'll find that out anyway
Just like before

Everything you say to me
(Takes me one step closer to the edge)
(And I'm about to break)
I need a little room to breathe
(Cause I'm one step closer to the edge)
(I'm about to break)
*/

Compilation message

longesttrip.cpp: In lambda function:
longesttrip.cpp:127:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |         for (int lol = 2; lol < ord.size(); ++lol)
      |                           ~~~~^~~~~~~~~~~~
longesttrip.cpp:141:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |             for (int i = 0; i < ans.size(); ++i)
      |                             ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 626 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 336 KB Output is correct
2 Correct 76 ms 336 KB Output is correct
3 Correct 465 ms 336 KB Output is correct
4 Execution timed out 1245 ms 584 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 336 KB Output is correct
2 Correct 75 ms 336 KB Output is correct
3 Correct 480 ms 336 KB Output is correct
4 Execution timed out 1302 ms 440 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 336 KB Output is correct
2 Correct 76 ms 336 KB Output is correct
3 Correct 469 ms 336 KB Output is correct
4 Execution timed out 1154 ms 440 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 336 KB Output is correct
2 Correct 81 ms 336 KB Output is correct
3 Partially correct 428 ms 336 KB Output is partially correct
4 Execution timed out 1071 ms 440 KB Time limit exceeded
5 Halted 0 ms 0 KB -