莫名RE 求助大佬
查看原帖
莫名RE 求助大佬
472628
Mandel520楼主2022/3/30 11:56

样例和第一个测试数据欸有问题, 但是后 4 个数据 RE 我稍微检测了以下可能导致 RE 的原因:

(1) 数组不够大. 显然下面的程序中的数组大小是达到题目要求的, 因此应该不是这个问题

(2) 忘了设定第一篇文章为已经遍历. 观察下面的程序 dfs()bfs() 都在输出一篇文章后立即将这篇文章标记为已经遍历, 而且第一个输出的就是第一篇文章, 因此应该也不是这个问题

(3) 陷入循环遍历. 我测试了"4 4 1 2 2 3 3 4 4 2"这组 hack 数据, 发现并不是这个问题

(4) DFS()递归过深. 我写的 dfs() 不会重复遍历标记过的点, 而且其它部分和标准dfs()好像也没什么区别, 因此也不想是递归过深导致的问题

因为以上原因都已经被基本排除, 因此我是在找不到导致我程序 RE 的真实原因, 只好向各位大佬求助

完整程序入下:

#include<bits/stdc++.h>
using namespace std;

int n, m; //n篇文章, m个引用
bool visited[100005]; //标记已经遍历过的文章
vector<int> articles[100005];
queue<int> q;

struct relation{
    int art, ref; //文章和引用
};

relation relations[100005];

bool cmp(relation a, relation b){ //引用编号较小的排在前面
    return a.ref < b.ref;
}

void dfs(int x){ //深度优先遍历
    cout << x << ' ';
    visited[x] = 1;
    for (int i = 0; i < articles[x].size(); i++){
        if (!visited[articles[x][i]]){
            dfs(articles[x][i]);
        }
    }
}

void bfs(int x){ //广度优先遍历
    q.push(x);
    while(!q.empty()){
        int x = q.front();
        q.pop();
        cout << x << ' ';
        visited[x] = 1;
        for (int i = 0; i < articles[x].size(); i++){
            if (!visited[articles[x][i]]){
                q.push(articles[x][i]);
                visited[articles[x][i]] = 1;
            }
        }
    }
}

int main(){
    cin >> n >> m;
    for (int i = 1; i <= m; i++){ //读入所有关系
        cin >> relations[i].art >> relations[i].ref;
    }

    sort(relations + 1, relations + m + 1, cmp); //将编号从小到大排序

    for (int i = 1; i <= m; i++){ //储存所有关系
        articles[relations[i].art].push_back(relations[i].ref); 
    } 

    dfs(1); //深度优先遍历
    cout << endl;
    memset(visited, 0, sizeof(visited));
    bfs(1); //广度优先遍历
    cout << endl;

    return 0;
}
2022/3/30 11:56
加载中...