P3883 91分求助

题目总版

chenkaihuan @ 2024-11-05 08:57:11

有没有大佬知道为什么我第一个空总是过不了啊

#include <bits/stdc++.h>
using namespace std;
bool a[101][101];

struct node {
    int s;
    int d;
};

int main() {
    int n;
    cin >> n;
    if (n==1) {
        cout<<1<<endl;
        cout<<1<<endl;
        cout<<0;
        return 0;

    }
    for (int i = 0; i < n - 1; i++) {
        int u, b;
        cin >> u >> b;
        a[u][b] = 1;
    }

    int num = 0;
    int dn[1005] = {};
    node q;
    q.s = 1;
    q.d = 1;
    queue <node> b;
    b.push(q);
    int x, y;
    cin >> x >> y;

    while (num < n) {
        dn[b.front().d]++;
        num++;

        for (int i = 1; i <= n; i++) {
            if (a[b.front().s][i]) {
                q.s = i;
                q.d = b.front().d + 1;
                b.push(q);
            }
        }

        b.pop();
    }

    cout << q.d << endl;
    int ans = -1;

    for (int i = 0; i < 1005; i++) {
        ans = max(ans, dn[i]);
    }

    cout << ans << endl;
    bool step[1005];
    queue <node> b2;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[i][j]) {
                a[j][i] = 1;
            }

        }
    }

    /*
    for (int i = 1; i <= n; i++) {
        cout << endl;

        for (int j = 1; j <= n; j++) {
            cout << a[i][j] << ' ';

        }

    }
    */
    q.s = x;
    q.d = 0;
    b2.push(q);
    step[x] = 1;

    while (b2.front().s != y) {

        for (int i = 1; i <= n; i++) {
            if (a[b2.front().s][i] && !step[i]) {
                step[i] = 1;
                q.s = i;

                if (b2.front().s > i)
                    q.d = b2.front().d + 2;
                else
                    q.d = b2.front().d + 1;

                b2.push(q);
            }
        }

        b2.pop();
    }

    cout << b2.front().d;
    return 0;
}

|