Submission #68156289


Source Code Expand

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

void solve() {
    int n, m, x, y;
    cin >> n >> m >> x >> y, x--, y--;
    vector<vector<int>> grf(n);
    for (int i = 0, u, v; i < m; i++) cin >> u >> v, u--, v--, grf[u].push_back(v), grf[v].push_back(u);
    for (auto &adj : grf) sort(adj.begin(), adj.end());
    vector<bool> vis(n);
    vis[x] = true;
    vector<int> ans = {x};
    int now = x;

    auto reach = [&](int s, int t) {
        if (vis[s]) return false;
        if (s == t) return true;
        vector<bool> see(n);
        queue<int> q;
        q.push(s), see[s] = true;
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            for (int to : grf[now]) {
                if (see[to] || vis[to]) continue;
                if (to == t) return true;
                see[to] = true, q.push(to);
            }
        }
        return false;
    };

    while (now != y)
        for (int to : grf[now])
            if (!vis[to] && reach(to, y)) {
                vis[to] = true, now = to, ans.push_back(now);
                break;
            }

    for (int x : ans) cout << x + 1 << " ";
    cout << endl;
}

int main() {
    int t;
    for (cin >> t; t; t--) solve();
}

Submission Info

Submission Time
Task E - A Path in A Dictionary
User lhx1048576
Language C++ 20 (gcc 12.2)
Score 475
Code Size 1272 Byte
Status AC
Exec Time 63 ms
Memory 4448 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 1
AC × 43
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.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, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3424 KiB
hand_00.txt AC 16 ms 4204 KiB
hand_01.txt AC 5 ms 3544 KiB
hand_02.txt AC 63 ms 4176 KiB
hand_03.txt AC 5 ms 3548 KiB
hand_04.txt AC 13 ms 3644 KiB
hand_05.txt AC 17 ms 4292 KiB
hand_06.txt AC 45 ms 4448 KiB
hand_07.txt AC 1 ms 3516 KiB
hand_08.txt AC 16 ms 4100 KiB
hand_09.txt AC 38 ms 4260 KiB
hand_10.txt AC 6 ms 3628 KiB
hand_11.txt AC 2 ms 3452 KiB
random_00.txt AC 1 ms 3576 KiB
random_01.txt AC 1 ms 3496 KiB
random_02.txt AC 1 ms 3496 KiB
random_03.txt AC 2 ms 3504 KiB
random_04.txt AC 2 ms 3532 KiB
random_05.txt AC 2 ms 3496 KiB
random_06.txt AC 1 ms 3432 KiB
random_07.txt AC 1 ms 3552 KiB
random_08.txt AC 2 ms 3544 KiB
random_09.txt AC 4 ms 3540 KiB
random_10.txt AC 6 ms 3492 KiB
random_11.txt AC 7 ms 3520 KiB
random_12.txt AC 1 ms 3468 KiB
random_13.txt AC 1 ms 3560 KiB
random_14.txt AC 3 ms 3396 KiB
random_15.txt AC 7 ms 3584 KiB
random_16.txt AC 11 ms 3568 KiB
random_17.txt AC 15 ms 3592 KiB
random_18.txt AC 1 ms 3484 KiB
random_19.txt AC 1 ms 3496 KiB
random_20.txt AC 17 ms 4008 KiB
random_21.txt AC 3 ms 3604 KiB
random_22.txt AC 16 ms 3928 KiB
random_23.txt AC 51 ms 4292 KiB
random_24.txt AC 1 ms 3612 KiB
random_25.txt AC 2 ms 3584 KiB
random_26.txt AC 19 ms 4256 KiB
random_27.txt AC 17 ms 4256 KiB
random_28.txt AC 16 ms 4448 KiB
random_29.txt AC 16 ms 4292 KiB