답안 #840642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840642 2023-08-31T14:50:31 Z danikoynov 가장 긴 여행 (IOI23_longesttrip) C++17
30 / 100
893 ms 964 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 > 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)
    {
        vector < int > cur;
        cur.push_back(v);
        return cur;
    }

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

    vector < int > r1 = construct(viable[0]), r2;
    for (int u : viable)
    {
        if (used[u] == 0)
            r2 = construct(u);
    }
    vector < int > res = r1;
    if (r2.size() > res.size())
        res = r2;
    res.push_back(v);
    return res;
}
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();

    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;
    vector < int > res = construct(0);


    return res;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> 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 ++)
      |                     ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 214 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 28 ms 208 KB Output is correct
3 Correct 141 ms 340 KB Output is correct
4 Correct 437 ms 468 KB Output is correct
5 Correct 692 ms 840 KB Output is correct
6 Incorrect 1 ms 208 KB Incorrect
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 208 KB Output is correct
2 Correct 31 ms 208 KB Output is correct
3 Correct 163 ms 340 KB Output is correct
4 Correct 429 ms 452 KB Output is correct
5 Correct 830 ms 836 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Correct 29 ms 208 KB Output is correct
8 Correct 160 ms 344 KB Output is correct
9 Correct 309 ms 348 KB Output is correct
10 Correct 846 ms 856 KB Output is correct
11 Correct 853 ms 964 KB Output is correct
12 Correct 869 ms 868 KB Output is correct
13 Correct 778 ms 884 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 15 ms 208 KB Output is correct
16 Correct 65 ms 208 KB Output is correct
17 Correct 107 ms 208 KB Output is correct
18 Correct 139 ms 356 KB Output is correct
19 Correct 313 ms 428 KB Output is correct
20 Correct 272 ms 428 KB Output is correct
21 Correct 837 ms 872 KB Output is correct
22 Correct 826 ms 852 KB Output is correct
23 Correct 818 ms 840 KB Output is correct
24 Correct 863 ms 852 KB Output is correct
25 Correct 12 ms 208 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 30 ms 208 KB Output is correct
28 Correct 29 ms 208 KB Output is correct
29 Correct 31 ms 208 KB Output is correct
30 Correct 207 ms 336 KB Output is correct
31 Correct 211 ms 336 KB Output is correct
32 Correct 181 ms 208 KB Output is correct
33 Correct 299 ms 332 KB Output is correct
34 Correct 321 ms 336 KB Output is correct
35 Correct 319 ms 348 KB Output is correct
36 Correct 739 ms 544 KB Output is correct
37 Correct 703 ms 572 KB Output is correct
38 Correct 824 ms 564 KB Output is correct
39 Correct 816 ms 584 KB Output is correct
40 Correct 837 ms 664 KB Output is correct
41 Correct 831 ms 428 KB Output is correct
42 Correct 736 ms 444 KB Output is correct
43 Correct 791 ms 588 KB Output is correct
44 Correct 893 ms 584 KB Output is correct
45 Correct 12 ms 208 KB Output is correct
46 Correct 11 ms 208 KB Output is correct
47 Correct 29 ms 320 KB Output is correct
48 Correct 34 ms 208 KB Output is correct
49 Correct 32 ms 208 KB Output is correct
50 Correct 208 ms 424 KB Output is correct
51 Correct 219 ms 348 KB Output is correct
52 Correct 210 ms 348 KB Output is correct
53 Correct 326 ms 352 KB Output is correct
54 Correct 327 ms 420 KB Output is correct
55 Correct 284 ms 444 KB Output is correct
56 Correct 754 ms 844 KB Output is correct
57 Correct 789 ms 820 KB Output is correct
58 Correct 778 ms 780 KB Output is correct
59 Correct 769 ms 820 KB Output is correct
60 Correct 863 ms 784 KB Output is correct
61 Correct 771 ms 728 KB Output is correct
62 Correct 733 ms 616 KB Output is correct
63 Correct 830 ms 688 KB Output is correct
64 Correct 792 ms 792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 28 ms 208 KB Output is correct
3 Partially correct 154 ms 328 KB Output is partially correct
4 Partially correct 388 ms 480 KB Output is partially correct
5 Partially correct 767 ms 852 KB Output is partially correct
6 Incorrect 0 ms 208 KB Incorrect
7 Halted 0 ms 0 KB -