Submission #840650

# Submission time Handle Problem Language Result Execution time Memory
840650 2023-08-31T14:57:54 Z danikoynov Longest Trip (IOI23_longesttrip) C++17
5 / 100
1000 ms 772 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 260;
vector < int > adj[maxn];
int used[maxn], edge[maxn][maxn];

int n;
void dfs(int v, int c)
{
    used[v] = c;
    for (int u : adj[v])
    {
        if (!used[u])
            dfs(u, c);

    }
}

int deg[maxn];
vector < int > ans[maxn];
void construct(int v)
{
    used[v] = 1;
    vector < int > viable;
    for (int u : adj[v])
        if (!used[u])
        {
            viable.push_back(u);
            deg[u] --;
            deg[v] --;
        }

    if (viable.size() == 0)
    {
        ans[v].push_back(v);
        return;
    }

    for (int i = 1; i < viable.size(); i ++)
    {
        if (deg[viable[i]] < deg[viable[0]])
            swap(viable[i], viable[0]);
    }
    if (viable.size() == 1)
    {
        swap(ans[v], ans[viable[0]]);
        ans[v].push_back(v);
        return;
    }

    int var = -1, dar = viable[0];
    for (int u : viable)
    {
        if (used[u] == 0)
            {
                construct(u);
                var = u;
            }
    }

    if (var == -1)
    {
        swap(ans[v], ans[dar]);
        ans[v].push_back(v);
        return;
    }

    ///if (ans[var].size() > ans[dar].size())
       /// swap(var, dar);
    swap(ans[v], ans[var]);
    ans[v].push_back(v);
    for (int u : ans[dar])
        ans[v].push_back(u);
    //if (r2.size() > res.size())
      //  res = r2;
    //res.push_back(v);
    return;
}
vector<int> longest_trip(int N, int D)
{
    n = N;
    if (n == 0)
        return {};
    if (D == 3)
    {
        vector < int > r;
        for (int i = 0; i < N; i ++)
            r.push_back(i);
        return r;
    }

    for (int i = 0; i < N; i ++)
        adj[i].clear(), ans[i].clear();

    for (int i = 0; i < N; i ++)
        used[i] = 0, deg[i] = 0;
    for (int i = 0; i < N; i ++)
        for (int j = i + 1; j < N; j ++)
        {
            vector < int > a, b;
            a.push_back(i);
            b.push_back(j);
            if (are_connected(a, b))
            {
                adj[i].push_back(j);
                adj[j].push_back(i);

                deg[i] ++;
                deg[j] ++;
            }
        }


    int cp = 0;
    for (int i = 0; i < N; i ++)
    {
        if (!used[i])
            dfs(i, ++ cp);
    }

    if (cp == 2)
    {

        vector < int > a, b;
        for (int i = 0; i < N; i ++)
            if (used[i] == 1)
                a.push_back(i);
            else
                b.push_back(i);
        if (a.size() > b.size())
            return a;
        return b;
    }


    for (int i = 0; i < N; i ++)
        used[i] = 0;
    construct(0);

    return ans[0];
}

Compilation message

longesttrip.cpp: In function 'void construct(int)':
longesttrip.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 1; i < viable.size(); i ++)
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 208 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 31 ms 320 KB Output is correct
3 Correct 152 ms 336 KB Output is correct
4 Correct 527 ms 440 KB Output is correct
5 Execution timed out 1115 ms 772 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 36 ms 320 KB Output is correct
3 Correct 220 ms 340 KB Output is correct
4 Correct 433 ms 428 KB Output is correct
5 Execution timed out 1058 ms 772 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 30 ms 320 KB Output is correct
3 Partially correct 174 ms 336 KB Output is partially correct
4 Partially correct 489 ms 428 KB Output is partially correct
5 Partially correct 873 ms 768 KB Output is partially correct
6 Incorrect 0 ms 208 KB Incorrect
7 Halted 0 ms 0 KB -