标签:bool tor end oid int code 最短路径 for ons
const int N=1e6+10;
vector<int> g[N];
int dist[N];
bool vis[N];
int cnt[N];
int n,m;
void bfs()
{
queue<int> q;
memset(dist,-1,sizeof dist);
q.push(1);
dist[1]=0;
cnt[1]=1;
while(q.size())
{
int t=q.front();
q.pop();
for(int i=0;i<g[t].size();i++)
{
int j=g[t][i];
if(dist[j] == -1)
{
dist[j] = dist[t]+1;
cnt[j]=cnt[t];
q.push(j);
}
else if(dist[j] == dist[t]+1)
{
cnt[j]=(cnt[j]+cnt[t])%mod;
}
}
}
}
int main()
{
ios;
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
bfs();
for(int i=1;i<=n;i++)
if(dist[i] == -1) cout<<0<<endl;
else cout<<cnt[i]<<endl;
//system("pause");
}
标签:bool tor end oid int code 最短路径 for ons
原文地址:https://www.cnblogs.com/fxh0707/p/13619178.html