Submission #56676542


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)

int N,X,Y;
vector<vector<int>> nodeLists(200010,vector<int>(0));
vector<bool> isReached(200010,false);
deque<int> pathStack; // stackとして使う
// (最後に0からの値を表示するため stack では無理)
bool stop = false; // 探索を止めるためのフラグ

void searchPath(int from, int to) {
    if (!stop) pathStack.push_back(from);
    if (from == to) stop=true;
    isReached.at(from) = true;
    for(int i=0; i<(int)nodeLists.at(from).size(); i++) {
        int toPos = nodeLists.at(from).at(i);
        if (!isReached.at(toPos)) {
            searchPath(toPos, to);
        } 
    }
    if (!stop) pathStack.pop_back();
    return;
}

// これは深さ優先探索でできそう
int main() {
    cin >> N >> X >> Y;
    rep(i,0,N-1) {
        int U,V;
        cin >> U >> V;
        nodeLists.at(U).push_back(V);
        nodeLists.at(V).push_back(U);
    }
    searchPath(X, Y);
    for(int i=0; i<(int)pathStack.size(); i++) {
        cout << pathStack.at(i) << " ";
    }
    cout << endl;
}

Submission Info

Submission Time
Task C - Simple path
User strkgr
Language C++ 20 (gcc 12.2)
Score 300
Code Size 1167 Byte
Status AC
Exec Time 140 ms
Memory 27632 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 27
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt
Case Name Status Exec Time Memory
example_00.txt AC 3 ms 7864 KiB
example_01.txt AC 3 ms 7768 KiB
hand_00.txt AC 3 ms 7788 KiB
hand_01.txt AC 130 ms 27408 KiB
hand_02.txt AC 83 ms 14996 KiB
hand_03.txt AC 132 ms 27632 KiB
hand_04.txt AC 140 ms 27472 KiB
random_00.txt AC 119 ms 14260 KiB
random_01.txt AC 106 ms 14200 KiB
random_02.txt AC 110 ms 14180 KiB
random_03.txt AC 105 ms 14336 KiB
random_04.txt AC 110 ms 14296 KiB
random_05.txt AC 109 ms 14224 KiB
random_06.txt AC 117 ms 14220 KiB
random_07.txt AC 106 ms 14188 KiB
random_08.txt AC 108 ms 14196 KiB
random_09.txt AC 97 ms 15328 KiB
random_10.txt AC 105 ms 14980 KiB
random_11.txt AC 100 ms 15320 KiB
random_12.txt AC 126 ms 24784 KiB
random_13.txt AC 137 ms 23244 KiB
random_14.txt AC 125 ms 24028 KiB
random_15.txt AC 124 ms 25092 KiB
random_16.txt AC 122 ms 23172 KiB
random_17.txt AC 109 ms 18036 KiB
random_18.txt AC 117 ms 21392 KiB
random_19.txt AC 110 ms 18980 KiB