#53844: 看似給答案,實則請你們debug


sams.961120@gmail.com (Bill Cipher)

學校 : 不指定學校
編號 : 308400
來源 : [218.166.145.179]
最後登入時間 :
2025-09-30 22:12:38

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

struct node
{
    vector<int> p, c;
    bool is_legal = false;
    int distance = 0;
};

int main()
{
    int n, m;
    while (cin >> n >> m) {
        // 單向圖
        vector<node> tree(n + 1);
        // tree.resize(n);
        for (int i = 0; i < m; i++) {
            int x, y;
            cin >> x >> y;
            // x -> y
            tree[x].c.push_back(y);
            tree[y].p.push_back(x);
        }

        int s, t;
        cin >> s >> t;
        tree[s].is_legal = true;
        tree[t].is_legal = true;
        // bfs : check legality
        queue<int> q1;
        // vector<bool> visited1(n, false);
        for (int next : tree[t].p) {
            q1.push(next);
        }
        while (!q1.empty()) {
            int now = q1.front();
            q1.pop();
            // if (visited1[now] == true) {
            //     continue;
            // }
            // visited1[now] = true;
            bool Legal = true;
            for (int c : tree[now].c) {
                if (!tree[c].is_legal) {
                    Legal = false;
                    break;
                }
            }
            if (Legal) {
                tree[now].is_legal = true;
                for (int next : tree[now].p) {
                    if (!tree[next].is_legal) {
                        q1.push(next);
                    }
                }
            }
        }

        // bfs : find shortest legal way
        queue<int> q2;
        vector<bool> visited2(n + 1, false);
        q2.push(s); // <- 從起點開始
        bool find = false;
        while (!q2.empty()) {
            int now = q2.front();
            q2.pop();
            if (visited2[now] == true) {
                continue;
            }
            visited2[now] = true;
            if (now == t) {
                find = true;
                break;
            } else {
                for (int next : tree[now].c) {
                    if (tree[next].is_legal) {
                        q2.push(next);
                        if (tree[next].distance == 0) {
                            tree[next].distance = tree[now].distance + 1;
                        }
                    }
                }
            }
        }
        if (find) {
            cout << tree[t].distance << '\n';
        } else {
            cout << -1 << '\n';
        }
    }

    return 0;
}
#53845: Re: 看似給答案,實則請你們debug


sams.961120@gmail.com (Bill Cipher)

學校 : 不指定學校
編號 : 308400
來源 : [218.166.145.179]
最後登入時間 :
2025-09-30 22:12:38

#include
#include
#include

using namespace std;

struct node
{
    vector p, c;
    bool is_legal = false;
    int distance = 0;
};

int main()
{
    int n, m;
    while (cin >> n >> m) {
        // 單向圖
        vector tree(n + 1);
        // tree.resize(n);
        for (int i = 0; i < m; i++) {
            int x, y;
            cin >> x >> y;
            // x -> y
            tree[x].c.push_back(y);
            tree[y].p.push_back(x);
        }

        int s, t;
        cin >> s >> t;
        // tree[s].is_legal = true;
        tree[t].is_legal = true;
        // bfs : check legality
        queue q1;
        // vector visited1(n, false);
        for (int next : tree[t].p) {
            q1.push(next);
        }
        while (!q1.empty()) {
            int now = q1.front();
            q1.pop();
            // if (visited1[now] == true) {
            //     continue;
            // }
            // visited1[now] = true;
            bool Legal = true;
            for (int c : tree[now].c) {
                if (!tree[c].is_legal) {
                    Legal = false;
                    break;
                }
            }
            if (Legal) {
                tree[now].is_legal = true;
                for (int next : tree[now].p) {
                    if (!tree[next].is_legal) {
                        q1.push(next);
                    }
                }
            }
        }

        // bfs : find shortest legal way
        queue q2;
        vector visited2(n + 1, false);
        q2.push(s); // <- 從起點開始
        bool find = false;
        while (!q2.empty()) {
            int now = q2.front();
            q2.pop();
            if (visited2[now] == true) {
                continue;
            }
            visited2[now] = true;
            if (now == t) {
                find = true;
                break;
            } else {
                for (int next : tree[now].c) {
                    if (tree[next].is_legal) {
                        q2.push(next);
                        if (tree[next].distance == 0) {
                            tree[next].distance = tree[now].distance + 1;
                        }
                    }
                }
            }
        }
        if (find) {
            cout << tree[t].distance << '\n';
        } else {
            cout << -1 << '\n';
        }
    }

    return 0;
}

題目理解很重要啊!!!
給個測資想想(測資二加強版)
7 8
1 2
1 3
3 4
4 5
2 6
6 5
6 7
2 5
1 5
雖然應該會過,但注意節點2發生了什麼改變...