话说这题为啥WA了?
  • 板块灌水区
  • 楼主梦回江南
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/3/7 23:21
  • 上次更新2023/10/28 07:03:28
查看原帖
话说这题为啥WA了?
492676
梦回江南楼主2022/3/7 23:21

题目

#include <queue>
#include <vector>
#include <cstdio>
#include <memory.h>
#define maxn 1000005
using namespace std;

int n, m;
bool b[maxn];
queue <int> q;
vector <int> p[maxn];

// 输入
void input() {
    scanf("%d %d", &n, &m);
    int u, v;
    for(int i = 1; i <= m; i++) {
        scanf("%d %d", &u, &v);
        p[u].push_back(v);
    }
    return ;
}

// 深度优先遍历
void dfs(int x) {
    printf("%d ", x);
    for(int i = 0, sz = p[x].size(); i < sz; i++)
        if(!b[p[x][i]]) {
            b[p[x][i]] = true;
            dfs(p[x][i]);
        }
    return ;
}

// 广度优先遍历
void bfs() {
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        printf("%d ", x);
        for(int i = 0, sz = p[x].size(); i < sz; i++)
            if(!b[p[x][i]]) {
                b[p[x][i]] = true;
                q.push(p[x][i]);
            }
        continue;
    }
    return ;
}

int main() {
    input();
    b[1] = true;
    dfs(1);
    puts("");
    memset(b, false, sizeof(b));
    b[1] = true;
    q.push(1);
    bfs();
    return 0;
}
2022/3/7 23:21
加载中...