#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, p;
vector<int> o, ptop, top, nrt, dp;
vector<vector<int>> v, vi;
void maketop()
{
queue<int> q;
for(int i = 1; i <= n; i ++)
if(!nrt[i])
q.push(i);
while(!q.empty())
{
int x = q.front();
q.pop();
for(auto i : v[x])
{
nrt[i] --;
if(!nrt[i])
q.push(i);
}
top.push_back(x);
}
for(int i = 1; i <= n; i ++)
ptop[top[i]] = i;
}
bool cmp(int x, int y)
{
return ptop[x] < ptop[y];
}
void rsz()
{
o.resize(p + 1);
ptop.resize(n + 1);
dp.resize(n + 1);
nrt.resize(n + 1);
top.push_back(-1);
v.resize(n + 1);
vi.resize(n + 1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> p;
rsz();
for(int i = 1; i <= p; i ++)
cin >> o[i];
for(int i = 1; i <= m; i ++)
{
int u, w;
cin >> u >> w;
v[u].push_back(w);
vi[w].push_back(u);
nrt[w] ++;
}
maketop();
int maxx = 0;
for(int i = 1; i <= p; i ++)
maxx = max(maxx, ptop[o[i]]);
if(maxx > ptop[n])
{
cout << 0;
return 0;
}
sort(o.begin() + 1, o.end(), cmp);
o.push_back(n);
int pos = 1;
dp[1] ++;
for(int i = 1; i <= n; i ++)
{
if(top[i] == o[pos])
pos ++;
for(auto j : v[top[i]])
{
if(ptop[j] <= ptop[o[pos]])
dp[j] += dp[top[i]], dp[j] %= mod;
}
}
cout << dp[n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
1116 KB |
Output is correct |
10 |
Correct |
5 ms |
928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
56 ms |
9984 KB |
Output is correct |
3 |
Correct |
60 ms |
10068 KB |
Output is correct |
4 |
Correct |
54 ms |
10068 KB |
Output is correct |
5 |
Correct |
35 ms |
6740 KB |
Output is correct |
6 |
Correct |
126 ms |
24028 KB |
Output is correct |
7 |
Correct |
124 ms |
24148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
1116 KB |
Output is correct |
10 |
Correct |
5 ms |
928 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
56 ms |
9984 KB |
Output is correct |
13 |
Correct |
60 ms |
10068 KB |
Output is correct |
14 |
Correct |
54 ms |
10068 KB |
Output is correct |
15 |
Correct |
35 ms |
6740 KB |
Output is correct |
16 |
Correct |
126 ms |
24028 KB |
Output is correct |
17 |
Correct |
124 ms |
24148 KB |
Output is correct |
18 |
Correct |
291 ms |
36740 KB |
Output is correct |
19 |
Correct |
270 ms |
34856 KB |
Output is correct |
20 |
Correct |
330 ms |
34420 KB |
Output is correct |
21 |
Correct |
76 ms |
12696 KB |
Output is correct |
22 |
Correct |
272 ms |
35472 KB |
Output is correct |