3 条题解

  • 0
    @ 2026-2-12 12:15:20
    # include <cctype>
    # include <cstdio>
    # include <cstring>
    # include <vector>
    
    namespace Main {
        namespace Source {
            typedef short unsigned int hu;
            typedef std::vector<hu> vector;
            static inline const hu min(const hu a, const hu b) { return b < a ? b : a; }
            static inline const void amin(hu& a, const hu b) { a = min(a, b); }
        }
        using namespace Source;
        enum { M = 100, N = 500 };
        static hu m, n;
        static vector bus[M];
        static vector out[N + 1];
        static hu ans[N + 1];
        static inline const void read_bus(vector& bus) {
            char u;
            while (not isdigit(u = getchar()));
            while (true) {
                hu t(0);
                while (t = t * 10 + (u - '0'), isdigit(u = getchar()));
                bus.push_back(t);
                bool brk(false);
                do { if (u == '\n' or u == EOF) { brk = true; break; } }
                while (not isdigit(u = getchar()));
                if (brk) break;
            }
            for (hu i(0); i < bus.size(); ++i) for (hu j(i); j < bus.size(); ++j)
                out[bus[i]].push_back(bus[j]);
        }
        static inline const void main() {
            scanf("%hu%hu", &m, &n);
            for (hu i(0); i < m; ++i) read_bus(bus[i]);
            memset(ans, 0x7f, sizeof ans), ans[1] = 0;
            for (hu i(0); i < n; ++i)
                for (hu j(1); j <= n; ++j)
                    for (hu k(0); k < out[j].size(); ++k)
                        amin(ans[out[j][k]], ans[j] + 1);
            if (ans[n] == ans[0]) puts("NO");
            else printf("%hu\n", ans[n] ? ans[n] - 1 : 0);
        }
    }
    
    signed int main() { Main::main(); return 0; }
    
    
    
    

    信息

    ID
    378
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    3
    已通过
    1
    上传者