60pts 求助
查看原帖
60pts 求助
235786
Alone0213楼主2022/5/25 19:06

用的链式前向星存图,但是#2,#4测试点深搜部分错误

感觉是没有满足题目要求从大到小的遍历顺序(也不一定是这个)

求助各位大佬

错误信息:#2:第1行第57036列 read 2,expected 1. #4:第1行第39007列 read 1,expexted 8.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int N=1e5+18;
const int M=1e6+89;

struct node{
    int ne,to;
}g[M];
int h[M];bool p[N],p1[N];
queue<int> q;

struct e{
    int u,v;
}ed[M];

int n,m,cnt;
int a,b;
void add(int x,int t){
    g[++cnt].ne=h[x];
    g[cnt].to=t;
    h[x]=cnt;
}
bool cmp(e a,e b){
    if(a.v!=b.v) return a.v>b.v;//终点大的先加,因为链式前向星的遍历顺序
    return a.u<b.u;
}//sort as destinations from B to S
void dfs(int u){
    printf("%d ",u);
    p[u]=1;
    for(int i=h[u]; i; i=g[i].ne){
        int v=g[i].to;
        if(!p[v]) dfs(v);
    }
}
void bfs(int s){
    q.push(s); 
    p1[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=h[u]; i; i=g[i].ne){
            int v=g[i].to;
            if(!p1[v]){
                q.push(v);
                p1[v]=1;
            }
        }
        printf("%d ",u);
    }
}
signed main(){
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
        scanf("%d%d",&ed[i].u,&ed[i].v);
    
    sort(ed+1,ed+m,cmp);//边排序

    for(int i=1; i<=m; i++) add(ed[i].u,ed[i].v);//建图
    dfs(1);
    puts("");
    bfs(1);
    return 0;
}
2022/5/25 19:06
加载中...