Submission #24506510


Source Code Expand

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
// __int128->GNU G++ 17 9.2.0 Codeforces (but doesnot use with cout cin And cant take direct input use normal datatype the typecast ) 
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


//https://codeforces.com/blog/entry/20935
//https://www.hackerrank.com/contests/mit-open-20-finals/challenges/fractions-arent-tough-or-are-they
//
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;


// ll power(ll x,ll y){
//     if(y==0)
//         return 1;
//     ll res=power(x,y/2);
//     res=(res*res);
//     if(y&1)
//         res=(res*x);
//     return res;
// }
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}

void solve(){
    int n,m;
    cin>>n>>m;
    vector<vector<int>> gr(n+1);
    int a,b;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        gr[a].pb(b);
        gr[b].pb(a);
    }
    vector<int> dis(n+1),vis(n+1,0);
    vector<vector<int>> v(n+1);
    dis[1]=0;
    queue<int> q;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int no=q.front();
        q.pop();
        for(int i:gr[no]){
            if(vis[i]==0){
                q.push(i);
                dis[i]=dis[no]+1;
                v[dis[i]].pb(i);
                vis[i]=1;
            }
        }
    }
    if(vis[n]==0){
        cout<<0<<endl;
        return;
    }
    vector<ll> dp(n+1,0);
    for(int i:v[1])
        dp[i]=1;
    for(int i=2;i<=dis[n];i++){
        for(int j:v[i])
        {
            for(int k:gr[j])
                if(dis[k]==i-1){
                    dp[j]+=dp[k];
                    dp[j]%=M;
                }
        }  
    }
    cout<<dp[n]<<endl;
}   
int main(){ 
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //sieve();
    
    int test=1; 
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}

Submission Info

Submission Time
Task D - Number of Shortest paths
User rounak_0501
Language C++ (GCC 9.2.1)
Score 400
Code Size 2584 Byte
Status AC
Exec Time 145 ms
Memory 28220 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 30
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name Status Exec Time Memory
random_01.txt AC 121 ms 22128 KiB
random_02.txt AC 86 ms 15808 KiB
random_03.txt AC 75 ms 19872 KiB
random_04.txt AC 46 ms 7580 KiB
random_05.txt AC 114 ms 21932 KiB
random_06.txt AC 78 ms 13684 KiB
random_07.txt AC 42 ms 17660 KiB
random_08.txt AC 19 ms 8664 KiB
random_09.txt AC 108 ms 21616 KiB
random_10.txt AC 84 ms 13956 KiB
random_11.txt AC 43 ms 17876 KiB
random_12.txt AC 12 ms 9168 KiB
random_13.txt AC 112 ms 22052 KiB
random_14.txt AC 103 ms 20116 KiB
random_15.txt AC 90 ms 19868 KiB
random_16.txt AC 48 ms 13388 KiB
random_17.txt AC 111 ms 22004 KiB
random_18.txt AC 72 ms 11744 KiB
random_19.txt AC 96 ms 21472 KiB
random_20.txt AC 41 ms 10256 KiB
random_31.txt AC 37 ms 6104 KiB
random_32.txt AC 65 ms 14152 KiB
random_33.txt AC 145 ms 28220 KiB
random_34.txt AC 83 ms 14224 KiB
random_35.txt AC 84 ms 14208 KiB
random_36.txt AC 111 ms 20152 KiB
sample_01.txt AC 2 ms 3524 KiB
sample_02.txt AC 2 ms 3584 KiB
sample_03.txt AC 2 ms 3524 KiB
sample_04.txt AC 2 ms 3600 KiB