接着wa呀wa呀wa
查看原帖
接着wa呀wa呀wa
804115
yzm0325楼主2023/6/3 21:27

还是WA0pts

#include <bits/stdc++.h>
using namespace std;
int n, m;
vector <int> p[100010];
queue <int> q;
bool vis[100010];
void solve(int x) {
    cout << x << " ";
    for(int i = 0; i < p[x].size(); i++)
        if(!vis[p[x][i]]) {
            vis[p[x][i]] = 1;
            solve(p[x][i]);
        }
}
int main() {
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        p[x].push_back(y);
    }
    vis[1] = 1;
    solve(1);
    cout << "\n";
    // ----------------------------------------
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    q.push(1);
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        cout << x << " ";
        for(int i = 0; i < p[x].size(); i++)
            if(!vis[p[x][i]]) {
                vis[p[x][i]] = 1;
                q.push(p[x][i]);
            }
    }
    return 0;
}
2023/6/3 21:27
加载中...