답안 #960320

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960320 2024-04-10T09:07:45 Z TAhmed33 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
770 ms 1884 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) {
            assert(pos == -1);
            pos = i;
        }
    }
    if (pos == -1) return x;
    int u = get(tree[pos][0]), v = get(tree[pos][1]);
    if (p[pos] != -1 && find(tree[p[pos]].begin(), tree[p[pos]].end(), u) == tree[p[pos]].end()) swap(u, v);
    auto g = find(x.begin(), x.end(), pos) - x.begin();
    x.erase(x.begin() + g);
    g = find(x.begin(), x.end(), u) - x.begin(); g++;
    x.insert(x.begin() + g, pos);
    return x;   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 180 ms 1332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Correct 113 ms 612 KB Output is correct
4 Correct 354 ms 1368 KB Output is correct
5 Correct 749 ms 1652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 30 ms 344 KB Output is correct
3 Correct 137 ms 856 KB Output is correct
4 Correct 362 ms 848 KB Output is correct
5 Correct 746 ms 1884 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Incorrect 4 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 35 ms 344 KB Output is correct
3 Correct 129 ms 856 KB Output is correct
4 Correct 385 ms 856 KB Output is correct
5 Correct 770 ms 1564 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Incorrect 5 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Partially correct 123 ms 712 KB Output is partially correct
4 Partially correct 352 ms 868 KB Output is partially correct
5 Partially correct 751 ms 1380 KB Output is partially correct
6 Correct 6 ms 344 KB Output is correct
7 Incorrect 4 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -