답안 #732499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
732499 2023-04-29T05:04:57 Z Programmer123 Through Another Maze Darkly (CCO21_day1problem3) C++17
0 / 25
9000 ms 8296 KB
//Do initial DFSs to naively find first results.
//When return to root after N runs, all lasers should point to parent.
#include <bits/stdc++.h>
#define int long long int
#define init(x, s) x = new std::remove_pointer<decltype(x)>::type[s]
std::vector<int> traversal;
std::vector<int> *connections;
int *walkTime;
int *parent;
int *laserIndex;
bool *seen;
int traverse(int node) {
    walkTime[node] = 0;
    for (auto sub: connections[node]) {
        walkTime[node] += 2 + traverse(sub);
    }
    return walkTime[node];
}
int walk(int node, int steps) {
    if (steps == 0) return node;
    for (auto sub: connections[node]) {
        if (walkTime[sub] + 2 == steps) return node;
        if (walkTime[sub] + 2 <= steps) {
            steps -= walkTime[sub] + 2;
        } else {
            return walk(sub, steps - 1);
        }
    }
    assert(false);
}
signed main() {
    int N, Q;
    std::cin >> N >> Q;
    init(connections, N);
    init(walkTime, N);
    init(parent, N);
    init(laserIndex, N);
    init(seen, N);
    for (int i = 0; i < N; ++i) {
        seen[i] = false;
        laserIndex[i] = 0;
        int k;
        std::cin >> k;
        for (int j = 0; j < k; ++j) {
            int a;
            std::cin >> a;
            a--;
            connections[i].push_back(a);
        }
    }
    int x;
    std::cin >> x;
    int current = 0;
    int remaining = N;
    while (current != 0 || remaining > 0) {
        if (x == 0) {
            std::cout << current + 1 << std::endl;
            return 0;
        }
        if (!seen[current]) {
            seen[current] = true;
            remaining--;
        }
        laserIndex[current] = (laserIndex[current] + 1) % connections[current].size();
        //        traversal.push_back(current);
        current = connections[current][laserIndex[current]];
        x--;
    }
#ifdef LOCAL
    for (auto n: traversal) {
        std::cout << n + 1 << " ";
    }
    std::cout << std::endl;
#endif
    for (int i = 0; i < N; ++i) {
        std::vector<int> subtrees;
        int pIndex = laserIndex[i];
        for (int j = pIndex + 1; j < connections[i].size(); ++j) {
            subtrees.push_back(connections[i][j]);
        }
        for (int j = 0; j < pIndex; j++) {
            subtrees.push_back(connections[i][j]);
        }
        if (i == 0) subtrees.push_back(connections[i][laserIndex[i]]);
        connections[i] = subtrees;
    }
    traverse(0);
    //for (int i = 0; i < Q; ++i) {
    //    int x;
    //    std::cin >> x;
    //        if (x < traversal.size()) {
    //            std::cout << traversal[x] + 1 << std::endl;
    //        } else {
    //            x -= traversal.size();
    x = x % walkTime[0];
    std::cout << walk(0, x) + 1 << std::endl;
    //    }
    //}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:78:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         for (int j = pIndex + 1; j < connections[i].size(); ++j) {
      |                                  ~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 996 KB Output is correct
2 Correct 105 ms 4332 KB Output is correct
3 Execution timed out 9044 ms 8296 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -