求助WA#6
查看原帖
求助WA#6
1073342
Genshin_ZFYX楼主2024/8/17 22:24
#include<bits/stdc++.h>
using namespace std;
// #define int long long
struct Edge{
    int v,to;
}e[4000005];
int n,m,tot,head[1000005],dp[1000005],d[1000005];
bool vis[1000005];
const int INF=0x3f3f3f3f,mod=100003;
void bfs1()
{
    queue<int>q;
    q.push(1);
    d[1]=1;
    while(!q.empty())
    {
        int t=q.front();q.pop();
        for(int i=head[t];i;i=e[i].to)
        {
            int v=e[i].v;
            if(!d[v])
            {
                d[v]=d[t]+1;
                q.push(v);
            }
        }
    }
}
void bfs2()
{
    queue<int>q;
    q.push(1);
    dp[1]=1;
    while(!q.empty())
    {
        int t=q.front();q.pop();
        for(int i=head[t];i;i=e[i].to)
        {
            int v=e[i].v;
            if(d[v]==d[t]+1)
            {
                if(dp[v]==0)q.push(v);
                dp[v]+=dp[t];dp[v]%=mod;
            }
        }
    }
}
void addedge(int u,int v)
{
    tot++;
    e[tot].v=v;
    e[tot].to=head[u];
    head[u]=tot;
}
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v;cin>>u>>v;
        addedge(u,v);
        addedge(v,u);
    }
    bfs1();bfs2();
    for(int i=1;i<=n;i++)
    cout<<dp[i]<<'\n';
    return 0;
}
2024/8/17 22:24
加载中...