제출 #689615

#제출 시각아이디문제언어결과실행 시간메모리
689615tvladm2009Shell (info1cup18_shell)C++17
100 / 100
437 ms78976 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 1e6 + 7;
const int M = (int) 1e9 + 7;
int a[N], special[N], degree[N], dp[N], order[N];
vector<int> in[N], out[N];

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  int n, m, p;
  cin >> n >> m >> p;
  for (int i = 1; i <= p; i++) {
    cin >> a[i];
    special[a[i]] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    in[y].push_back(x);
    out[x].push_back(y);
    degree[y]++;
  }
  queue<int> q;
  vector<int> depth(n + 1);
  for (int i = 1; i <= n; i++) {
    if (degree[i] == 0) {
      q.push(i);
      depth[i] = 1;
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto &v : out[u]) {
      degree[v]--;
      if (degree[v] == 0) {
        depth[v] = depth[u] + 1;
        q.push(v);
      }
    }
  }
  for (int i = 2; i <= p; i++) {
    if (depth[a[i]] <= depth[a[i - 1]]) {
      cout << "0";
      return 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    order[i] = i;
  }
  sort(order + 1, order + n + 1, [&](int i, int j) {
    return depth[i] < depth[j];
  });
  int last = 0;
  for (int i = 1; i <= n; i++) {
    if (order[i] != 1) {
      if (last != 0) {
        for (auto &v : in[order[i]]) {
          if (depth[v] > depth[last] || v == last) {
            dp[order[i]] += dp[v];
            if (dp[order[i]] >= M) {
              dp[order[i]] -= M;
            }
          }
        }
      }
      if (special[order[i]]) {
        last = order[i];
      }
    } else {
      if (last == 0) {
        dp[1] = 1;
      }
      last = 1;
    }
  }
  cout << dp[n];
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...