Submission #960366

# Submission time Handle Problem Language Result Execution time Memory
960366 2024-04-10T10:14:06 Z TAhmed33 Longest Trip (IOI23_longesttrip) C++17
60 / 100
886 ms 2100 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]);
    }
    if (p[pos] == -1 && find(adj[pos].begin(), adj[pos].end(), u) == adj[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 0 ms 344 KB Output is correct
2 Correct 152 ms 1632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 116 ms 604 KB Output is correct
4 Correct 355 ms 1144 KB Output is correct
5 Correct 747 ms 1376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 20 ms 344 KB Output is correct
3 Correct 133 ms 856 KB Output is correct
4 Correct 379 ms 1228 KB Output is correct
5 Correct 736 ms 1576 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 19 ms 544 KB Output is correct
8 Correct 127 ms 856 KB Output is correct
9 Correct 259 ms 1368 KB Output is correct
10 Correct 762 ms 1152 KB Output is correct
11 Correct 793 ms 1372 KB Output is correct
12 Correct 755 ms 1240 KB Output is correct
13 Correct 774 ms 1376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Correct 151 ms 852 KB Output is correct
4 Correct 384 ms 1484 KB Output is correct
5 Correct 702 ms 1368 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 24 ms 344 KB Output is correct
8 Correct 131 ms 856 KB Output is correct
9 Correct 304 ms 1372 KB Output is correct
10 Correct 760 ms 1160 KB Output is correct
11 Correct 741 ms 1280 KB Output is correct
12 Correct 767 ms 1388 KB Output is correct
13 Correct 769 ms 1644 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 13 ms 344 KB Output is correct
16 Correct 38 ms 344 KB Output is correct
17 Correct 82 ms 856 KB Output is correct
18 Correct 126 ms 600 KB Output is correct
19 Correct 269 ms 996 KB Output is correct
20 Correct 243 ms 1624 KB Output is correct
21 Correct 767 ms 1676 KB Output is correct
22 Correct 768 ms 1700 KB Output is correct
23 Correct 728 ms 1108 KB Output is correct
24 Correct 724 ms 1644 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 9 ms 344 KB Output is correct
27 Correct 23 ms 344 KB Output is correct
28 Correct 21 ms 344 KB Output is correct
29 Correct 19 ms 344 KB Output is correct
30 Correct 159 ms 1112 KB Output is correct
31 Correct 164 ms 972 KB Output is correct
32 Correct 146 ms 856 KB Output is correct
33 Correct 257 ms 848 KB Output is correct
34 Correct 266 ms 1124 KB Output is correct
35 Correct 260 ms 1112 KB Output is correct
36 Correct 713 ms 1120 KB Output is correct
37 Correct 761 ms 1364 KB Output is correct
38 Correct 799 ms 1356 KB Output is correct
39 Correct 723 ms 1848 KB Output is correct
40 Correct 732 ms 1660 KB Output is correct
41 Correct 746 ms 1264 KB Output is correct
42 Correct 825 ms 1648 KB Output is correct
43 Correct 713 ms 1200 KB Output is correct
44 Correct 765 ms 1520 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 10 ms 596 KB Output is correct
47 Correct 22 ms 344 KB Output is correct
48 Correct 20 ms 344 KB Output is correct
49 Correct 24 ms 344 KB Output is correct
50 Correct 173 ms 972 KB Output is correct
51 Correct 174 ms 856 KB Output is correct
52 Correct 168 ms 596 KB Output is correct
53 Correct 311 ms 740 KB Output is correct
54 Correct 245 ms 1368 KB Output is correct
55 Correct 268 ms 984 KB Output is correct
56 Correct 741 ms 1148 KB Output is correct
57 Correct 767 ms 1776 KB Output is correct
58 Correct 718 ms 1612 KB Output is correct
59 Correct 684 ms 1720 KB Output is correct
60 Correct 750 ms 1820 KB Output is correct
61 Correct 764 ms 1476 KB Output is correct
62 Correct 727 ms 1256 KB Output is correct
63 Correct 708 ms 1472 KB Output is correct
64 Correct 694 ms 1548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 21 ms 340 KB Output is correct
3 Partially correct 127 ms 600 KB Output is partially correct
4 Partially correct 346 ms 1012 KB Output is partially correct
5 Partially correct 736 ms 1320 KB Output is partially correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 26 ms 344 KB Output is correct
8 Partially correct 121 ms 856 KB Output is partially correct
9 Partially correct 258 ms 856 KB Output is partially correct
10 Partially correct 762 ms 1260 KB Output is partially correct
11 Partially correct 734 ms 1648 KB Output is partially correct
12 Partially correct 727 ms 1776 KB Output is partially correct
13 Partially correct 742 ms 1880 KB Output is partially correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 40 ms 344 KB Output is correct
17 Partially correct 80 ms 600 KB Output is partially correct
18 Partially correct 110 ms 600 KB Output is partially correct
19 Partially correct 310 ms 716 KB Output is partially correct
20 Partially correct 260 ms 1128 KB Output is partially correct
21 Correct 12 ms 340 KB Output is correct
22 Correct 11 ms 344 KB Output is correct
23 Correct 27 ms 344 KB Output is correct
24 Correct 25 ms 344 KB Output is correct
25 Correct 20 ms 344 KB Output is correct
26 Partially correct 198 ms 1112 KB Output is partially correct
27 Partially correct 178 ms 856 KB Output is partially correct
28 Partially correct 179 ms 856 KB Output is partially correct
29 Partially correct 269 ms 1128 KB Output is partially correct
30 Partially correct 267 ms 876 KB Output is partially correct
31 Partially correct 276 ms 856 KB Output is partially correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 10 ms 344 KB Output is correct
34 Correct 22 ms 344 KB Output is correct
35 Correct 32 ms 344 KB Output is correct
36 Correct 28 ms 344 KB Output is correct
37 Partially correct 166 ms 860 KB Output is partially correct
38 Partially correct 153 ms 1112 KB Output is partially correct
39 Partially correct 217 ms 604 KB Output is partially correct
40 Partially correct 256 ms 1468 KB Output is partially correct
41 Partially correct 262 ms 1116 KB Output is partially correct
42 Partially correct 272 ms 976 KB Output is partially correct
43 Partially correct 736 ms 1416 KB Output is partially correct
44 Partially correct 759 ms 1480 KB Output is partially correct
45 Partially correct 705 ms 1156 KB Output is partially correct
46 Partially correct 715 ms 1652 KB Output is partially correct
47 Partially correct 747 ms 1560 KB Output is partially correct
48 Partially correct 749 ms 1116 KB Output is partially correct
49 Partially correct 738 ms 1408 KB Output is partially correct
50 Partially correct 788 ms 1580 KB Output is partially correct
51 Partially correct 886 ms 1688 KB Output is partially correct
52 Partially correct 773 ms 1108 KB Output is partially correct
53 Partially correct 732 ms 1236 KB Output is partially correct
54 Partially correct 727 ms 1648 KB Output is partially correct
55 Partially correct 789 ms 1440 KB Output is partially correct
56 Partially correct 813 ms 1316 KB Output is partially correct
57 Partially correct 781 ms 1536 KB Output is partially correct
58 Partially correct 738 ms 1464 KB Output is partially correct
59 Partially correct 704 ms 1844 KB Output is partially correct
60 Partially correct 726 ms 1232 KB Output is partially correct
61 Partially correct 744 ms 1532 KB Output is partially correct
62 Partially correct 761 ms 1764 KB Output is partially correct
63 Partially correct 741 ms 1632 KB Output is partially correct
64 Partially correct 786 ms 1468 KB Output is partially correct
65 Partially correct 737 ms 1808 KB Output is partially correct
66 Partially correct 727 ms 1436 KB Output is partially correct
67 Partially correct 723 ms 1048 KB Output is partially correct
68 Partially correct 767 ms 1064 KB Output is partially correct
69 Partially correct 780 ms 1784 KB Output is partially correct
70 Partially correct 707 ms 1952 KB Output is partially correct
71 Partially correct 754 ms 1592 KB Output is partially correct
72 Partially correct 703 ms 1676 KB Output is partially correct
73 Partially correct 752 ms 1684 KB Output is partially correct
74 Partially correct 713 ms 1344 KB Output is partially correct
75 Partially correct 759 ms 1264 KB Output is partially correct
76 Partially correct 737 ms 1368 KB Output is partially correct
77 Partially correct 742 ms 1836 KB Output is partially correct
78 Partially correct 702 ms 1484 KB Output is partially correct
79 Partially correct 670 ms 1460 KB Output is partially correct
80 Partially correct 752 ms 2100 KB Output is partially correct
81 Partially correct 749 ms 1404 KB Output is partially correct
82 Partially correct 728 ms 1108 KB Output is partially correct