比赛 20251001国庆欢乐赛1 评测结果 WWWMMMEEEE
题目名称 信使 最终得分 0
用户昵称 xxz 运行时间 2.449 s
代码语言 C++ 内存使用 149.10 MiB
提交时间 2025-10-01 11:17:52
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,mod,q,u,v,d,x,y,ans;
struct node{
    int x,step;
}c;
vector<int>g[555];
void bfs(){
    queue<node>q;
    q.push({u,0});
    while(!q.empty()){
        c=q.front();q.pop();
        if(c.step==d){
            if(c.x==v){
                ans++;
                ans%=mod;
            }
            continue;
        }
        x=c.x;
        for(int i=0;i<g[x].size();i++){
            y=g[x][i];
            q.push({y,c.step+1});
        }
    }
    return;
}
signed main(){
    freopen("messenger.in","r",stdin);freopen("messenger.out","w",stdout);
    scanf("%lld%lld%lld",&n,&m,&mod);
    for(int i=i;i<=m;i++){
        scanf("%lld%lld",&x,&y);
        g[x].push_back(y);
    }
    scanf("%lld",&q);
    while(q--){
        ans=0;
        scanf("%lld%lld%lld",&u,&v,&d);
        bfs();
        printf("%lld\n",ans%mod);
    }
    return 0;
}