【題解】CSES 1681 Game Routes

【題目敘述】https://cses.fi/problemset/task/1681/

#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
 
int n, m, a, b, in[100005], ans[100005], mod = 1e9+7;
vector <int> v[100005];
 
int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++){
        cin >> a >> b;
        v[a].push_back(b);
        in[b]++;
    }
    queue <int> q;
    for (int i = 1; i <= n; i++){
        if (in[i] == 0) q.push(i);
    }
    ans[1] = 1;
    while (!q.empty()){
        int now = q.front();
        q.pop();
        for (int i:v[now]){
            ans[i] += ans[now];
            ans[i] %= mod;
            in[i]--;
            if (in[i] == 0) q.push(i);
        }
    }
    cout << ans[n] << "\n";
}
分享本文 Share with friends