Untitled

 avatar
meda
c_cpp
a month ago
1.5 kB
3
Indexable
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
template<class T>
void printff(vector<T>& v) {
  for (auto k : v) cout << k << " ";
  cout << endl;
}
const ll MOD = 998244353;
int mult(ll a, ll b) { return (a * b) % MOD; }
int add(ll a, ll b) { return (a + b)%MOD; }
int sub(ll a, ll b) { return (a - b + MOD)%MOD; }
int binpow (ll a, ll b){
    a %= MOD;
    ll res = 1;
    while(b > 0){if(b & 1) res = mult(res, a); a = mult(a, a); b >>= 1; }
    return res;
}
int divide(ll a, ll b) { return mult(a, binpow(b, MOD - 2)); }
void SOLVE() {
  int n, m; cin >> n >> m;
  vector<vector<int>> graph(n + 1);
  vector<int> in_degree(n + 1);
  for(int i = 0, u , v; i < m; i++){
    cin >> u >> v;
    graph[u].push_back(v);
    in_degree[v]++;
  }
  vector<int> bfs;
  queue<int> q;

  for(int i = 1; i <= n; i++)
    if(in_degree[i] == 0) q.push(i);
  
  while(!q.empty()){
    int front = q.front();
    q.pop();
    bfs.push_back(front);
    for(auto child : graph[front]){
        if(--in_degree[child] == 0){
            q.push(child);
        }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<ll> dp(n + 1);
  for(auto node : bfs){
    ll sum = 1;
    for(auto child : graph[node]){
        sum = add(sum, dp[child]);
    }
    dp[node] = sum;
  }
  cout << dp[1] - 1 << endl;
}
int main() {
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL); 
  cout.tie(NULL);
  int tc = 1; //cin >> tc;
  while(tc--) SOLVE();
  return 0;
}
Leave a Comment