Submission #960370

# Submission time Handle Problem Language Result Execution time Memory
960370 2024-04-10T10:39:40 Z TAhmed33 Longest Trip (IOI23_longesttrip) C++17
60 / 100
822 ms 2056 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector <int> adj[256], tree[256];
bool vis[256];
struct DSU {
    int p[256], sze[256];
    void init (int n) {
        for (int i = 0; i < n; i++) {
            p[i] = i; sze[i] = 1;
        }
    }
    int find (int x) {
        return p[x] == x ? x : p[x] = find(p[x]);
    }
    bool merge (int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return 0;
        if (sze[a] > sze[b]) swap(a, b);
        sze[b] += sze[a]; p[a] = b;
        return 1;
    }
} cur;
vector <int> x;
int p[256];
void dfs (int pos) {
    vis[pos] = 1; x.push_back(pos);
    for (auto j : adj[pos]) {
        if (vis[j]) continue;
        tree[pos].push_back(j);
        p[j] = pos;
        dfs(j);
    }
}
int get (int x) {
    if (tree[x].empty()) return x;
    return get(tree[x][0]);
}
vector <int> longest_trip (int n, int d) {
    for (int i = 0; i < n; i++) adj[i].clear(), tree[i].clear();
    memset(p, -1, sizeof(p));
    cur.init(n);
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (are_connected({i}, {j})) {
                adj[i].push_back(j);
                adj[j].push_back(i);
                cur.merge(i, j);
            }
        }
    }
    bool flag = 0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            flag |= cur.merge(i, j);
        }
    }
    if (flag) {
        memset(vis, 0, sizeof(vis));
        vector <int> ret; 
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                x.clear(); dfs(i);
                if ((int)x.size() > (int)ret.size()) ret = x;
            }
        }
        return ret;
    }
    memset(vis, 0, sizeof(vis));
    x.clear(); dfs(0);
    int pos = -1;
    for (int i = 0; i < n; i++) {
        if ((int)tree[i].size() == 2) {
            pos = i;
        }
    }
    if (pos == -1) return x;
    int u = get(tree[pos][0]), v = get(tree[pos][1]);
    if (p[pos] != -1 && find(adj[p[pos]].begin(), adj[p[pos]].end(), u) == adj[p[pos]].end()) {
        swap(u, v); swap(tree[pos][0], tree[pos][1]);
    }
    u = tree[pos][0], v = tree[pos][1];
    while (!x.empty() && x.back() != p[pos]) x.pop_back();
    vector <int> l;
    while (true) {
        l.push_back(u);
        if (tree[u].empty()) break;
        u = tree[u][0];
    }
    reverse(l.begin(), l.end());
    for (auto i : l) x.push_back(i);
    x.push_back(pos);
    while (true) {
        x.push_back(v);
        if (tree[v].empty()) break;
        v = tree[v][0];
    }
    return x;   
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 187 ms 1840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 140 ms 1120 KB Output is correct
4 Correct 366 ms 1208 KB Output is correct
5 Correct 732 ms 1836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 151 ms 1112 KB Output is correct
4 Correct 353 ms 1112 KB Output is correct
5 Correct 754 ms 1364 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 21 ms 344 KB Output is correct
8 Correct 135 ms 856 KB Output is correct
9 Correct 285 ms 728 KB Output is correct
10 Correct 766 ms 1552 KB Output is correct
11 Correct 786 ms 1572 KB Output is correct
12 Correct 767 ms 1540 KB Output is correct
13 Correct 808 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Correct 130 ms 600 KB Output is correct
4 Correct 338 ms 1112 KB Output is correct
5 Correct 714 ms 1300 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 17 ms 344 KB Output is correct
8 Correct 111 ms 856 KB Output is correct
9 Correct 264 ms 1112 KB Output is correct
10 Correct 790 ms 1792 KB Output is correct
11 Correct 711 ms 1660 KB Output is correct
12 Correct 758 ms 1324 KB Output is correct
13 Correct 801 ms 1612 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 38 ms 344 KB Output is correct
17 Correct 94 ms 600 KB Output is correct
18 Correct 129 ms 608 KB Output is correct
19 Correct 300 ms 1212 KB Output is correct
20 Correct 262 ms 1228 KB Output is correct
21 Correct 788 ms 1412 KB Output is correct
22 Correct 731 ms 1776 KB Output is correct
23 Correct 724 ms 1348 KB Output is correct
24 Correct 754 ms 1744 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 11 ms 344 KB Output is correct
27 Correct 30 ms 344 KB Output is correct
28 Correct 20 ms 344 KB Output is correct
29 Correct 20 ms 344 KB Output is correct
30 Correct 151 ms 712 KB Output is correct
31 Correct 163 ms 708 KB Output is correct
32 Correct 161 ms 704 KB Output is correct
33 Correct 273 ms 1116 KB Output is correct
34 Correct 255 ms 856 KB Output is correct
35 Correct 265 ms 1360 KB Output is correct
36 Correct 729 ms 1120 KB Output is correct
37 Correct 728 ms 1320 KB Output is correct
38 Correct 744 ms 1584 KB Output is correct
39 Correct 769 ms 1844 KB Output is correct
40 Correct 713 ms 1628 KB Output is correct
41 Correct 748 ms 1052 KB Output is correct
42 Correct 735 ms 1288 KB Output is correct
43 Correct 745 ms 1836 KB Output is correct
44 Correct 727 ms 1624 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 25 ms 344 KB Output is correct
48 Correct 23 ms 344 KB Output is correct
49 Correct 28 ms 344 KB Output is correct
50 Correct 177 ms 700 KB Output is correct
51 Correct 163 ms 960 KB Output is correct
52 Correct 189 ms 964 KB Output is correct
53 Correct 295 ms 880 KB Output is correct
54 Correct 299 ms 736 KB Output is correct
55 Correct 268 ms 976 KB Output is correct
56 Correct 749 ms 1784 KB Output is correct
57 Correct 780 ms 1528 KB Output is correct
58 Correct 788 ms 1292 KB Output is correct
59 Correct 748 ms 1324 KB Output is correct
60 Correct 736 ms 1472 KB Output is correct
61 Correct 716 ms 1036 KB Output is correct
62 Correct 774 ms 1496 KB Output is correct
63 Correct 752 ms 1484 KB Output is correct
64 Correct 821 ms 1892 KB Output is correct
# 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 Partially correct 111 ms 616 KB Output is partially correct
4 Partially correct 396 ms 976 KB Output is partially correct
5 Partially correct 769 ms 1276 KB Output is partially correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 33 ms 344 KB Output is correct
8 Partially correct 120 ms 1316 KB Output is partially correct
9 Partially correct 250 ms 896 KB Output is partially correct
10 Partially correct 742 ms 1132 KB Output is partially correct
11 Partially correct 746 ms 1352 KB Output is partially correct
12 Partially correct 772 ms 1388 KB Output is partially correct
13 Partially correct 758 ms 1808 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 10 ms 596 KB Output is correct
16 Correct 38 ms 344 KB Output is correct
17 Partially correct 78 ms 600 KB Output is partially correct
18 Partially correct 119 ms 608 KB Output is partially correct
19 Partially correct 301 ms 740 KB Output is partially correct
20 Partially correct 256 ms 1120 KB Output is partially correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 8 ms 344 KB Output is correct
23 Correct 21 ms 344 KB Output is correct
24 Correct 22 ms 344 KB Output is correct
25 Correct 22 ms 344 KB Output is correct
26 Partially correct 147 ms 964 KB Output is partially correct
27 Partially correct 176 ms 972 KB Output is partially correct
28 Partially correct 172 ms 960 KB Output is partially correct
29 Partially correct 267 ms 848 KB Output is partially correct
30 Partially correct 260 ms 1200 KB Output is partially correct
31 Partially correct 281 ms 1472 KB Output is partially correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 26 ms 344 KB Output is correct
35 Correct 24 ms 344 KB Output is correct
36 Correct 23 ms 344 KB Output is correct
37 Partially correct 165 ms 1224 KB Output is partially correct
38 Partially correct 171 ms 968 KB Output is partially correct
39 Partially correct 172 ms 960 KB Output is partially correct
40 Partially correct 259 ms 848 KB Output is partially correct
41 Partially correct 273 ms 740 KB Output is partially correct
42 Partially correct 280 ms 1484 KB Output is partially correct
43 Partially correct 766 ms 1540 KB Output is partially correct
44 Partially correct 771 ms 1608 KB Output is partially correct
45 Partially correct 711 ms 1680 KB Output is partially correct
46 Partially correct 782 ms 1360 KB Output is partially correct
47 Partially correct 710 ms 1568 KB Output is partially correct
48 Partially correct 761 ms 1144 KB Output is partially correct
49 Partially correct 757 ms 1336 KB Output is partially correct
50 Partially correct 745 ms 1772 KB Output is partially correct
51 Partially correct 776 ms 1968 KB Output is partially correct
52 Partially correct 728 ms 1252 KB Output is partially correct
53 Partially correct 798 ms 788 KB Output is partially correct
54 Partially correct 776 ms 2056 KB Output is partially correct
55 Partially correct 769 ms 1960 KB Output is partially correct
56 Partially correct 800 ms 1204 KB Output is partially correct
57 Partially correct 765 ms 1380 KB Output is partially correct
58 Partially correct 735 ms 1588 KB Output is partially correct
59 Partially correct 758 ms 1848 KB Output is partially correct
60 Partially correct 727 ms 1512 KB Output is partially correct
61 Partially correct 730 ms 1276 KB Output is partially correct
62 Partially correct 779 ms 1804 KB Output is partially correct
63 Partially correct 735 ms 1788 KB Output is partially correct
64 Partially correct 715 ms 1080 KB Output is partially correct
65 Partially correct 724 ms 1524 KB Output is partially correct
66 Partially correct 732 ms 1460 KB Output is partially correct
67 Partially correct 721 ms 1540 KB Output is partially correct
68 Partially correct 707 ms 992 KB Output is partially correct
69 Partially correct 737 ms 1440 KB Output is partially correct
70 Partially correct 733 ms 1704 KB Output is partially correct
71 Partially correct 717 ms 1208 KB Output is partially correct
72 Partially correct 728 ms 1484 KB Output is partially correct
73 Partially correct 754 ms 1652 KB Output is partially correct
74 Partially correct 822 ms 1608 KB Output is partially correct
75 Partially correct 729 ms 1104 KB Output is partially correct
76 Partially correct 774 ms 868 KB Output is partially correct
77 Partially correct 731 ms 1384 KB Output is partially correct
78 Partially correct 768 ms 1472 KB Output is partially correct
79 Partially correct 799 ms 1340 KB Output is partially correct
80 Partially correct 740 ms 1364 KB Output is partially correct
81 Partially correct 778 ms 1020 KB Output is partially correct
82 Partially correct 786 ms 1760 KB Output is partially correct