Submission #944618

# Submission time Handle Problem Language Result Execution time Memory
944618 2024-03-13T01:54:59 Z Nhoksocqt1 Longest Trip (IOI23_longesttrip) C++17
30 / 100
850 ms 868 KB
#ifndef Nhoksocqt1
    #include "longesttrip.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 260;

#ifdef Nhoksocqt1

bitset<MAXN> adjp[MAXN], idA, idB, bsA, bsB;
struct Jury {
    int n, d;

    void init(int _n, int _d) {
        n = _n, d = _d;
        int u, v;
        bool check(0);
        while(cin >> u >> v) {
            adjp[u][v] = adjp[v][u] = 1;
            check = 1;
        }

        if(!check) {
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    for (int k = j + 1; k < n; ++k) {
                        int cnt = adjp[i][j] + adjp[i][k] + adjp[j][k];
                        if(cnt < d) {
                            vector<ii> p;
                            p.push_back(ii(i, j));
                            p.push_back(ii(i, k));
                            p.push_back(ii(j, k));

                            while(cnt < d) {
                                int r = Random(0, 2);
                                if(adjp[p[r].fi][p[r].se])
                                    continue;

                                ++cnt;
                                adjp[p[r].fi][p[r].se] = adjp[p[r].se][p[r].fi] = 1;
                                cout << p[r].fi << ' ' << p[r].se << '\n';
                            }
                        }
                    }
                }
            }
        }
    }

    bool are_connected(vector<int> &A, vector<int> &B) {
        bsA = bsB = idA = idB = 0;
        for (int it = 0; it < sz(A); ++it) {
            bsA |= adjp[A[it]];
            idA[A[it]] = 1;
        }

        for (int it = 0; it < sz(B); ++it) {
            bsB |= adjp[B[it]];
            idB[B[it]] = 1;
        }

        return ((bsA & idB).count() > 0 || (bsB & idA).count() > 0);
    }

} jury;

#endif // Nhoksocqt1

vector<int> A[MAXN];
int tr[MAXN], numNode;
bool adj[MAXN][MAXN], dx[MAXN];

bool check_are_connected(vector<int> &A, vector<int> &B) {
    #ifdef Nhoksocqt1
        return jury.are_connected(A, B);
    #else
        return are_connected(A, B);
    #endif // Nhoksocqt1
}

int evaluation(int u) {
    vector<int> tmp;
    while(1) {
        dx[u] = 1;
        tmp.push_back(u);

        int maxLen(0), v(-1);
        for (int i = 0; i < numNode; ++i) {
            if(!dx[i] && adj[u][i]) {
                v = i;
                break;
            }
        }

        if(v < 0)
            break;

        u = v;
    }

    int res = sz(tmp);
    while(sz(tmp)) {
        dx[tmp.back()] = 0;
        tmp.pop_back();
    }

    return res;
}

int evaluation2(int u) {
    vector<int> tmp;
    while(1) {
        dx[u] = 1;
        tmp.push_back(u);

        int maxLen(0), v(-1);
        for (int i = 0; i < numNode; ++i) {
            if(!dx[i] && adj[u][i]) {
                int len = evaluation(i);
                if(maxLen < len) {
                    maxLen = len;
                    v = i;
                }
            }
        }

        if(v < 0)
            break;

        u = v;
    }

    int res = sz(tmp);
    while(sz(tmp)) {
        dx[tmp.back()] = 0;
        tmp.pop_back();
    }

    return res;
}

vector<int> longest_trip(int N, int D) {
    numNode = N;
    if(D == 3) {
        vector<int> ans;
        for (int i = 0; i < numNode; ++i)
            ans.push_back(i);

        return ans;
    }

    for (int u = 0; u < numNode; ++u) {
        for (int v = u + 1; v < numNode; ++v) {
            vector<int> A, B;
            A.push_back(u);
            B.push_back(v);
            adj[u][v] = adj[v][u] = check_are_connected(A, B);
        }
    }

    for (int i = 0; i < numNode; ++i)
        dx[i] = 0;

    vector<int> ans;
    int len(0), u(-1);
    for (int i = 0; i < numNode; ++i) {
        int len = evaluation(i);
        if(len >= numNode / 2) {
            u = i;
            break;
        }
    }

    while(1) {
        dx[u] = 1;
        ans.push_back(u);

        int maxLen(0), v(-1);
        for (int i = 0; i < numNode; ++i) {
            if(!dx[i] && adj[u][i]) {
                int len = sz(ans) + evaluation(i);
                if(len >= numNode / 2) {
                    v = i;
                    break;
                }
            }
        }

        if(v < 0)
            break;

        u = v;
    }

    return ans;
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "longesttrip"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    int n, d;
    cin >> n >> d;
    jury.init(n, d);

    vector<int> path = longest_trip(n, d);
    cout << "ANSWER: " << sz(path) << ": ";
    for (int it = 0; it < sz(path); ++it)
        cout << path[it] << ' '; cout << '\n';

    return 0;
}

#endif // Nhoksocqt1

Compilation message

longesttrip.cpp: In function 'int evaluation(int)':
longesttrip.cpp:100:13: warning: unused variable 'maxLen' [-Wunused-variable]
  100 |         int maxLen(0), v(-1);
      |             ^~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:191:13: warning: unused variable 'maxLen' [-Wunused-variable]
  191 |         int maxLen(0), v(-1);
      |             ^~~~~~
longesttrip.cpp:178:9: warning: unused variable 'len' [-Wunused-variable]
  178 |     int len(0), u(-1);
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 188 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 28 ms 344 KB Output is correct
3 Correct 127 ms 600 KB Output is correct
4 Correct 339 ms 344 KB Output is correct
5 Correct 765 ms 488 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Correct 134 ms 340 KB Output is correct
4 Correct 382 ms 344 KB Output is correct
5 Correct 812 ms 344 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 26 ms 344 KB Output is correct
8 Correct 116 ms 344 KB Output is correct
9 Correct 267 ms 344 KB Output is correct
10 Correct 790 ms 492 KB Output is correct
11 Correct 758 ms 488 KB Output is correct
12 Correct 763 ms 592 KB Output is correct
13 Correct 754 ms 496 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 39 ms 344 KB Output is correct
17 Correct 83 ms 344 KB Output is correct
18 Correct 120 ms 344 KB Output is correct
19 Correct 278 ms 596 KB Output is correct
20 Correct 279 ms 344 KB Output is correct
21 Correct 771 ms 492 KB Output is correct
22 Correct 753 ms 492 KB Output is correct
23 Correct 747 ms 600 KB Output is correct
24 Correct 771 ms 488 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 9 ms 344 KB Output is correct
27 Correct 27 ms 344 KB Output is correct
28 Correct 24 ms 344 KB Output is correct
29 Correct 24 ms 344 KB Output is correct
30 Correct 150 ms 452 KB Output is correct
31 Correct 169 ms 456 KB Output is correct
32 Correct 177 ms 460 KB Output is correct
33 Correct 250 ms 344 KB Output is correct
34 Correct 255 ms 344 KB Output is correct
35 Correct 262 ms 868 KB Output is correct
36 Correct 709 ms 600 KB Output is correct
37 Correct 710 ms 492 KB Output is correct
38 Correct 704 ms 492 KB Output is correct
39 Correct 825 ms 488 KB Output is correct
40 Correct 761 ms 496 KB Output is correct
41 Correct 777 ms 600 KB Output is correct
42 Correct 796 ms 344 KB Output is correct
43 Correct 766 ms 492 KB Output is correct
44 Correct 784 ms 492 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 28 ms 344 KB Output is correct
48 Correct 24 ms 344 KB Output is correct
49 Correct 23 ms 600 KB Output is correct
50 Correct 180 ms 448 KB Output is correct
51 Correct 158 ms 456 KB Output is correct
52 Correct 176 ms 452 KB Output is correct
53 Correct 268 ms 344 KB Output is correct
54 Correct 272 ms 344 KB Output is correct
55 Correct 277 ms 344 KB Output is correct
56 Correct 827 ms 492 KB Output is correct
57 Correct 850 ms 712 KB Output is correct
58 Correct 828 ms 600 KB Output is correct
59 Correct 772 ms 492 KB Output is correct
60 Correct 850 ms 496 KB Output is correct
61 Correct 765 ms 688 KB Output is correct
62 Correct 774 ms 496 KB Output is correct
63 Correct 699 ms 488 KB Output is correct
64 Correct 774 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 30 ms 344 KB Output is correct
3 Partially correct 123 ms 344 KB Output is partially correct
4 Partially correct 355 ms 344 KB Output is partially correct
5 Partially correct 794 ms 600 KB Output is partially correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -